因业务需求,需对一个目录进行定期备份,现在使用tar进行自动备份,计划每周一次完整备份,其他每天进行增量或者差异备份,所以写出下面的脚步通过指定参数来选择备份模式。备份文件只保留最近两个完整备份版本和附属的增量或差异备份版本。
[root@desktop1 script]# ./backup.sh Please select the backup or delete mode,[full|diff|incr|delete|help] [root@desktop1 script]# ./backup.sh help full-------完整备份 diff-------差异备份 incr-------增量备份 delete-----删除备份
脚步正文:
#!/bin/bash # Backup script # 2014/5/25 by Dinker # Source directory,modify as need. SOURCEDIR=/root/test # Backup file directory,modify as need. BAKDIR=/root # The last name of the directory interception,modify as need. #name=${SOURCEDIR##*/} name=`basename $SOURCEDIR` LOG=$BAKDIR/$name.log DB=$BAKDIR/$name-bakdb DATE=`date +%Y%m%d-%H%M%S` if [ $# != 1 ];then echo -e "\033[034;1mPlease select the backup or delete mode,\033[0m\033[033;1m[full|diff|incr|delete|help]\033[0m" >&2 exit 1 fi OPT=$1 case $OPT in full) # Full backup script # Read db file and config #控制主备版本号 if [ -f "$DB" ]; then eval `grep FULLVER $DB` OLD=$FULLVER NEW=$((OLD+1)) else NEW=1 fi BAKFILE=$name-full-$NEW-$DATE.tar.gz SNAP=$name-$NEW.snap tar -cvzpf $BAKDIR/$BAKFILE -g /$BAKDIR/$SNAP $SOURCEDIR echo FULLVER=$NEW > $DB echo "------------------------------------" >> $LOG echo "$BAKFILE " >> $LOG echo -e "\033[32;1mArchived $BAKFILE with snapshot $SNAP\033[0m" ;; diff) # Different backup script # Read db file and config #读取主备版本号 if [ -f "$DB" ]; then eval `grep FULLVER $DB` else echo -e "\033[031;1mWithout a full backup, backup cannot be different.\033[0m" exit 1 fi if [ -f "$DB" ]; then #控制差异备份的版本号 OLD=`grep DIFFVER $DB | tail -n 1 | awk -F= ‘{ print $2 }‘` NEW=$(($OLD+1)) else NEW=1 fi BAKFILE=$name-diff-$FULLVER-$NEW-$DATE.tar.gz # Find the latest full backup file. FULLBAK=`find $BAKDIR -name "$name-full-$FULLVER-*" -exec ls {} \;` find $SOURCEDIR -newer $FULLBAK -print0 |tar --null -czpvf $BAKDIR/$BAKFILE -T - echo DIFFVER=$NEW >> $DB echo $BAKFILE >> $LOG echo -e "\033[32;1mArchived $BAKFILE\033[0m" ;; incr) # Incremental backup script # Read the full version. if [ -f "$DB" ]; then eval `grep FULLVER $DB` else echo -e "\033[031;1mWithout a full backup, backup cannot be incremental.\033[0m" exit fi # Incremental version control if [ -f "$DB" ]; then OLD=`grep INCRVER $DB | tail -n 1 | awk -F= ‘{ print $2 }‘` NEW=$(($OLD+1)) else NEW=1 fi BAKFILE=$name-incr-$FULLVER-$NEW-$DATE.tar.gz VERSNAP=$name-$FULLVER.snap # Begin incremental backup. tar -cvzpf $BAKDIR/$BAKFILE -g /$BAKDIR/$VERSNAP $SOURCEDIR echo INCRVER=$NEW >> $DB echo $BAKFILE >> $LOG echo -e "\033[032;1mArchived $BAKFILE with snapshot $VERSNAP\033[0m" ;; delete) # Delete the old backup files. #自动删除14前的备份文件 # find $BAKDIR -name "$name-$DEL.snap" -mtime -14 -exec rm -rf {} \; # find $BAKDIR -name "$name-full-*" -mtime -14 -exec rm -rf {} \; # find $BAKDIR -name "$name-incr-*" -mtime -14 -exec rm -rf {} \; # find $BAKDIR -name "$name-diff-*" -mtime -14 -exec rm -rf {} \; #通过读取全备的版本号来实现删除指定的备份文件 if [ -f "$DB" ]; then eval `grep FULLVER $DB` DEL=`expr $FULLVER - 2` fi echo -e "\033[32;1mThe delete full version is\033[0m \033[31;1m$DEL\033[0m" #查找需要删除的文件 if [ "$DEL" -ge 1 ]; then snapdel=`find $BAKDIR -name "$name-$DEL.snap"` fulldel=`find $BAKDIR -name "$name-full-$DEL-*"` incrdel=`find $BAKDIR -name "$name-incr-$DEL-*" |sort` diffdel=`find $BAKDIR -name "$name-diff-$DEL-*" |sort` fi # Delete the backup file and modify it states in the log file. for i in [ $snapdel $fulldel $incrdel $diffdel ] do echo $i |sed ‘s/\ /\n/g‘ |awk -F/ ‘{print $3}‘ |xargs -I {} sed -i ‘s/{}/{}---------Deleted/‘ $LOG #修改日志文件中已删除文件为Deleted rm -rf $i #删除备份 done echo -e "\033[32;1mNow delete the following files.\033[0m \033[32;1m\n---------------------------------------\n$snapdel \n$fulldel \n$incrdel \n$diffdel \n---------------------------------------\033[0m" ;; help) echo -e "\033[32;1mfull-------完整备份\ndiff-------差异备份\nincr-------增量备份\ndelete-----删除备份\033[0m" ;; *) echo -e "\033[034;1mPlease select the backup or delete mode,\033[0m\033[033;1m[full|diff|incr|delete|help]\033[0m" >&2 ;; esac
测试使用:
[root@desktop1 script]# ./backup.sh full tar: /root/test: Directory is new tar: Removing leading `/‘ from member names /root/test/ /root/test/1 /root/test/passwd /root/test/yppasswd Archived test-full-1-20140525-122919.tar.gz with snapshot test-1.snap [root@desktop1 script]# ./backup.sh diff Archived test-diff-1-1-20140525-122925.tar.gz [root@desktop1 script]# ./backup.sh incr tar: Removing leading `/‘ from member names /root/test/ Archived test-incr-1-1-20140525-122930.tar.gz with snapshot test-1.snap
查看生成的日志文件
[root@desktop1 script]# cat /root/test.log ------------------------------------ test-full-1-20140525-122919.tar.gz test-diff-1-1-20140525-122925.tar.gz test-incr-1-1-20140525-122930.tar.gz
为了测试需求,我这里使用seq 6|xargs -i command 重复运行一条命令,
[root@desktop1 script]# ./backup.sh full && seq 6 |xargs -i ./backup.sh incr && seq 6 |xargs -i ./backup.sh diff
日志:
[root@desktop1 script]# cat /root/test.log ------------------------------------ test-full-1-20140525-122919.tar.gz test-diff-1-1-20140525-122925.tar.gz test-incr-1-1-20140525-122930.tar.gz test-diff-1-2-20140525-123639.tar.gz test-diff-1-3-20140525-123639.tar.gz test-diff-1-4-20140525-123639.tar.gz test-diff-1-5-20140525-123639.tar.gz test-diff-1-6-20140525-123639.tar.gz test-incr-1-2-20140525-123650.tar.gz test-incr-1-3-20140525-123650.tar.gz test-incr-1-4-20140525-123650.tar.gz test-incr-1-5-20140525-123650.tar.gz test-incr-1-6-20140525-123650.tar.gz ------------------------------------ test-full-2-20140525-123755.tar.gz test-incr-2-1-20140525-123755.tar.gz test-incr-2-2-20140525-123755.tar.gz test-incr-2-3-20140525-123755.tar.gz test-incr-2-4-20140525-123755.tar.gz test-incr-2-5-20140525-123755.tar.gz test-incr-2-6-20140525-123755.tar.gz test-diff-2-1-20140525-123755.tar.gz test-diff-2-2-20140525-123755.tar.gz test-diff-2-3-20140525-123755.tar.gz test-diff-2-4-20140525-123755.tar.gz test-diff-2-5-20140525-123755.tar.gz test-diff-2-6-20140525-123755.tar.gz ------------------------------------ test-full-3-20140525-123759.tar.gz test-incr-3-1-20140525-123759.tar.gz test-incr-3-2-20140525-123759.tar.gz test-incr-3-3-20140525-123759.tar.gz test-incr-3-4-20140525-123759.tar.gz test-incr-3-5-20140525-123759.tar.gz test-incr-3-6-20140525-123759.tar.gz test-diff-3-1-20140525-123759.tar.gz test-diff-3-2-20140525-123759.tar.gz test-diff-3-3-20140525-123759.tar.gz test-diff-3-4-20140525-123759.tar.gz test-diff-3-5-20140525-123759.tar.gz test-diff-3-6-20140525-123759.tar.gz
使用delete删除历史备份文件,并在日志文件中修改已删除备份的状态;我这里是删除最新的两个版本的前一版本
[root@desktop1 script]# ./del-bakfile.sh delete The delete full version is 1 Now delete the following files. --------------------------------------- /root/test-1.snap /root/test-full-1-20140525-122919.tar.gz /root/test-incr-1-1-20140525-122930.tar.gz /root/test-incr-1-2-20140525-123650.tar.gz /root/test-incr-1-3-20140525-123650.tar.gz /root/test-incr-1-4-20140525-123650.tar.gz /root/test-incr-1-5-20140525-123650.tar.gz /root/test-incr-1-6-20140525-123650.tar.gz /root/test-diff-1-1-20140525-122925.tar.gz /root/test-diff-1-2-20140525-123639.tar.gz /root/test-diff-1-3-20140525-123639.tar.gz /root/test-diff-1-4-20140525-123639.tar.gz /root/test-diff-1-5-20140525-123639.tar.gz /root/test-diff-1-6-20140525-123639.tar.gz --------------------------------------- [root@desktop1 script]# cat /root/test.log ------------------------------------ test-full-1-20140525-122919.tar.gz---------Deleted test-diff-1-1-20140525-122925.tar.gz---------Deleted test-incr-1-1-20140525-122930.tar.gz---------Deleted test-diff-1-2-20140525-123639.tar.gz---------Deleted test-diff-1-3-20140525-123639.tar.gz---------Deleted test-diff-1-4-20140525-123639.tar.gz---------Deleted test-diff-1-5-20140525-123639.tar.gz---------Deleted test-diff-1-6-20140525-123639.tar.gz---------Deleted test-incr-1-2-20140525-123650.tar.gz---------Deleted test-incr-1-3-20140525-123650.tar.gz---------Deleted test-incr-1-4-20140525-123650.tar.gz---------Deleted test-incr-1-5-20140525-123650.tar.gz---------Deleted test-incr-1-6-20140525-123650.tar.gz---------Deleted ------------------------------------ test-full-2-20140525-123755.tar.gz test-incr-2-1-20140525-123755.tar.gz test-incr-2-2-20140525-123755.tar.gz test-incr-2-3-20140525-123755.tar.gz test-incr-2-4-20140525-123755.tar.gz test-incr-2-5-20140525-123755.tar.gz test-incr-2-6-20140525-123755.tar.gz test-diff-2-1-20140525-123755.tar.gz test-diff-2-2-20140525-123755.tar.gz test-diff-2-3-20140525-123755.tar.gz test-diff-2-4-20140525-123755.tar.gz test-diff-2-5-20140525-123755.tar.gz test-diff-2-6-20140525-123755.tar.gz ------------------------------------ test-full-3-20140525-123759.tar.gz test-incr-3-1-20140525-123759.tar.gz test-incr-3-2-20140525-123759.tar.gz test-incr-3-3-20140525-123759.tar.gz test-incr-3-4-20140525-123759.tar.gz test-incr-3-5-20140525-123759.tar.gz test-incr-3-6-20140525-123759.tar.gz test-diff-3-1-20140525-123759.tar.gz test-diff-3-2-20140525-123759.tar.gz test-diff-3-3-20140525-123759.tar.gz test-diff-3-4-20140525-123759.tar.gz test-diff-3-5-20140525-123759.tar.gz test-diff-3-6-20140525-123759.tar.gz
本文出自 “Dinker” 博客,请务必保留此出处http://dinker.blog.51cto.com/8733303/1416821
利用tar进行完全备份、增量备份、差异备份,并控制版本号,自动删除备份文件,布布扣,bubuko.com
利用tar进行完全备份、增量备份、差异备份,并控制版本号,自动删除备份文件
原文地址:http://dinker.blog.51cto.com/8733303/1416821