Linux-定时任务备份并压缩文件,同时删除原旧的备份文件
脚本如下
[mickmao@bogon ~]$ cat backup_avps.sh
#!/bin/bash
AvpsPathHome=/home/mickmao
BackupPathHome=/home/mickmao
#AvpsPath="$AvpsPathHome"
BackupPath="$BackupPathHome"/avps_bak$(date +%y%m%d).tar.gz
#backup_logs下面会自动生成back.log文件
LogFile="$BackupPathHome"/backup_logs/back.log
#输出重定向到LogFile
echo $(date +"%y-%m-%d") >> $LogFile
echo "-------------begin to back up avps file------------------" >> $LogFile
#查找旧的压缩备份文件并删除
find $BackupPathHome -type f -name "avps_bak*" | xargs rm -rf
#echo "results: $?" >> $LogFile
if [ $? -eq 0 ]
then
cd $AvpsPathHome
tar -zcP -f $BackupPath ./avps
if [ $? -eq 0 ]
then
echo "$(date +'%y-%m-%d %H:%M:%S') Backup success" >> $LogFile
fi
else
echo "$(date +'%y-%m-%d %H:%M:%S') Fail to remove avps_bak file" >> $LogFile
fi
echo "-------------end the back up process------------------" >> $LogFile
设置crontab 定时运行