完全备份数据和差异备份数据的shell脚本
#!/bin/bash # # Description: 该脚本功能,以周为单位循环;周五实行完全备份,周六不备份,周日至周四实行差异备份; # 因工作需要,不实行压缩打包的方式,采用cp完全备份,rsync差异备份; # 若要使用该脚本,请直接修改下面的两个参数:BackupPath和DATAPath,其他地方请不要修改; # DATE: 2017-04-24 22:43:00 # MODIFY DATE: 2017-08-28 17:20:00 # VERSION: 2.0 # Author: xixi_chan # 导入PATH环境变量 export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/bin:/sbin:/root/bin # 重新定义IFS变量避免目录或文件名存在空格时被分割 IFS=`echo -en "\n\b"` # 定义全局变量 BackupPath=/mnt/backup_data/# 指定备份路径 MountPath=//ds-bk01/drv-y# 指定windwos备份服务器共享路径 DATAPath=/data/ # 需要备份的目录 Time=`date +%Y-%m-%d`# 定义Today的时间变量 Host=`echo $HOSTNAME | awk -F'.' '{print $1}'`# 定义主机名 LogFile="/var/log/${Host}/${Host}_${Time}.log" # 定义日志文件名称 LogDir=`echo "$LogFile" | sed -r 's@[^/]+/?$@@g'`# 定义日志文件所在的目录 DirFile=`ls $DATAPath`# 将备份路径下的文件及目录保存至变量DirFile中 FirstFile="${BackupPath}${Host}_${Time}/`basename ${DATAPath}`/" # 根据时间定义当前备份的目录 OldFirstFile="${BackupPath}${Host}_$(date +%Y-%m-%d --date='7 days ago')"# 定义7天前的备份目录 Mail_Group="test@aaa.com" # 定义收件人 username=user # 定义挂载备份服务器的用户名 password=password # 定义挂载备份服务器的密码 # 记录日志文件的开头部分 Head() { [ ! -d $LogDir ] && mkdir $LogDir echo "[ 日文 ] このメッセ?ジはAB Linuxサ?バを介して、このメッセ?ジを介してs-smb01サ?バのデ?タバックアップを??するために送信されます。" > $LogFile echo "[ 中文 ] 这封邮件是通过AB的linux服务器发出来的,通过这封邮件来确认s-smb01服务器的数据备份情况!" >> $LogFile 2>&1 echo >> $LogFile 2>&1 echo "---------------------The backup starts----------------------" >> $LogFile echo >> $LogFile 2>&1 echo "Start Backup $DATAPath $(date +"%y-%m-%d %H:%M:%S")" >> $LogFile 2>&1 echo >> $LogFile 2>&1 echo "---------------------The backup starts----------------------" >> $LogFile 2>&1 echo >> $LogFile 2>&1 echo >> $LogFile 2>&1 } # 日志的结束部分 end(){ echo >> $LogFile 2>&1 echo >> $LogFile 2>&1 echo "---------------------The backup ends ----------------------" >> $LogFile 2>&1 echo >> $LogFile 2>&1 echo " $DATAPath backup ends $(date +"%y-%m-%d %H:%M:%S")" >> $LogFile 2>&1 echo >> $LogFile 2>&1 echo "---------------------The backup ends ----------------------" >> $LogFile 2>&1 } # 完全备份 FullBackup(){ echo "Today is week: `date +%a`, Start performing a full backups..." >> $LogFile 2>&1 echo >> $LogFile 2>&1 echo >> $LogFile 2>&1 if [ -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 cd $DATAPath for File in $DirFile; do cp -a ${File} $FirstFile >> $LogFile 2>&1 if [ $? -eq 0 ];then echo "[ ${DATAPath}${File} ] Backup Success!`date`" >> $LogFile 2>&1 fi done else if [ ! -d $FirstFile ];then mkdir -pv $FirstFile >> $LogFile 2>&1 cd $DATAPath for File in $DirFile; do cp -a ${File} $FirstFile >> $LogFile 2>&1 if [ $? -eq 0 ];then echo "[ ${DATAPath}${File} ] Backup Success!`date`" >> $LogFile 2>&1 fi done else echo "[ $DATAPath ]The Backup File is exists,Can't Backup!`date`" >> $LogFile 2>&1 fi fi } # 差异备份 DiffBackup() { echo "Today is week: `date +%a`, Start performing differential backups..." >> $LogFile 2>&1 echo >> $LogFile 2>&1 echo >> $LogFile 2>&1 if [ -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}/./{} $FirstFile if [ $? -eq 0 ];then echo "[ ${DATAPath}${File} ] Backup Success!`date`" >> $LogFile 2>&1 fi else 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}/./{} $FirstFile if [ $? -eq 0 ];then echo "[ ${DATAPath}${File} ] Backup Success!`date`" >> $LogFile 2>&1 fi else echo "[ ${DATAPath} ]The Backup File is exists,Can't Backup! `date`" >> $LogFile 2>&1 fi fi } # 日志开始部分 Head # 判断是否挂载//ds-bk01/drv-y,如果没有则挂载,如果挂载不成功则退出脚本,并记录至日志文件 mount | grep $MountPath >> $LogFile 2>&1 if [ $? -eq 0 ];then echo "[ $Host ] has successfully installed backup server shared directories: [ $MountPath ] to local: [ $BackupPath ] ... " >> $LogFile 2>&1 echo >> $LogFile 2>&1 else 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>&1 end mail -s "From [ $Host ] backup information [ $Time ]" $Mail_Group < $LogFile exit 8 fi fi # 备份策略的变量 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有多少天 fi d_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" ]; then FullBackup elif [ "$a" -eq "$d" ]; then echo "Today is Saturday 6, according to the backup strategy, today no data backup." >> $LogFile 2>&1 else DiffBackup fi # 日志结束部分 end # 备份完成后发送邮件至指定邮箱 mail -s "From [ $Host ] backup information [ $Time ]" $Mail_Group < $LogFile exit 0
文章名称:完全备份数据和差异备份数据的shell脚本
标题URL:http://azwzsj.com/article/jhgcdc.html