一、全量备份
环境:一台主SVN,一台备SVN(主要提供备份功能),后续可通过钩子脚本进行实时备份,后续发给大家。
工作原理:通过svn的hotcopy命令过行热备份,并进行一系列的检查,备份后通过rsync推送到备份机上。
脚本如下:
=======
#!/bin/bash
#Version: V2
#Date: 2015-02-03
#Author: wang
CONFDIR=/usr/local/httpd
BASEDIR=/home/xxx/scripts/svn_full_bak
SVNCMD=/usr/local/subversion/bin/svnadmin
SVNDIR=/data/svn
BAKDIR=$BASEDIR/full_bak_dir
SCDIR=/home/xxx/scripts
DISK=`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.log
echo -e "\n" >>$BASEDIR/logs/Info.log
echo "######################### Backup Start in Time: $(date +%F-%T)#########################" >>$BASEDIR/logs/Info.log
echo "## SVN周备开始 Time:$(date +%F-%T) ##" >>/home/xxx/scripts/svn_full_bak/logs/linshi.log
##########Begin Loop backup SVN Repository##########
while read repo other
do
[ $DISK -lt 30 ] && {
# echo "Warning: Disk free less then 30G,svn backup failed (Time: $(date +%F-%T) ==>repo:$repo)"|mailx -s "Disk Free Check" [email protected]
echo "Warning: Disk free less then 30G,svn backup failed (Time: $(date +%F-%T) ==>repo:$repo)" >>$BASEDIR/logs/Info.log
echo "######################### Backup Stop in Time: $(date +%F-%T)#########################" >>$BASEDIR/logs/Info.log
echo "Warning: Disk free less then 30G,svn backup failed (Time: $(date +%F-%T) ==>repo:$repo)" >>/home/xxx/scripts/svn_full_bak/logs/linshi.log
echo "## SVN周备结束 Time:$(date +%F-%T) ##" >>/home/xxx/scripts/svn_full_bak/logs/linshi.log
mailx -s "SVN 周日全备" [email protected] < /home/xxx_a
dmin/scripts/svn_full_bak/logs/linshi.log
exit 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.log
continue
}
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.log
echo "$repo Local is Failed" >>/home/xxx/scripts/svn_full_bak/logs/linshi.log
clear_bak_dir
continue
}
############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.log
echo "$repo is Failed" >>/home/xxx/scripts/svn_full_bak/logs/linshi.log
continue
}||{
echo "remote backup ..OK. in Time: $(date +%F-%T) ==>repo:$repo" >>$BASEDIR/logs/Info.log
echo "$repo is OK" >>/home/xxx/scripts/svn_full_bak/logs/linshi.log
}
clear_bak_dir
sleep 3
done < /home/xxx/scripts/svn_full_bak/repository.txt
###########Begin back svn conf directory##############
while read conf_dir rename other
do
[ ! -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.log
echo "${conf_dir} is OK" >>/home/xxx/scripts/svn_full_bak/logs/linshi.log
echo "####### remote backup .OK.. --**${conf_dir}**--#######"
}||{
echo "remote backup ${conf_dir} failed in Time: $(date +%F-%T)" >>$BASEDIR/logs/Info.log
echo "${conf_dir} is Failed" >>/home/xxx/scripts/svn_full_bak/logs/linshi.log
echo "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.log
echo "## SVN周备结束 Time:$(date +%F-%T) ##" >>/home/xxx/scripts/svn_full_bak/logs/linshi.log
mailx -s "SVN 周日全备" [email protected] < /home/xxx/scripts/svn_full_bak/logs/linshi.log
二、增量备份
环境:一台主SVN,一台备SVN(主要提供备份功能),后续可通过钩子脚本进行实时备份,后续发给大家。
工作原理:
1、备机上通过nfs挂载到主机上,主要实现通过svnlook youngest来查看主和备间各版本库的版本号差别。(挂载后相当于在本地一样,就可以使用youngest参数或取到主和备版本号的区别别,并进行打包。)
2、挂载并打包后,将打包后的文件存放到挂载目录中,在到备机上通过还原脚本分别对更新的版本库进行还原,并将结果通过邮件发给告知人。
脚本如下:
======
主SVN上备份脚本:
#!/bin/bash
CMD1=/usr/local/subversion/bin/svnlook
CMD2=/usr/local/subversion/bin/svnadmin
SCDIR=/home/xxx/scripts/svn_incre_bak
BAKDIR=$SCDIR/incre_bak_dir
LOGS=/home/xxx/scripts/svn_incre_bak/logs
SVNDIR=/data/svn
SVNBAK=/svnbak
rm -f /svnbak/linshi.log
echo -e "\n" >>$LOGS/Info.log
echo "==================> Incremental Start in Time: $(date +%F-%T) <=========================" >>$LOGS/Info.log
echo "##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 /svnbak
RET=$?
}||{
RET=0
}
times=0
while true
do
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.log
echo "==================> Incremental Stop(failed:umount) in Time: $(date +%F-%T) <============" >>$LOGS/Info.loged.log
echo "mount xxx is Failed Time: $(date +%F-%T)" >>/svnbak/linshi.log
echo "##SVN 增量备份结束 Time:$(date +%F-%T)##" >>/svnbak/linshi.log
exit 1
}
done
##################################Begin backup################################################
rm -rf $SVNBAK/tmp/*
while read repo
do
[ ! -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.log
echo "$repo dump is Failed" >>/svnbak/linshi.log
continue
}||{
echo "$repo dump is OK" >>$LOGS/Info.log
echo "$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.log
echo "$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
fi
done < $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.log
echo "##SVN 增量备份结束 Time:$(date +%F-%T)##" >>/svnbak/linshi.log
备SVN上还原脚本:
#!/bin/bash
echo -e "\n" >>/root/scripts/svn_incre_restore/logs/Info.log
echo -e "\n" >>/data/svn/linshi.log
echo "##SVN 增量还原开始 Time:$(date +%F-%T)##" >>/data/svn/linshi.log
echo "####################### 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.log
echo "All repository is Neweast" >>/data/svn/linshi.log
echo "##SVN 增量还原结束 Time:$(date +%F-%T)##" >>/data/svn/linshi.log
mailx -s "SVN 每日增量备份及还原" [email protected] </data/svn/linshi.log
#rm -f /data/svn/linshi.log
exit 0
}
while read repo
do
[ ! -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.log
echo "$repo recovery is OK" >>/data/svn/linshi.log
}||{
echo "$repo recovery failed" >>/root/scripts/svn_incre_restore/logs/Info.log
echo "$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.log
echo "##SVN 增量还原结束 Time:$(date +%F-%T)##" >>/data/svn/linshi.log
mailx -s "SVN 每日增量备份及还原" [email protected] </data/svn/linshi.log
#rm -f /data/svn/linshi.log