#!/bin/bash
#
# Filename:    svnbak.sh
# Revision:    1.0
# Date:        2014/11/07
# Author:      Qicheng
# Email:
# Website:
# Description: SVN完全备份
# Notes:       将脚本加入crontab中,每天定时执行
#
MAXDAYS=7                       # 备份保留最大天数
TIME=`date +'%Y%m%d%H%M%S'`
SVNPATH=/data/svn               # 定义仓库repos路径
BAKDIR=/data/svnbackup          # 定义备份主目录
BAKSUBDIR=${BAKDIR}/${TIME}     # 定义全备路径
BAKLOG=${BAKDIR}/${TIME}.log    # 定义备份日志
[ ! -f "${BAKSUBDIR}" ] && mkdir -p ${BAKSUBDIR}
exec 1>${BAKLOG}
exec 2>&1

set -x
# 删除历史备份
find ${BAKDIR} -maxdepth 1 -type f -mtime +$(expr ${MAXDAYS} - 1) -exec echo "Delete {}" \; -exec rm "{}" \;
# 全备份
svnadmin hotcopy ${SVNPATH} ${BAKSUBDIR}
# 如果能打印版本号说明备份正确
svnlook oungest ${BAKSUBDIR}
[ $? -ne 0 ] && exit 1

cd ${BAKDIR}
# 压缩备份
tar cvzf ${TIME}.tar.gz ${TIME}/
rm -rf ${BAKSUBDIR}
# 可以通过rsync把备份文件同步到远程备份机上,此处省略。