一、增量备份脚本
#!/bin/bash #svn 增量备份脚本 repos_path="/home/svn/repos" repos=`ls $repos_path` Date=`date +%Y%m%d` backup_path="/data1/backup/svn/" echo `date` >> $backup_path/svn_backup.log for i in $repos do pre_youngest=`cat $backup_path/$i/youngest_revision` #获取上次备份到的版本号 youngest=`svnlook youngest $repos_path/$i` #获取最后提交的版本号 if [ $pre_youngest -eq $youngest ] then echo "$i has no new revisions to backup." >> $backup_path/svn_backup.log continue fi first_youngest=$[$pre_youngest +1] #获取新提交的版本号 if [ ! -d "$i" ]; then mkdir -p $backup_path/$i fi svnadmin dump --incremental --revision $first_youngest:$youngest $repos_path/$i > $backup_path/$i/$i.inc.$Date #备份新提交的版本到最后提交的版本 if [ $? -eq 0 ]; then echo "$i backup success" else echo "$i backup failed" fi echo $youngest > $backup_path/$i/youngest_revision done
二、全量备份脚本
#!/bin/bash #svn 全量备份脚本 repos_path="/home/erduo/svn/repos" back_path="/data1/backup/svn" repos=`ls /home/erduo/svn/repos` Date=`date +%Y%m%d` cd $back_path #进入目录,使总备份文件存放在该目录 echo `date` >> $back_path/svn_backup.log for i in $repos do youngest=`svnlook youngest $repos_path/$i` #获取最后提交的版本号 svnadmin dump --revision 0:$youngest $repos_path/$i > $back_path/$i/$i-full.$Date #备份 if [ $? -eq 0 ]; then echo "$i Full amount of backup success" >> $back_path/svn_backup.log else echo "$i Full amount of backup failed" >> $back_path/svn_backup.log fi echo $youngest > $back_path/$i/youngest_revision find $back_path/$i -mtime +20 -type f -name "$i.*" |xargs rm -f #删除二十天以上的备份文件 done find $back_path -type f -name "*-full*" |xargs tar -C $back_path -zcvf svn_backup_full_$Date.tar.gz --exclude=svn_backup_full_* #将各仓库的备份文件打包 scp -P 10086 svn_backup_full_$Date.tar.gz ray@192.168.1.250:/data1/backup/backup_svn_254 #将打包好的备份文件传到另一台服务器上,这里需要先设置好 ssh的免密登录,不然会需要输入密码,大致流程是本机生成密钥对,然后将公钥传至目标主机,目标主机把公钥内容追加到authorized_keys中。 #-P 为指定端口 if [ $? -eq 0 ]; then echo "svn_backup_full_$Date.tar.gz has been copy to 192.168.1.250 successfully" fi find $back_path -mtime +5 -type f -name "svn_backup_full_*.tar.gz" |xargs rm -f #删除打包好的备份文件,这里写的规则是删除五天前的文件,是因为偷懒懒得改了,定时任务设置的是一周备份一次,就当是保存上周的备份了。
三.把脚本放入定时执行就可以了;