#!/bin/bash#svn服务器地址如下,本机的ip地址为192.168.4.248IP152=192.168.3.152Password=xxxx2015fewdaysago=`date -d "8 day ago" +%Y%m%d`now=`date +%Y%m%d`#定义svn备份日志路径logdir=/svnbackup/log#定义svn备份路径backupdir=/svnbackup#定义httpd的配置文件subdir=/etc/httpd/conf.d#定义 函数 连接192.168.3.152 svn服务器connection152() { cd $backupdir/backup152 #如果系统存在8天前的备份则删除。 if [[ -d $fewdaysago ]] then rm -rf $fewdaysago else echo "1. $fewdaysago not exist in /svnbackup/backup152/ " >> $logdir/report152_$now.log 2>&1 fi echo "2. scp backup svn from the 3.152 to 4.248 right now " >> $logdir/report152_$now.log 2>&1 #这里利用expect 实现自动输入密码等操作。使用该命令前,请先安装yum -y install expect* expect -c " #永不超时 set timeout -1 #定义一个子shell,执行scp命令,将192.168.3.152上的svn备份数据,拷贝到本机。 spawn scp -r root@$IP152:/backup/svn/backup/$now.tar.gz $backupdir/backup152 #自动化输入密码 expect \"root@$IP152's password:\" send \"$Password\r\" #完成传输则结束命令。 expect \"eof\" " #对上面的操作进行判断,如果正常执行完,则继续下面的动作。 if [[ $? = "0" ]]; then echo "3. backup 152svn have done " >> $logdir/report152_$now.log 2>&1 fi echo "4. unpack the $now.tar.gz" >> $logdir/report152_$now.log 2>&1 cd $backupdir/backup152 tar -xf $now.tar.gz echo "5. delete tar pack " >> $logdir/report152_$now.log 2>&1 rm -f $now.tar.gz echo "6. Testing rollback 152svn backup right now" >> $logdir/report152_$now.log 2>&1 #删除本机svn库目录下的所有文件及目录除了conf目录。这里本机做这个是为了测试拷贝过来的svn备份文件能够正常恢复。演练svn服务器灾后恢复。 cd $backupdir/152 ls -l $backupdir/152 | grep -v "conf" | awk '{print $9}' | while read dir do rm -rf $dir done echo "7. yesterday data all has delete done" >> $logdir/report152_$now.log 2>&1 echo "8. rollback now" >> $logdir/report152_$now.log 2>&1} #开始svn灾后恢复动作。start() { mv $backupdir/backup152/backup/svn/$now $backupdir/backup152/ rm -rf $backupdir/backup152/backup/* rm -rf $backupdir/backup152/backup/ cd $backupdir/backup152/$now cp -fvr $backupdir/backup152/$now/* $backupdir/152 #这里svn结合了httpd,方便用户web端访问。所以,httpd的配置文件中,就要修改下svn工程目录的位置。 cd $subdir echo "9. change httpd subversion.conf" >> $logdir/report152_$now.log 2>&1 #自动化执行httpd配置文件替换和httpd重启动作。 expect -c " set timeout -1 spawn cp -ar $subdir/subversion152 $subdir/subversion.conf expect \"cp: overwrite 'subversion.conf'?\" send \"yes\r\" expect \"eof\" " #给目录赋权。 echo "10. chown -R apache.apache $backupdir/152" chown -R apache.apache $backupdir/152 /etc/init.d/httpd restart}connection152start
以上shell可以执行svn的远程异地备份动作。磁盘监控以及mail报警,在下篇日志将介绍。