SVN全量备份和增量备份脚本

时间:2021-12-19 08:48:01

Linux SVN 全备和增量备份脚本_Cisco技术 无忧网客联盟http://www.net527.cn/a/caozuoxitong/Linux/2011/1008/20014.html

SVN备份需求分析

完整备份
==============
1.备份时在/svn_backup目录下新建 "Full+当天的日期" 为名字的目录,备份机是一个Windows机器(SVN服务器空间不足),有个共享文件夹(svnbackup)有读写权限。
  把这个svnbackup挂载到SVN服务器/svn_backup目录下,这时我们备份的时候就备份到我们Windows机器上了。
2.把备份的日志写到/svn_backup/full_backup_svn.log里去,同时要把备份时的
版本号也写进去。
3.在/svn_backup目录下新建full_repo_number目录,然后把每次备份时的每个版本库的版本号单独写一个文件
  方便增量备份时用,名字的格式:版本库的名字.number,每次备份时都对上次的版本进行覆盖(用>,而不是用>>,就是要覆盖以前的版本).

完整备份逻辑:
1.首先判断/svn_backup是否存在,如果不存在,就创建
2.判断备份机器是否是开机状态,如果没有退出备份程序,判断共享文件夹是否挂载到SVN服务器上,如果没有挂载上,重新挂载
3.然后检查当天的备份目录是否存在(是以日期为名),如果不存在就创建,如果已经存在,那就写“备份目录已经存在”写到完整备份日志(full_backup.log)里。
4.获得每个版本库的名字
5.然后判断存放每个版本库版本号的目录(full_repo_number)是否存在,不存在就创建,这个目录是为增量备份做准备,把每个版本库的版本号单独写一个文件,(每个文件对应一个版本库,一个文件内只有一个版本号,名字就是版本库的名字.number)
6.循环开始备份。
7.生成备份日志

备份日志像这样:
====================2011-09-16========================
23:16:36==> [ledge]:17433 Successfull Full Backup !!!
23:18:22==> [arch]:361 Successfull Full Backup !!!
23:19:46==> [core]:2140 Successfull Full Backup !!!

增量备份
==============
1.备份时在/svn_backup目录下新建 "Increment+当天日期" 为名字的目录
2.每次备份时都取到最新的版本号,写入到当天备份目录下的repo_version_number文件中。
3.把备份日志写到/svn_backup/increment_backup_svn.log日志文件里去,同时也是把版本号写进去
4.增量备份时都从上次完整备份的版本号开始到当天的版本号,而不是从上次增量备份时的版本号到当天备份时的版
  本号。


增量备份逻辑:
1.首先判断/svn_backup目录是否存在,如果不存在,创建
2.首先判断当天的备份目录存不存在,如果不存在就创建一个,如果存在写一个日志到日志文件中说当天备份目录已经存在,然后就退出。
3.然后判断上次的full_repo_number是否存在,如果存在,就开始备份:
a.依次取出版本库的名字,然后再取出版本最新的版本。
b.读取full_repo_number目录中的对应版本库文件的版本号(每个文件对应一个版本库,一个文件内只有一个版本号)。
c.利用备份语句进行备份
d.备份成功,把上次的版本号和本次的版本号写入到日志文件中,加上时间戳。
4.如果full_repo_number目录不存在,就创建该目录,然后依次把本次的版本号写入到该目录下对应的单独的文件。
5.生成备份日志

备份日志像这样:
=============2011-09-24_23:00:01==============
[ledge] version(from 17443 to 17447) backup successful!
[arch] version(from 389 to 414) backup successful!
[core] version(from 2140 to 2145) backup successful!

完整备份
=================
#!/bin/bash
#This scripts for full bakcup svn repository!
# DATE:2011/9/23 AUTHOR:Leo EMAIL:411765588@qq.com

#Define Variable
SVN="/usr/local/svn"
SVN_PRO="/usr/local/svn/bin/svnadmin hotcopy"
SVN_LOOK="/usr/local/svn/bin/svnlook youngest"
SVN_ROOT=/svn
SVN_BACKUP=/svn_backup
BACKUP_DATE=$(date +%Y-%m-%d)

#check up backup root directory if exist !
if [ ! -e /svn_backup ]
then
mkdir /svn_backup
fi

#check mount point if not mount
IP="192.168.1.26"
ping -c 1 -w 1 $IP &>/dev/null
if [ $? = 0 ]
then
        df -k | grep $IP &>/dev/null || mount -t cifs //$IP/svnbackup $SVN_BACKUP -o password=123456
else
echo "$(date +%Y-%m-%d-%T)==>Backup Computer[$IP] already shutdown!" >>$SVN/backup_error.log
echo " " >>$SVN/backup_error.log
        exit 1
fi

#Get repo directory name
REPO_NAME_LIST=$(ls $SVN_ROOT)

#define logfile
LOGFILE=$SVN_BACKUP/full_backup.log

#output a backup date to log file
echo "====================$BACKUP_DATE========================" >>$LOGFILE

