Shell script for full backup data and differential backup data
#! / bin/bash # # Description: this script features weekly cycle; full backup is implemented on Friday, no backup on Saturday, and differential backup from Sunday to Thursday; # due to work needs, full backup of cp and differential backup of rsync are not implemented. # to use this script, please directly modify the following two parameters: BackupPath and DATAPath. Please do not modify it anywhere else. # DATE: 2017-04-24 22 VERSION: 2017-08-28 17:20:00 # VERSION: 2.imports Author: xixi_chan# imports the PATH environment variable export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/bin:/sbin:/root/bin# redefines the IFS variable to avoid splitting the directory or file name with spaces IFS= `echo-en "\ n\ b" `# define global variable BackupPath=/mnt/backup_data/# specify backup path MountPath=//ds-bk01/drv-y# specify windwos backup server share path DATAPath=/data/ # directories to be backed up Time= `date +% Ymuri% d` # define Today's time variable Host= `echo $HOSTNAME | awk-filtering.'{print $1}'` # define hostname LogFile= "/ Var/log/$ {Host} / ${Host} _ ${Time} .log "# define the log file name LogDir= `echo" $LogFile "| sed-r's @ [^ /] + /? $@ @ g'` # define the directory where the log file is located DirFile= `ls $datapath` # Save the files and directories under the backup path to the variable DirFile" ${BackupPath} ${Host} ${Time} / `basename ${DATAPath} `/ "# Root Define the current backup directory OldFirstFile= "${BackupPath} ${Host} _ $(date +% Y-%m-%d-- date='7 days ago')" # define the backup directory Mail_Group= "test@aaa.com" # define the recipient username=user # define the user name password=password # define the password for mounting the backup server # record the log file The opening part Head () {[!-d $LogDir] & & mkdir $LogDirecho "[Japanese] do you know what to do? "AB Linux"? Why don't you tell me what to do? Do you want to introduce "s-smb01"? What are you doing? I don't know what to do. I don't know how to deliver the letter. " > $LogFileecho "[Chinese] this email is sent through AB's linux server to confirm the data backup of the s-smb01 server!" > $LogFile 2 > & 1echo > > $LogFile 2 > & 1echo "- The backup starts--" > > $LogFile echo > > $LogFile 2 > & 1echo "Start Backup $DATAPath $(date +"% y-%m-%d% H:%M:%S ")" > > $LogFile 2 > & 1echo > > $LogFile 2 > & 1echo "- -The backup starts-- "> > $LogFile 2 > & 1echo > > $LogFile 2 > & 1echo > > $LogFile 2 > & 1} # end of log end () {echo > > $LogFile 2 > & 1echo > > $LogFile 2 > & 1echo"-The backup ends- -"> > $LogFile 2 > & 1echo > > $LogFile 2 > & 1echo" $DATAPath backup ends $(date + "% y-%m-%d% H:%M:%S") "> > $LogFile 2 > & 1echo > > $LogFile 2 > & 1echo"-The backup ends-"> > $LogFile 2 > & 1} # full backup FullBackup () {echo "Today is week: `date +% a` Start performing a full backups... "> > $LogFile 2 > & 1echo > > $LogFile 2 > & 1echo > > $LogFile 2 > & 1if [- d $OldFirstFile] Then rm-rf $OldFirstFile > > $LogFile 2 > & 1 echo "[$OldFirstFile] Delete Old File Success! `date`" > $LogFile 2 > & 1 [!-d $FirstFile] & & mkdir-pv $FirstFile > > $LogFile 2 > & 1cd $DATAPathfor File in $DirFile; docp-a ${File} $FirstFile > > $LogFile 2 > & 1if [$?-eq 0]; thenecho "[$DATAPath} ${File}] Backup Success! `date`" > $LogFile 2 > & 1 fidoneelseif [!-d $FirstFile]; thenmkdir-pv $FirstFile > $LogFile 2 > & 1cd $DATAPathfor File in $DirFile Docp-a ${File} $FirstFile > > $LogFile 2 > & 1if [$?-eq 0]; thenecho "[${DATAPath} ${File}] Backup Success! `date`" > $LogFile 2 > & 1 fidoneelseecho "[$DATAPath] The Backup File is exists,Can't Backup! `date`" > > $LogFile 2 > & 1 fifi} # differential backup DiffBackup () {echo "Today is week: `date +% a`, Start performing differential backups..." > > $LogFile 2 > & 1echo > > $LogFile 2 > & 1echo > > $LogFile 2 > & 1if [- d $OldFirstFile] Then rm-rf $OldFirstFile > > $LogFile 2 > & 1 echo "[$OldFirstFile] Delete Old File Success! `date` > > $LogFile 2 > & 1 [!-d $FirstFile] & & mkdir-pv $FirstFile > > $LogFile 2 > & & 1 rsync-- dry-run-racnC-- out-format="% n "$DATAPath $d_dir | grep-v" / $"| xargs-I {} rsync-R-a-z ${DATAPath} /. / {} $FirstFileif [$?-eq 0] Thenecho "[${DATAPath} ${File}] Backup Success! `date`" > $LogFile 2 > & 1 fielse if [!-d $FirstFile]; then mkdir-pv $FirstFile > > $LogFile 2 > & 1 rsync-- dry-run-racnC-- out-format= "n" $DATAPath $d_dir | grep-v "/ $" | xargs-I {} rsync-R-a-z ${DATAPath} /. / {} $FirstFileif [$?-eq 0] Thenecho "[${DATAPath} ${File}] Backup Success! `date`" > $LogFile 2 > & 1 fi else echo "[${DATAPath}] The Backup File is exists,Can't Backup! `date`" > > $LogFile 2 > & 1 fifi} # Head# determines whether to mount / / ds-bk01/drv-y at the beginning of the log, and if not, mount it If the mount is not successful, exit the script and record it to the log file mount | grep $MountPath > > $LogFile 2 > & 1if [$?-eq 0] Then echo "[$Host] has successfully installed backup server shared directories: [$MountPath] to local: [$BackupPath]..." > > $LogFile 2 > & 1 echo > > $LogFile 2 > & 1else echo "[$Host] does not mount a backup server shared directory: [$MountPath]..." > $LogFile 2 > & 1 mount.cifs-o username=$username,password=$password,dir_mode=0777,file_mode=0777$ MountPath $BackupPath if [$?-eq 0] Then echo "Mount successfully!" > $LogFile 2 > & 1 else echo "[$Host] failed to mount backup server shared directory: [$MountPath] to local: [$BackupPath]..." > > $LogFile 2 > & 1 echo "Mount again, please!" > > $LogFile 2 > & 1 echo "The shell script has exited..." > $LogFile 2 > & 1end mail-s "From [$Host] backup information [$Time]" $Mail_Group
< $LogFile exit 8 fifi# 备份策略的变量a=`date +%u`# 定义Today是星期几b=7 # 定义多少天循环一次完全备份和差异备份,这里定义为7天循环一次,如果要增大循环天数,请修改此处c=5# 这里5代表是周五完全备份,如果要改成周六完全备份,请修改为6,注意:c与d的值不能相同d=6# 定义每周6不备份数据,注意:c与d的值不能相同# 注意,下面的语句很重,判断Today是否大于$c,定义变量ago将采用不同的表达式计算if [ "$a" -gt "$c" ]; then ago=$[ $a - $c ]# 定义Today离$c有多少天else ago=$[ $a + $b - $c ] # 定义Today离$c有多少天fid_dir=${BackupPath}${Host}_$(date +%Y-%m-%d -d "`echo $ago` days ago")/`basename ${DATAPath}`/ # 获取完全备份数据后的目录路径#date +%Y-%m-%d -d "`echo $ago` days ago" # 定义备份策略if [ "$a" -eq "$c" ]; thenFullBackupelif [ "$a" -eq "$d" ]; thenecho "Today is Saturday 6, according to the backup strategy, today no data backup." >> $LogFile 2 > & the end of 1elseDiffBackupfi# log after the end# backup is completed, send an email to the specified mailbox mail-s "From [$Host] backup information [$Time]" $Mail_Group < $LogFileexit 0