`
hellosoft
  • 浏览: 56542 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

升级文件预备份工具

阅读更多
主要功能:
在升级系统之前,
首先将本次升级所需要覆盖的文件,按照原来的目录结构copy出来,备份在指定的地方。
万一升级有问题,可以使用这个预备份的进行还原。
注意:这个工具只能处理文件覆盖的问题,暂时不能处理 新增或者删除文件的情况。
建议使用 linux + php5.2以及以上版本。
如果是windows 请将常量 CNT_SEPARATE_CHAR_FOR_PATH 更改为 \。 应该可以的,不过笔者就没试过而已。

<?php
/**
 * 名称   :  升级文件预备份工具
 * 主要功能:
 *      在升级系统之前,
 *      首先将本次升级所需要覆盖的文件,按照原来的目录结构copy出来,备份在指定的地方。
 *      防止,万一升级有问题,可以使用这个预备份的进行还原。
 *
 *      注意:这个工具只能处理文件覆盖的问题,暂时不能处理 新增或者删除文件的情况。
 *
 * Step:
 * 1. check arguments
 * 2. scan upgRoot,
 *      1) mkdir in desc
 *      2) copy the file to Desc
 * 
 * If desc not exist, create it.
 * Suggest : Desc is empty, or is not exists.
 */
define("CNT_SEPARATE_CHAR_FOR_PATH", "/");
define("CNT_SHOW_DETAIL", true);
define("CNT_SHOW_NOT_FIND", true); # if (CNT_SHOW_NOT_FIND and CNT_SHOW_DETAIL) show the file or dir which do not find 

# 1. check arguments
if (count($argv) != 4 ){
    showHelp("miss arguments .");
}

$appRoot = $argv[1];
$upgRoot = $argv[2];
$desRoot = $argv[3];

if (!is_dir($appRoot)){
    showHelp(" [{$appRoot}] is not exist, or is not a directory.");
}
if (!is_dir($upgRoot)){
    showHelp(" [{$upgRoot}] is not exist, or is not a directory.");
}


# 2. scan upgRoot
echo "\n";
if (CNT_SHOW_DETAIL) echo "{$desRoot}\n";
$prefixForShow = "";
$re = copyUpgFileFromAppToDesc($appRoot, $upgRoot,  $desRoot, $prefixForShow);
if ($re){
    echo "\nCongratulation! The {$desRoot} has been created successfully.\n\n";
}else{
    echo "\nMay be something is wrong, check it and try again. \n";
}

# finish

#------------- functions -----------------------

function copyUpgFileFromAppToDesc($appPath, $upgPath,  $desPath, $prefixForShow){
    $re = true;

    if (!file_exists($desPath)) mkdir_p($desPath); // 创建相应目录

    
    $fileList = scandir($upgPath);
    $count = count($fileList) - 2; # 因为有 . ..
    $index = 0;
    foreach ($fileList as $fileName){
        if ($fileName == "." || $fileName == "..") continue; // next cycle
        $index++;
        $isLast = ($index == $count);
        #$nSpaceChar = getSpaceChar($prefixForShow * 4);

        $appFullFile = $appPath . CNT_SEPARATE_CHAR_FOR_PATH . $fileName;
        $upgFullFile = $upgPath . CNT_SEPARATE_CHAR_FOR_PATH . $fileName;
        $desFullFile = $desPath . CNT_SEPARATE_CHAR_FOR_PATH . $fileName;

        if (!file_exists($appFullFile)){
            if (CNT_SHOW_DETAIL && CNT_SHOW_NOT_FIND){
                if (!$isLast){
                    echo $prefixForShow . "|-- " . $fileName . " (not find) \n";
                }else{
                    echo $prefixForShow . "`-- " . $fileName . " (not find)  \n";
                }
            }        
        continue; // app中不存在相应文件,就不必再copy了
        } 

        if (CNT_SHOW_DETAIL){
            if (!$isLast){
                echo $prefixForShow . "|-- " . $fileName . "\n";
            }else{
                echo $prefixForShow . "`-- " . $fileName . "\n";
            }
        }
        if (is_dir($upgFullFile)){
            $nextPrefix = $prefixForShow . "|   ";
           if ($isLast) $nextPrefix = $prefixForShow . "    ";
            $re = copyUpgFileFromAppToDesc($appFullFile,$upgFullFile,$desFullFile, $nextPrefix);
            if (!$re){
                echo "Error: copy direcotry [{$appFullFile}] to [{$desFullFile}]";
                break;
            }
        }else{
            $re = copy($appFullFile, $desFullFile);
            if (!$re){
                echo "Error: copy file [{$appFullFile}] to [{$desFullFile}]";
                break;
            }
        }

    }

    return $re;
}

// 强行创建相应的目录
function mkdir_p($dirname){
    if (!file_exists($dirname)){
        mkdir_p(dirname($dirname));
        mkdir($dirname);
    }
}
function getSpaceChar($n){
    $re = "";
    for($i =0; $i<$n; $i++) $re.=" ";
    return $re;
}

function showHelp($errorMessage = ""){ // with exit.
    if (!$errorMessage == ""){
         echo $errorMessage . "\n";
    }
    echo "Usage:

    php bak_file_before_upgrade.php RootPath_Application RootPath_UpgradePackage RootPath_Desc

    If one file is both in RootPath_Application and RootPath_UpgradePackage,
    we will copy the file in RootPath_Application to RootPath_Desc.
    If it in subdirecotry, then we will create the same name directory in RootPath_Desc.

    e.g.
    php bak_file_before_upgrade.php /export/yourApplication /tmp/upgrade/yourUpgradePackage /tmp/bakbeforeupgrade/bakupDirectory
    \n";
    exit;
}

?>
1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics