首页 > 代码库 > 完全备份数据和差异备份数据的shell脚本
完全备份数据和差异备份数据的shell脚本
#!/bin/bash # # Description: 该脚本功能,以周为单位循环;周五实行完全备份,周六不备份,周日至周四实行差异备份; # 因工作需要,不实行压缩打包的方式,采用cp完全备份,rsync差异备份; # 若要使用该脚本,请直接修改下面的两个参数:BackupPath和DATAPath,其他地方请不要修改; # DATE: 2017-04-24 22:43:00 # MODIFY DATE: 2017-04-28 10:00:00 # VERSION: 1.1 # 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/ # 指定备份路径 DATAPath=/data/ac-cnc/ # 需要备份的目录 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天前的备份目录 # 记录日志文件的开头部分 Head() { [ ! -d $LogDir ] && mkdir $LogDir 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 } # 完全备份 FullBackup(){ echo "Today is week: `date +%a`, Start performing a full backups..." 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..." 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 } # 日志的结束部分 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 } a=`date +%u` # 定义Today是星期几 b=7 # 定义多少天循环一次完全备份和差异备份,这里定义为7点循环一次,如果要增大循环天数,请修改此处 c=5 # 这里5代表是周五完全备份,如果要改成周六完全备份,请修改为6 d=6 # 定义每周6不备份数据 ago=$[ $a + $b -$c ] # 定义Today离$c有多少天 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 Head FullBackup end elif [ "$a" -eq "$d" ]; then Head echo "Today is Saturday 6, according to the backup strategy, today no data backup." >> $LogFile 2>&1 end else Head DiffBackup end fi # 备份完成后发送邮件至指定邮箱 Mail_Group="username1@test.com username2@test.com" mail -s "From $Host backup information" $Mail_Group < $LogFile exit 0
本文出自 “ccschan” 博客,转载请与作者联系!
完全备份数据和差异备份数据的shell脚本
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。