Linux · Shell · 点滴记录

一个SVN批量备份脚本

此脚本是根据https://blog.ohyeahwoo.com/?p=68此搭建环境来做备份的

[root@localhost scripts]# cat svn_dump.sh
#!/bin/bash
#Date 2020-05-12
#Version 1.2
#Update 2020-05-13

. /etc/profile

svndatadir=/opt/svn
backupdir=/opt/backup_svn
backuptimes=`date +%Y%m%d%H%M`
old_verfile=’old_version.txt’
BAKIP=’192.168.31.100′

for i in `ls ${backupdir}`;do if [ ! -f ${backupdir}/${i}/old_version.txt ];then echo “${backupdir}/${i}/old_version.txt no such file or directory”; fi; done

for a in `ls ${svndatadir}`
do
if [ -d ${backupdir}/${a} ];then
cd ${backupdir}/${a}
echo “cd ${backupdir}/${a} 进入目录”
elif [ -f ${backupdir}/${a}/${old_verfile} ];then
cd ${backupdir}/${a}
echo “请手动创建文件 ${backupdir}/${a}/${old_verfile}”
exit 1
else
mkdir -p ${backupdir}/${a} && cd ${backupdir}/${a} && echo 0 > ${old_verfile}
echo “mkdir -p ${backupdir}/${a} && cd ${backupdir}/${a} && echo 0 > ${old_verfile}”
fi

start_ver=`cat old_version.txt |awk ‘{print int($0)}’`
end_ver=`svnlook youngest ${svndatadir}/${a} | awk ‘{print int($0)}’`

if [ `expr $end_ver – $start_ver` -eq 0 ];then
echo -e “${backupdir}/${a} 项目版本没有更新无需备份!”

elif [ `expr $end_ver – $start_ver` -gt 0 ] && [ $start_ver -eq 0 ];then
svnadmin dump ${svndatadir}/${a} -r 0:${end_ver} –incremental |gzip > ${a}_0_${end_ver}_${backuptimes}.dump.gz \
&& echo $end_ver > $old_verfile \
&& scp ${a}_0_${end_ver}_${backuptimes}.dump.gz ${old_verfile} ${BAKIP}:${backupdir}/${a}

elif [ `expr $end_ver – $start_ver` -gt 0 ] && [ $start_ver -ne 0 ];then
svnadmin dump ${svndatadir}/${a} -r `expr ${start_ver} + 1`:${end_ver} –incremental |gzip > ${a}_`expr ${start_ver} + 1`_${end_ver}_${backuptimes}.dump.gz \
&& echo $end_ver > $old_verfile \
&& scp ${a}_`expr ${start_ver} + 1`_${end_ver}_${backuptimes}.dump.gz ${old_verfile} ${BAKIP}:${backupdir}/${a}

else
echo -e “${backupdir}/${a} 项目版本异常,请查看备份版本情况!”
fi
done