升级文件预备份工具

主要功能:
在升级系统之前,
首先将本次升级所需要覆盖的文件,按照原来的目录结构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;
}

?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值