项目恢复脚本
必须配合我之前的备份脚本使用
实现功能
- 能回退最近版本
变量解释
project=$project_name #数据恢复项目名,需存储端有该项目的数据
restore_path=/ #数据恢复路径
backup_store_path=$dest_path #备份数据路径
restore_version=1 #恢复最近第一次数据,可以更改
source_back_path=/tmp #原始数据存放目录
配合备份脚本恢复示例
本次我将以/test/index.html为例
[root@cicd-node ~]# cat /test/index.html
<html>
<body>
<h1> version 1 </h1>
</body>
</html>
[root@cicd-node ~]# export source_paths="/test/index.html" #指定源地址
[root@cicd-node ~]# export project_name="test" #指定项目名
[root@cicd-node ~]# export dest_path="/backup" #指定存储地址
[root@cicd-node ~]# bash backup.sh project fullbackup #执行备份脚本(必须是我的脚本)
项目名:test
项目备份地址:/test/index.html
数据存储位置:/backup/project/test
-----------开始校验----------
源md5值:1c2a10fa4b34839fcec1f972afa3589e
备份md5值:1c2a10fa4b34839fcec1f972afa3589e
[root@cicd-node ~]# tree /backup/ #查看备份是否成功
/backup/
└── project
├── test #项目名
├── backup_path.data
└── full
└── 2020
└── 11
└── 12
└── test-2020-11-12_01-29-47.tar.gz #备份包
[root@cicd-node ~]# vi /test/index.html #将代码更新至版本2,准备恢复
<html>
<body>
<h1> version 2 </h1>
</body>
</html>
[root@cicd-node ~]# bash restore.sh #执行恢复脚本
tar: Removing leading `/' from member names
恢复成功
[root@cicd-node ~]# cat /test/index.html #查看恢复结果
<html>
<body>
<h1> version 1 </h1>
</body>
</html>
由此可见,我们对版本一进行了备份,然后将其更新至版本二,执行恢复脚本,将其成功恢复到版本一
脚本内容
#!/bin/bash
date=`date`
source /etc/profile
project=$project_name #数据恢复项目名,需存储端有该项目的数据
restore_path=/ #数据恢复路径
backup_store_path=$dest_path #备份数据路径
restore_version=1 #恢复最近第一次数据
source_back_path=/tmp
init_check(){
#初始检查,检测项目是否有备份,检测数据恢复路径是否存在,检测最近第$restore_version次数据是否存在
if [ ! -d $backup_store_path ];then
echo "备份数据路径$backup_store_path不存在"
exit 1
fi
if [ ! -d $backup_store_path/project/$project/full ];then
echo "备份池里不存在$project项目的全备资源"
exit 1
fi
if [ ! -d $restore_path ];then
echo "恢复路径:$restore_path 不存在目录"
exit 1
fi
backup_nums=`find $backup_store_path/project/$project/full -name "*.tar.gz" -type f|wc -l` #获取该项目拥有备份数量
if [ $restore_version -gt $backup_nums ];then
echo "存储里只有$backup_nums次备份,而你需要恢复的是最近第$restore_version次,超出!"
exit 1
fi
#获取恢复文件
restore_file=`ls -t \`find $backup_store_path/project/$project/full -name "*.tar.gz" -type f \` | sed -n "$restore_version p"`
if [ $? -eq 0 ];then
#调用恢复函数
restore $restore_file $restore_path
if [ $? -eq 0 ];then
echo "恢复成功"
else
echo "恢复错误"
exit 1
fi
else
echo "获取恢复文件出错"
exit 1
fi
}
restore(){
#备份函数,先将现有数据移动至回收站,再将压缩包拷过来本地解压
#$1 需要恢复的备份文件
#$2 恢复的路径
restore_file=$1
res_path=$2
if [ ! -f $backup_store_path/project/$project/backup_path.data ];then
echo "$backup_store_path/project/$project/backup_path.data不存在,上次备份是否成功"
fi
#获取需要移动至回收站的路径
for s_path in `cat $backup_store_path/project/$project/backup_path.data`
do
if [ -d /tmp/$project ];then
rm -rf /tmp/$project
fi
mv -f $s_path $source_back_path/$project
done
rsync -azlD $restore_file /tmp
if [ $? -ne 0 ];then
echo "恢复包$restore_file拷贝到本地失败"
exit 1
fi
file_name=`echo $restore_file|awk -F '/' '{print $NF}'`
tar zxf /tmp/$file_name -C $res_path
if [ $? -ne 0 ];then
echo "恢复包/tmp/$file_name解压到$res_path失败"
exit 1
fi
return 0
}
init_check