在linux上做到备份&更新单文件时的一个脚本,支持rollback,

由于日常中可能要更换个IP库什么的,但是线上的东西又不想停掉,同时考虑到,如果更新失败,怎么能快速回退(rollback),自己就动手写了一个shell脚本,当然是现学现卖,不过亲测好使。

老规矩,直接上代码:upgrade.sh 


#!/bin/sh
#by airzen 20130131 

filename=$1
newfilename=$filename.new
bakfilename=$filename.bak

if [ ! -f $filename ]; then
    printf "$filename NOT is exist\n"
    exit
fi

function upgrade()
{
    if [ -f $newfilename ]; then
        mv $filename $bakfilename
        mv $newfilename $filename
    else
        printf "$newfilename NOT is exist\n"
    fi
}

function restore()
{
    if [ -f $bakfilename ]; then
        mv $filename $newfilename
        mv $bakfilename $filename
    else
       printf "$bakfilename NOT is exist\n"
    fi 
}

if [ "$2" = "go" ]; then
    upgrade
elif [ "$2" = "back" ]; then
    restore
else
    printf "Usage:CMD {path/fn.ext}  {go|back} \n"
    printf "go   - MUST exist the [path/fn.ext.new] \n"
    printf "back - MUST exist the [path/fn.ext.bak] \n"
fi
思路:更新前的准备工作,将要进行更新的文件以[文件名].new 的方式准备好,放到同一目录下,执行


有图有真相。哈哈。

可以将此工具放到一个单独的目录,执行的时候,采用upgrade.sh 全路径 + 更新文件的全路径 + 参数(go|back),

当发现更新有问题的时候,直接使用上光标键调出命令,将go改为back,回车,当然你觉得还不够快可以加入文件自检查进行更新或是回退操作!希望对大家有帮助。


转载于:https://my.oschina.net/airzen/blog/117964

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 上线前的备份工作: 按日期生成上线记录文件文件结构目录和正式环境保持一致 备份你所涉及到的修改文件 2. 上线前的准备工作: 把你修改到的patch文件 到 PATCH_ROOT 目录中 文件结构目录和正式环境保持一致 3. 上线工作: diff -r -b 比较 备份环境 和 发布环境 文件差异; 再次确定修改是否正确 PATCH_ROOT 下的文件夹和文件 发布到正式环境 即可; 完成上线后,要检查系统功能是否正确。 4. 回滚工作: BAK_ROOT 下的文件夹和文件 回滚到正式环境 即可 ; 完成回滚后 要检查系统功能是否正确回滚。 5. shell脚本功能: === 参数配置 ===: #指定正式环境的路径 正式环境的文件 WEB_ROOT=/var/www/html echo ${WEB_ROOT} #指定发布环境的路径 发布环境的文件 PAT_ROOT=/home/chenlong/blog echo ${PAT_ROOT} #指定发布管理日志的路径 记录发布过程,完成文件备份文件patch PUB_ROOT=/home/chenlong/Publish === 使用方法 ===: #./apply.sh blog /home/chenlong/patch_sh/list20100520.txt 例如 -bash-3.2$ ./apply.sh blog /home/chenlong/patch_sh/list20100520.txt /var/www/html /home/chenlong/blog /home/chenlong/Publish blog /home/chenlong/patch_sh/list20100520.txt /home/chenlong/patch_sh === cmd === :/home/chenlong/patch_sh/apply_cp.sh /var/www/html/wp-includes/js/common.js /home/chenlong/Publish/blog/2010-05-21/bak /bin/cp /var/www/html/wp-includes/js/common.js /home/chenlong/Publish/blog/2010-05-21/bak/var/www/html/wp-includes/js/common.js === cmd === :/home/chenlong/patch_sh/apply_cp.sh /var/www/html/upload_editor.php /home/chenlong/Publish/blog/2010-05-21/bak /bin/cp /var/www/html/upload_editor.php /home/chenlong/Publish/blog/2010-05-21/bak/var/www/html/upload_editor.php === cmd === :/home/chenlong/patch_sh/apply_cp.sh /var/www/html/BM/Tpl/default/Admin/Img/index.php /home/chenlong/Publish/blog/2010-05-21/bak /bin/cp /var/www/html/BM/Tpl/default/Admin/Img/index.php /home/chenlong/Publish/blog/2010-05-21/bak/var/www/html/BM/Tpl/default/Admin/Img/index.php === cmd === :/home/chenlong/patch_sh/apply_cp.sh /home/chenlong/blog/wp-includes/js/common.js /home/chenlong/Publish/blog/2010-05-21/patch /bin/cp /home/chenlong/blog/wp-includes/js/common.js /home/chenlong/Publish/blog/2010-05-21/patch/home/chenlong/blog/wp-includes/js/common.js === cmd === :/home/chenlong/patch_sh/apply_cp.sh /home/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值