#check up backup directory if exist then exit,if not exist mkdir
if [ -e $SVN_BACKUP/Full_"$BACKUP_DATE" ]
then
echo "$(date +%T)==>$SVN_BACKUP/Full_"$BACKUP_DATE" already exists!" >>$LOGFILE
echo "Thers is no need backup again!" >>$LOGFILE
exit 1
else
mkdir $SVN_BACKUP/Full_"$BACKUP_DATE"
fi

#mkdir full_repo_number if not exsits!
#full_repo_number purpose is leave with repo number single file for each repo
if [ ! -e $SVN_BACKUP/full_repo_number ]
then
mkdir $SVN_BACKUP/full_repo_number
fi

#define variables for full_repo_numher
FULL_REPO_VERSION=$SVN_BACKUP/full_repo_number

#start backup
for repo_name in $REPO_NAME_LIST
do
#get repo version
get_repo_version=$($SVN_LOOK $SVN_ROOT/$repo_name)

#full backup
$SVN_PRO $SVN_ROOT/$repo_name $SVN_BACKUP/Full_"$BACKUP_DATE"/$repo_name
        echo "$(date +%T)==> repository:[$repo_name] at version($get_repo_version) Successfull Full Backup !" >>$LOGFILE

#output repo version number to /svn_backup/full_repo_number directory
echo "$get_repo_version" >$FULL_REPO_VERSION/$repo_name.number
done

#output a blank line to log file
echo "" >>$LOGFILE


增量备份
===============
#!/bin/bash

#This scripts for svn increment backup
#DATE:2011/09/23 AUTHOR:Leo EMAIL:411765588@qq.com

#define variables
SVN_ROOT=/svn
BK_DIRC=/svn_backup
BK_DATE=$(date +%Y-%m-%d)
DUMP="/usr/local/svn/bin/svnadmin dump"
LOOK="/usr/local/svn/bin/svnlook youngest"

#get repo name
NAME_LIST=$(ls $SVN_ROOT)

#This Time repo version number
THIS_NUM_FILE=$BK_DIRC/Increment_"$BK_DATE"/repo_version_number

#Last Full Backup Version Number Directory
LAST_FULL_NUM=$BK_DIRC/full_repo_number

#if /svn_backup not exist then create it
if [ ! -e $BK_DIRC ]
then
mkdir $BK_DIRC
fi

#check mount point if not mount
IP="192.168.1.26"
ping -c 1 -w 1 $IP &>/dev/null
if [ $? = 0 ]
then
        df -k | grep $IP &>/dev/null || mount -t cifs //$IP/svnbackup $SVN_BACKUP -o password=123456
else
        echo "$(date +%Y-%m-%d-%T)==>Backup Computer[$IP] already shutdown!" >>$SVN/backup_error.log
        echo " " >>$SVN/backup_error.log
        exit 1
fi

#increment backup Log file
LOGFILE=$BK_DIRC/increment_backup.log

#this time date insert into log file
echo "=============$(date +%Y-%m-%d_%T)==============" >>$LOGFILE

#if this backup directory not exist then create
if [ ! -e $BK_DIRC/Increment_"$BK_DATE" ]
then
mkdir $BK_DIRC/Increment_"$BK_DATE"
else
echo "$BK_DIRC/Increment_"$BK_DATE" backup directory already exists!" >>$LOGFILE
echo "There is no need to backup again!!" >>$LOGFILE
exit 1
fi

#get repo version number date
echo " ">>$THIS_NUM_FILE
echo "=============$(date +%Y-%m-%d_%T)==============" >>$THIS_NUM_FILE

#if last full version number directory exist then start backup
#if not exist then create and insert into this time repo version number!
if [ -e "$LAST_FULL_NUM" ]
then
  for name in $NAME_LIST
  do
    thisnum=$($LOOK $SVN_ROOT/$name)
    echo "[$name] this time version_number : $thisnum" >>$THIS_NUM_FILE
    lastnum=$(cat "$LAST_FULL_NUM"/"$name".number)
    if [ $thisnum -eq $lastnum
   then
      echo "[$name] no new version!!" >>$LOGFILE
    else
  $DUMP $SVN_ROOT/$name -r $lastnum:$thisnum --incremental >$BK_DIRC/Increment_"$BK_DATE"/$name.dumpfile

       echo "[$name] version(from $lastnum to $thisnum) backup successful!" >>$LOGFILE

    fi

  done

else
mkdir $LAST_FULL_NUM
for name in $NAME_LIST
do
  thisnum=$($LOOK $SVN_ROOT/$name)
      echo "[$name] this time version_number : $thisnum" >>$THIS_NUM_FILE
  echo "$thisnum" >>$LAST_FULL_NUM/$name.number
done
echo "$LAST_FULL_NUM directory is not exist!" >>$LOGFILE
echo "create directory $LAST_FULL_NUM !!" >>$LOGFILE

fi

#output a blank line


 

echo " " >>$LOGFILE

(责任编辑:admin)