SVN备份/恢复脚本

2022-07-04 12:37:18 浏览数 (1)

上一篇介绍了svn的配置(见http://www.linuxidc.com/Linux/2015-02/113177.htm),这里就写了两个脚本对svn进行全备和恢复,由于是全备,所以可以做个计划任务,每天执行一次即可。保留几天的数据。

1、备份脚本: #!/bin/sh # # # Description: SVN full backup of the project # Author: Mr.chen # Date: 2015/02/10 # Email:  # # SVN_BASICDIR="/usr/local/svn/bin" DAY=5      # Backup files keep days TIME=`date %Y%m%d%H%M` SVNPATH="/data/svn" BAKDIR="/data/svnbak" BAKPATH=${BAKDIR}/${TIME} BAKLOG=${BAKDIR}/bak_svn.log PROJECT=`/bin/ls -lh ${SVNPATH} | grep -E "^d" | awk '{print $NF}'` CONIFIG_FILE=`/bin/ls -lh ${SVNPATH} | 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 ${SVNPATH}/${i} ${BAKPATH}/     [ $? != 0 ] && echo "Configuration file backup failed..." >> ${BAKLOG} && ls -ldh ${BAKDIR}/* | grep '^d' | awk '{print $NF}' | xargs rm -rf && exit 121 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 # 此脚本对svn的库进行全备,并且上传至FTP服务器保留一份。

2、恢复脚本 #!/bin/sh # # Description: Download the backup file and restore from the FTP server # Author: Mr.chen # Date: 2015/02/10 # Email: # # # Require: SVN environment is normal # SVN_USER="svn" SVN_BASICDIR="/usr/local/svn/bin" YESTERDAY=`date -d'1 days ago' %Y%m%d` SVNDIR="/data/svn" DOWNLOAD_DIR="/data/download" LOGFILE="${DOWNLOAD_DIR}/svn_recover.log" FTPHOST="x.x.x.x" FTPPORT="x" FTPUSER="xxx" FTPPASSWD="xxxxxx" FTPDOCUMENT="xxx" ! type lftp &> /dev/null && echo "Lftp command does not exist,Plz install lftp." && exit 128 [ ! -d ${DOWNLOAD_DIR} ] && mkdir -p ${DOWNLOAD_DIR} make_log() {     /bin/echo -e "[ $(date %Y-%m-%d' '%H:%M:%S) ] ${1}" >> ${LOGFILE} } ftp_download() {     ############################     # Backup file download     cd ${1}     #echo "Start download file: `date`" /usr/bin/lftp << EOF open -p $FTPPORT ftp://$FTPUSER:$FTPPASSWD@$FTPHOST/$FTPDOCUMENT mirror -i ${YESTERDAY}.*.tgz EOF     cd        #echo "Download file end: `date`"     ############################ } extract() {     cd ${1}     make_log "tar xf *.tgz"     tar xf *.tgz &> /dev/null     make_log "rm -f *.tgz"     rm -f *.tgz &> /dev/null } ftp_download "${DOWNLOAD_DIR}" if [ `ls ${DOWNLOAD_DIR} | grep "tgz$"` ];then     extract "${DOWNLOAD_DIR}"     BAKDIR=`ls ${DOWNLOAD_DIR} | grep -Ev "log"`     #     # Recover SVN Project     make_log "=========== [`date %Y/%m/%d' '%H:%M`] Start Recover ==========="     for _project_ in `ls -lh ${DOWNLOAD_DIR}/${BAKDIR} | grep "^d" | awk '{print $NF}'`; do         if [ ! -d ${SVNDIR}/${_project_} ];then             make_log "mkdir -p ${SVNDIR}/${_project_}"             mkdir -p ${SVNDIR}/${_project_} &> /dev/null         else             make_log "Project directory exist."             echo "Project directory exist."             exit 127         fi         make_log "${SVN_BASICDIR}/svnadmin hotcopy ${DOWNLOAD_DIR}/${BAKDIR}/${_project_} ${SVNDIR}/${_project_}"         ${SVN_BASICDIR}/svnadmin hotcopy ${DOWNLOAD_DIR}/${BAKDIR}/${_project_} ${SVNDIR}/${_project_} &> /dev/null         make_log "${SVN_BASICDIR}/svnlook youngest ${SVNDIR}/${_project_}"         ${SVN_BASICDIR}/svnlook youngest ${SVNDIR}/${_project_} &> /dev/null         if [ $? != 0 ];then             make_log "Recover Failed."             echo "Recover Failed."             exit 125         fi     done     echo "" >> ${LOGFILE}     #     # Copy configuration file     for _config_file_ in `ls -lh ${DOWNLOAD_DIR}/${BAKDIR} | grep "^-" | awk '{print $NF}'`; do         #make_log "cp ${DOWNLOAD_DIR}/${BAKDIR}/${_config_file_} ${SVNDIR}"         #make_log "chown svn.svn ${SVNDIR}/${_config_file_}"         cp ${DOWNLOAD_DIR}/${BAKDIR}/${_config_file_} ${SVNDIR}     done     make_log "chown -R ${SVN_USER}.${SVN_USER} ${SVNDIR}"     make_log "rm -rf ${DOWNLOAD_DIR}/${BAKDIR}"     rm -rf ${DOWNLOAD_DIR}/${BAKDIR}     chown -R ${SVN_USER}.${SVN_USER} ${SVNDIR} &> /dev/null     make_log "=========== [`date %Y/%m/%d' '%H:%M`] Recover Finish ==========="     echo "" >> ${LOGFILE}     #echo "Recover SVN Project successfull." else     make_log "Download Backup File Failed."     echo "" >> ${LOGFILE}     echo "Download Backup File Failed.Plz Check..."     exit 122 fi # 从FTP的服务器下载备份脚本,并自动恢复到指定目录

有可能有很多不是很完善,如需要可以自己随便改改就行了。

0 人点赞