SVN backup/recovery script

Source: Internet
Author: User

SVN backup/recovery script

The previous article introduced the configuration of svn (see). Here we have written two scripts for full backup and recovery of svn. Because it is full backup, we can make a scheduled task and execute it once a day. Keep data for several days.

1. Backup script:
#! /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"
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

# This script provides full backup for the svn database and keeps one copy uploaded to the FTP server.

 

2. Restore the script
#! /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"
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

# Download the backup script from the FTP server and restore it to the specified directory automatically

Many of them may not be perfect. You can change them as needed.

Set up SVN server svn in Ubuntu 14.04 ://

CentOS 6.2 SVN setup (YUM installation)

Deploy Apache + SVN in CentOS 6.5

Build an SVN server using Apache + SVN

Set up and use the SVN server in Windows + reset the password on the client

Install SVN in Ubuntu Server 12.04 and migrate Virtual SVN data

Build svn service and migration method on Ubuntu Server

This article permanently updates the link address:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.