1.新建脚本
1 | vi backup.sh |
脚本如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | #!/bin/bash #backup.sh #备份至/home/bak/webbackup/ BackupPath=/home/bak/webbackup/ LogFile=/home/bak/webback.log DBPath=/home/wwwroot/ #需要备份的目录 NewFile="$BackupPath"web_back$(date +%y%m%d).tgz DumpFile="$BackupPath"web_back$(date +%y%m%d) OldFile="$BackupPath"web_back$(date +%y%m%d --date='7 days ago').tgz echo "-------------------------------------------" >> $LogFile echo $(date +"%y-%m-%d %H:%M:%S") >> $LogFile echo "--------------------------" >> $LogFile #Delete Old File if [ -f $OldFile ] then rm -f $OldFile >> $LogFile 2>&1 echo "[$OldFile]Delete Old File Success!" >> $LogFile else tar cpzvf $NewFile $DBPath >> $LogFile 2>&1 echo "[$OldFile]No Old Backup File!" >> $LogFile fi if [ -f $NewFile ] then echo "[$NewFile]The Backup File is exists,Can't Backup!" >> $LogFile # tar pczvf $NewFile $DBPath >> $LogFile 2>&1 echo "[$NewFile]Backup Success!" >> $LogFile else tar cpzvf $NewFile $DBPath >> $LogFile 2>&1 fi #------------------------结束 |
2.添加计划任务
1 | crontab -e |
1 | 0 03 * * 7 /home/bak/web_backup.sh |
说明:每周日 3点备份一次
3.wq保存后 重启计划任务服务
1 | service crond restart |
完成配置