写一个使用shell脚本增量备份系统文件,顺便复习一下shell脚本相关的命令,这个脚本可以根据自己的需求来备份不同的文件或者文件夹,进行完整备份和增量备份。
参考的网址:http://blog.51cto.com/wgkgood/1330200
直接上脚本如下:
1 ######################################################################### 2 # File Name: back_template.sh 3 # Author: happy 4 # mail: zaqwsx526@163.com 5 # Created Time: Wed 06 Jun 2018 12:17:16 PM CST 6 ######################################################################### 7 #!/bin/bash 8 SOURCE_DIR=( 9 $* 10 ) 11 TARGET_DIR=/backup/data/ 12 YEAR=`date +%Y` 13 MONTH=`date +%m` 14 DAY=`date +%d` 15 WEEK=`date +%u` 16 FILES=system_backup.tgz 17 CODE=$? 18 if [ -z "$*" ];then 19 echo -e "Please Enter Your Backup Files or Directories -------------------------------------------- Example $0 /boot /etc ......" 20 exit 21 fi 22 #Determine Whether the Target Directory Exists 23 if [ ! -d $TARGET_DIR/$YEAR/$MONTH/$DAY ];then 24 mkdir -p $TARGET_DIR/$YEAR/$MONTH/$DAY 25 echo "This $TARGET_DIR is Created Successfully !" 26 fi 27 #EXEC Full_Backup Function Command"" 28 Full_Backup() 29 { 30 if [ "$WEEK" -eq "7" ];then 31 rm -rf $TARGET_DIR/snapshot 32 cd $TARGET_DIR/$YEAR/$MONTH/$DAY 33 tar -g $TARGET_DIR/snapshot -czvf $FILES `echo ${SOURCE_DIR[@]}` 34 [ "$CODE" == "0" ]&&echo -e "-------------------------------------------- These Full_Backup System Files Backup Successfully !" 35 fi 36 } 37 #Perform incremental BACKUP Function Command 38 Add_Backup() 39 { 40 cd $TARGET_DIR/$YEAR/$MONTH/$DAY ; 41 if [ -f $TARGET_DIR/$YEAR/$MONTH/$DAY/$FILES ];then 42 read -p "These $FILES Already Exists, overwrite confirmation yes or no ? : " SURE 43 if [ $SURE == "no" -o $SURE == "n" ];then 44 sleep 1 ;exit 0 45 fi 46 #Add_Backup Files System 47 if [ $WEEK -ne "7" ];then 48 cd $TARGET_DIR/$YEAR/$MONTH/$DAY ;tar -g $TARGET_DIR/snapshot -czvf $$_$FILES `echo ${SOURCE_DIR[@]}` 49 [ "$CODE" == "0" ]&&echo -e "----------------------------------------- These Add_Backup System Files Backup Successfully !" 50 fi 51 else 52 if [ $WEEK -ne "7" ];then 53 cd $TARGET_DIR/$YEAR/$MONTH/$DAY ;tar -g $TARGET_DIR/snapshot -czvf $FILES `echo ${SOURCE_DIR[@]}` 54 [ "$CODE" == "0" ]&&echo -e "------------------------------------------- These Add_Backup System Files Backup Successfully !" 55 fi 56 fi 57 } 58 Full_Backup;Add_Backup