dataPath='opt/zndz/bin/logs/';
findDataPaths=`find /${dataPath} -ctime -2 -type f`
temp_path='/opt/temp_backups/';
sudo mkdir -p ${temp_path}
for path in ${findDataPaths}
do
sudo mkdir -p ${temp_path}${path%/*}
sudo cp ${path} ${temp_path}${path%/*}
echo ${path%/*}
echo ${path##*/}
done;
oldTarPath=`find /opt/ -name '*backup.tar'`
createTime=$(date +%Y%m%d)-$(date +%H%M%S);
cd ${temp_path}
newBackupPath='/opt/logs-'${createTime}'-backup.tar'
sudo tar -cvzf ${newBackupPath} ${dataPath}
sudo rm -rf ${temp_path}
if [ ! -n "$oldTarPath" ]; then
echo '没有发现历史压缩包'
else
cd /${dataPath}
mergeTime=$(date +%Y%m%d)-$(date +%H%M%S)
sudo cat ${oldTarPath} ${newBackupPath} > /opt/logs-${mergeTime}-backup.tar
cd /${dataPath}
sudo rm -f ${oldTarPath}
sudo rm -f ${newBackupPath}
fi
1、没有历史备份文件执行后效果如下 2.1、有历史备份的执行,为了方便查看,本次执行注释掉了历史版本的删除 : 执行后会生成三个压缩包,第一个是历史备份文件,第二个是本次备份文件,第三个是合并后的备份文件 2.2、有历史备份的执行,本次执行没有注释掉删除历史版本,图一是执行前,图二是执行后
|