一、全量备份
环境:一台主SVN,一台备SVN(主要提供备份功能),后续可通过钩子脚本进行实时备份,后续发给大家。
工作原理:通过svn的hotcopy命令过行热备份,并进行一系列的检查,备份后通过rsync推送到备份机上。
脚本如下:
=======
#!/bin/bash#Version: V2
#Date: 2015-02-03
#Author: wang
CONFDIR=/usr/local/httpdBASEDIR=/home/xxx/scripts/svn_full_bakSVNCMD=/usr/local/subversion/bin/svnadminSVNDIR=/data/svnBAKDIR=$BASEDIR/full_bak_dirSCDIR=/home/xxx/scriptsDISK=`df -h |sed -n '2p'|awk '{PRint $4}'|tr G " "|awk '{print $1}'`
######define function########clear_bak_dir(){sleep 2 ${BAKDIR:=/home/xxx/scripts/svn_full_bak/full_bak_dir} &>/dev/null rm -rf $BAKDIR/${repo}.bak}
rm -f /home/xxx/scripts/svn_full_bak/logs/linshi.logecho -e "/n" >>$BASEDIR/logs/Info.logecho "######################### Backup Start in Time: $(date +%F-%T)#########################" >>$BASEDIR/logs/Info.logecho "## SVN周备开始 Time:$(date +%F-%T) ##" >>/home/xxx/scripts/svn_full_bak/logs/linshi.log
##########Begin Loop backup SVN Repository##########while read repo otherdo[ $DISK -lt 30 ] && {# echo "Warning: Disk free less then 30G,svn backup failed (Time: $(date +%F-%T) ==>repo:$repo)"|mailx -s "Disk Free Check" wangbogui@xxx.comecho "Warning: Disk free less then 30G,svn backup failed (Time: $(date +%F-%T) ==>repo:$repo)" >>$BASEDIR/logs/Info.logecho "######################### Backup Stop in Time: $(date +%F-%T)#########################" >>$BASEDIR/logs/Info.logecho "Warning: Disk free less then 30G,svn backup failed (Time: $(date +%F-%T) ==>repo:$repo)" >>/home/xxx/scripts/svn_full_bak/logs/linshi.logecho "## SVN周备结束 Time:$(date +%F-%T) ##" >>/home/xxx/scripts/svn_full_bak/logs/linshi.loGmailx -s "SVN 周日全备" wangbogui@xxx.com < /home/xxx_admin/scripts/svn_full_bak/logs/linshi.logexit 1}${BAKDIR:=/home/xxx/scripts/svn_full_bak/full_bak_dir} &>/dev/null[ ! -d $BAKDIR ] && mkdir -p $BAKDIR/bin/chown -R xxx $BAKDIR[ -d $BAKDIR/${repo}.bak ] && {rm -rf $BAKDIR/${repo}.bak}[ ! -z "$other" ] && continue[ ! -d $SVNDIR/$repo ] && {echo "---- $repo repository ---- not exist ..."echo "---- $repo repository ---- not exist ..." >>$BASEDIR/logs/Info.logcontinue}
echo " local backup start in Time: $(date +%F-%T) ==>repo:$repo" >>$BASEDIR/logs/Info.log$SVNCMD hotcopy $SVNDIR/$repo $BAKDIR/${repo}.bak[ $? -ne 0 ] && {echo " local backup failed in Time: $(date +%F-%T) ==>repo:$repo" >>$BASEDIR/logs/Info.logecho "$repo Local is Failed" >>/home/xxx/scripts/svn_full_bak/logs/linshi.logclear_bak_dircontinue}
############Begin remote back##############
/usr/bin/rsync -avz $BAKDIR/${repo}.bak xxx_web@xxx::${repo}/${repo}_$(date +%F) --passWord-file=/etc/rsyncd.passwd
[ $? -ne 0 ] && {echo "remote backup failed in Time: $(date +%F-%T) ==>repo:$repo" >>$BASEDIR/logs/Info.logecho "$repo is Failed" >>/home/xxx/scripts/svn_full_bak/logs/linshi.logcontinue}||{echo "remote backup ..OK. in Time: $(date +%F-%T) ==>repo:$repo" >>$BASEDIR/logs/Info.logecho "$repo is OK" >>/home/xxx/scripts/svn_full_bak/logs/linshi.log}clear_bak_dirsleep 3done < /home/xxx/scripts/svn_full_bak/repository.txt
###########Begin back svn conf directory##############
while read conf_dir rename otherdo[ ! -z $conf_dir -a -d $SVNDIR/$conf_dir ] && continue
[ -z $rename ] && continue[ ! -z "$other" ] && {echo "---- $conf_dir ---- Invalid format,please define Two parameter"continue}
[ ! -d $conf_dir ] && {echo "---- $conf_dir conf_dir ---- not exist ..."continue}echo "####### remote start backup --**${conf_dir}**--#######"/usr/bin/rsync -avz ${conf_dir} xxx_web@xxx::bakdir/${rename}_$(date +%F) --password-file=/etc/rsyncd.passwd &>/dev/null[ $? -eq 0 ] && {echo "remote backup ${conf_dir} ..OK. in Time: $(date +%F-%T)" >>$BASEDIR/logs/Info.logecho "${conf_dir} is OK" >>/home/xxx/scripts/svn_full_bak/logs/linshi.logecho "####### remote backup .OK.. --**${conf_dir}**--#######"}||{echo "remote backup ${conf_dir} failed in Time: $(date +%F-%T)" >>$BASEDIR/logs/Info.logecho "${conf_dir} is Failed" >>/home/xxx/scripts/svn_full_bak/logs/linshi.logecho "remote backup ${conf_dir} is failed.."}done < /home/xxx/scripts/svn_full_bak/repository.txt
echo "######################### Backup Complete in Time: $(date +%F-%T)#########################" >>$BASEDIR/logs/Info.logecho "## SVN周备结束 Time:$(date +%F-%T) ##" >>/home/xxx/scripts/svn_full_bak/logs/linshi.log
mailx -s "SVN 周日全备" wangbogui@xxx.com< /home/xxx/scripts/svn_full_bak/logs/linshi.log
二、增量备份
环境:一台主SVN,一台备SVN(主要提供备份功能),后续可通过钩子脚本进行实时备份,后续发给大家。
工作原理:
1、备机上通过nfs挂载到主机上,主要实现通过svnlook youngest来查看主和备间各版本库的版本号差别。(挂载后相当于在本地一样,就可以使用youngest参数或取到主和备版本号的区别别,并进行打包。)
2、挂载并打包后,将打包后的文件存放到挂载目录中,在到备机上通过还原脚本分别对更新的版本库进行还原,并将结果通过邮件发给告知人。
脚本如下:
======
主SVN上备份脚本:
#!/bin/bashCMD1=/usr/local/subversion/bin/svnlookCMD2=/usr/local/subversion/bin/svnadminSCDIR=/home/xxx/scripts/svn_incre_bakBAKDIR=$SCDIR/incre_bak_dirLOGS=/home/xxx/scripts/svn_incre_bak/logsSVNDIR=/data/svnSVNBAK=/svnbak
rm -f /svnbak/linshi.logecho -e "/n" >>$LOGS/Info.logecho "==================> Incremental Start in Time: $(date +%F-%T) <=========================" >>$LOGS/Info.logecho "##SVN 增量备份开始 Time:$(date +%F-%T)##" >>/svnbak/linshi.log#######check mount###############################count=`ls /svnbak |wc -l`/bin/mount |/bin/grep xxx[ $? -ne 0 -o $count -lt 1 ] && {/bin/mount -t nfs xxx:/svn/data /svnbakRET=$?}||{RET=0}times=0while truedo if [ $RET -ne 0 ] then /bin/umount -lf /svnbak /bin/mount -t nfs xxx:/data/svn/ /svnbak count=`ls /svnbak |wc -l` /bin/mount |grep xxx [ $? -ne 0 -o $count -lt 1 ] && { RET=$? }||{ RET=0 } sleep 1 let times++ else break fi [ $times -eq 10 ]&&{ echo "mount xxx failed in Time: $(date +%F-%T)" >>$LOGS/logs/Info.logecho "==================> Incremental Stop(failed:umount) in Time: $(date +%F-%T) <============" >>$LOGS/Info.loged.logecho "mount xxx is Failed Time: $(date +%F-%T)" >>/svnbak/linshi.logecho "##SVN 增量备份结束 Time:$(date +%F-%T)##" >>/svnbak/linshi.log exit 1 }done
##################################Begin backup################################################rm -rf $SVNBAK/tmp/*while read repodo [ ! -d $SVNBAK/$repo ] && { echo "$repo not exist" >>$LOGS/Info.log sleep 1 continue } V_NEW=`$CMD1 youngest $SVNDIR/$repo` V_OLD=`$CMD1 youngest $SVNBAK/$repo` V_OLD_1=$((${V_OLD}+1))
if [ $V_OLD -lt $V_NEW ]then sleep 1 $CMD2 dump --incremental -r ${V_OLD_1}:${V_NEW} $SVNDIR/$repo >$BAKDIR/${repo}_${V_OLD_1}:${V_NEW} [ $? -ne 0 ] && { echo "bakcup $repo dump failed" >>$LOGS/Info.logecho "$repo dump is Failed" >>/svnbak/linshi.log continue }||{ echo "$repo dump is OK" >>$LOGS/Info.logecho "$repo dump is OK" >>/svnbak/linshi.log /bin/mkdir $SVNBAK/tmp/$repo -p sleep 1 /cp -r $BAKDIR/${repo}_${V_OLD_1}:${V_NEW} $SVNBAK/tmp/$repo/} [ $? -eq 0 ] && {# echo "$repo" >>$SCDIR/repository_remote.txt /bin/rm -rf $BAKDIR/${repo}_${V_OLD_1}:${V_NEW} }||{ echo "$repo copy to /svnbak/tmp is faild" >>$LOGS/Info.logecho "$repo copy to /svnbak/tmp is faild" >>/svnbak/linshi.log } else echo "$repo version is newest" >>$LOGS/Info.log echo "$repo version is newest" >>/svnbak/linshi.log sleep 1 continue fidone < $SCDIR/repository.txt
###################rsync repository.txt to xxx host#########################[ ! -f $SCDIR/repository_remote.txt ] && {#echo "==================> Incremental Stop(Version:newest) in Time: $(date +%F-%T) <===========" >>$LOGS/Info.log#exit 0#}#sleep 1#/usr/bin/rsync -avrz --delete $SCDIR/repository_remote.txt xxx_web@xxx::repolist/repository.txt --password-file=/etc/rsyncd.passwd#[ $? -ne 0 ] && {#echo "repository_remote.txt transful failed Time: $(date +%F-%T)" >>$LOGS/Info.log#}||{#/cp $SCDIR/repository_remote.txt $SCDIR/repository_remote.txt.bak#echo "repository_remote.txt transful Successful Time: $(date +%F-%T)" >>$LOGS/Info.log#/bin/rm -rf $SCDIR/repository_remote.txt#}
echo "==================> Incremental Stop(Status:Complete) in Time: $(date +%F-%T) <==========" >>$LOGS/Info.logecho "##SVN 增量备份结束 Time:$(date +%F-%T)##" >>/svnbak/linshi.log
备SVN上还原脚本:
#!/bin/bashecho -e "/n" >>/root/scripts/svn_incre_restore/logs/Info.logecho -e "/n" >>/data/svn/linshi.logecho "##SVN 增量还原开始 Time:$(date +%F-%T)##" >>/data/svn/linshi.logecho "####################### Recovery Start in Time: $(date +%F-%T) ######################" >>/root/scripts/svn_incre_restore/logs/Info.log [ ! -d /data/svn/tmp/* ] && {echo "No Repository Will Be Recovery ..." >>/root/scripts/svn_incre_restore/logs/Info.log echo "####################### Recovery Complete in Time: $(date +%F-%T) ######################" >>/root/scripts/svn_incre_restore/logs/Info.logecho "All repository is Neweast" >>/data/svn/linshi.logecho "##SVN 增量还原结束 Time:$(date +%F-%T)##" >>/data/svn/linshi.logmailx -s "SVN 每日增量备份及还原" wangbogui@xxx.com</data/svn/linshi.log#rm -f /data/svn/linshi.logexit 0}
while read repodo [ ! -d /data/svn/tmp/$repo ] && continue /usr/local/subversion/bin/svnadmin load /data/svn/$repo < `ls /data/svn/tmp/$repo/*` V_NEW=`ls /data/svn/tmp/$repo/*|awk -F ":" '{print $2}'` V_OLD=`/usr/local/subversion/bin/svnlook youngest /data/svn/$repo` [ $V_NEW -eq $V_OLD ] && { /bin/rm -rf /data/svn/tmp/$repo echo "$repo recovery ..OK.." >>/root/scripts/svn_incre_restore/logs/Info.logecho "$repo recovery is OK" >>/data/svn/linshi.log }||{ echo "$repo recovery failed" >>/root/scripts/svn_incre_restore/logs/Info.logecho "$repo recovery is Failed" >>/data/svn/linshi.log }done </root/scripts/svn_incre_restore/repository.txt
sleep 1/bin/chown -R daemon /data/svn
echo "####################### Recovery Complete in Time: $(date +%F-%T) ######################" >>/root/scripts/svn_incre_restore/logs/Info.logecho "##SVN 增量还原结束 Time:$(date +%F-%T)##" >>/data/svn/linshi.logmailx -s "SVN 每日增量备份及还原" wangbogui@xxx.com</data/svn/linshi.log#rm -f /data/svn/linshi.log
新闻热点
疑难解答