Its interesting that while source code repository like Concurrent Versions System (CVS) and Subversion (SVN) are primary used as for source code sharing and revision management, one useful purpose for these repositories are actually useful for backups. Its like using using Google Docs not only for online collaboration, but to keep your documents in the cloud in case you lose them.
An interesting twist at work is that I have to backup the subversion database in case Subversion fails- here we are backing up the backups.
The power and freedom in linux usually means that often no one does a single job a single way. So, here’s my take on writing a script used for saving my svn databases.
# Backup SVN Script
DIR=/svn # Svn directory
DATE=$(date +%Y%m%d) # Date
BACKUPDIR = /backupscd $BACKUPDIR
# Clear a temporary directory
rm -rf dumps/*
# Creating a list of SVN directories
for i in $(ls -l $DIR | awk '/^d/ { print $NF }') ; do
echo $i
# Export each SVN database
svnadmin dump $DIR/$i > dumps/$i.dump
done
cp $DIR/* dumps/ 2> /dev/null
# Taring them
tar zvcf archive/svn$DATE.tgz dumps/*
# Copying to another safe location
cp archive/svn$DATE.tgz /another/secret/location
Brief explaination of the script, if the script and comments doesn’t speak enough. All svn databases are dumpped in svn export/import friendly formats which are then zlib tarred.
Lets assume one day Murphy’s Law take places, your entired svn database vanish into thin air but your backups in the safe secret location is intact. Here’s how you could restore the subversion database easily, assuming your SVN servers are up and running.
# load_repo_after_the_sky_crashes.sh
tar zvxf $1.tgz # extract the backups
for i in $(ls -l dumps | awk ‘/^d/ { print $NF }’) ; do
echo $i
svnadmin create $i
svnadmin load $i < $1.dump
# chown / chmod file system permissions if needed,
done
The method for backups and restore is similar to migrating SVN repository and here’s a post if you wish to read up on them.
The backup script may be called by a cron job and run periodically like daily. To prevent taking too much disk space, you may decide to keep a number of the latest backup copies and delete older backup copies. Here’s a line in bash you could do for this.
ls -t * | tail -n +10 | xargs rm -rf # Delete everything except the 9 latest files
Who backup the backups like who guard the guards?


Recent Comments