# 此脚本对svn的库进行全备,并且上传至FTP服务器保留一份。
#!/bin/sh
# Description: SVN full backup of the project
#
SVN_BASICDIR="/usr/bin"
DAY=5 # Backup files keep days
TIME=`date +%Y%m%d%H%M`
SVNPATH="/var/svn"
BAKDIR="/data/svnbak"
BAKPATH=${BAKDIR}/${TIME}
BAKLOG=${BAKDIR}/bak_svn.log
PROJECT=`/bin/ls -lh ${SVNPATH} | grep -E "^d" | awk '{print $NF}'`
CONFIG_BASE="/etc/svn"
CONIFIG_FILE=`/bin/ls -lh ${CONFIG_BASE} | grep -E "^-" | awk '{print $NF}'`
! type lftp &> /dev/null && echo "Lftp command does not exist,Plz install lftp." && exit 128
[ ! -d "${BAKPATH}" ] && mkdir -p ${BAKPATH}
make_log() {
/bin/echo -e "[ $(date +%Y-%m-%d' '%H:%M:%S) ] ${1}" >> ${BAKLOG}
}
tar_bakfile() {
cd ${BAKDIR}
make_log "tar zcf ${1##*/}.tgz ${1##*/}"
tar zcf ${1##*/}.tgz ${1##*/} &> /dev/null
make_log "rm -rf ${1##*/}"
rm -rf ${1##*/} &> /dev/null
cd
echo "" >> ${BAKLOG}
}
rm_expired() {
cd ${1}
_DAY_=`date -d"${DAY} days ago" +%Y%m%d`
ls | grep ${_DAY_} | xargs rm -rf
cd
}
#ftp_upload() {
#################################
#FTP upload
# cd ${BAKDIR}
#echo "Start upload file: `date`"
# FTPHOST="x.x.x.x"
# FTPPORT="21"
# FTPUSER="xxx"
# FTPPASSWD="xxx"
# FTPDOCUMENT="xxx"
#/usr/bin/lftp << EOF
#open -p $FTPPORT ftp://$FTPUSER:$FTPPASSWD@$FTPHOST/$FTPDOCUMENT
#put $1
#EOF
#echo "Upload file end: `date`"
##################################
#}
for i in ${CONIFIG_FILE};do
cp ${CONFIG_BASE}/${i} ${BAKPATH}/
[ $? != 0 ] && echo "Configuration file backup failed..." >> ${BAKLOG}
done
make_log "=============== Start Backup ==============="
for _bak_project_ in ${PROJECT};do
make_log "${SVN_BASICDIR}/svnadmin hotcopy ${SVNPATH}/${_bak_project_} ${BAKPATH}/${_bak_project_}"
${SVN_BASICDIR}/svnadmin hotcopy ${SVNPATH}/${_bak_project_} ${BAKPATH}/${_bak_project_} &> /dev/null
if [ $? != 0 ];then
make_log "Project ${_bak_project_} backup failed, Plz check..."
echo "Project ${_bak_project_} backup failed, Plz check..."
ls -ldh ${BAKDIR}/* | grep '^d' | awk '{print $NF}' | xargs rm -rf
exit 122
fi
done
make_log "================ End Backup ================" && echo "" >> ${BAKLOG}
make_log "=============== Start Check ==============="
for _check_proejct_ in ${PROJECT};do
make_log "${SVN_BASICDIR}/svnlook youngest ${BAKPATH}/${_check_proejct_}"
${SVN_BASICDIR}/svnlook youngest ${BAKPATH}/${_check_proejct_}&> /dev/null
if [ $? != 0 ];then
make_log "Project ${_check_proejct_} backup file Invaild.Plz check..."
echo "Project ${_check_proejct_} backup file Invaild.Plz check..."
ls -ldh ${BAKDIR}/* | grep '^d' | awk '{print $NF}' | xargs rm -rf
exit 123
fi
done
make_log "================ End Check ================" && echo "" >> ${BAKLOG}
tar_bakfile "${BAKPATH}"
if [ $? != 0 ];then
make_log "Compressed file error."
echo "Compressed file error."
exit 124
else
#ftp_upload "${BAKPATH##*/}.tgz"
rm_expired "${BAKDIR}"
fi
2018-04-09
对svn服务端托管库进行完全备份shell脚本
评论
发表评论
姓 名: