System Engineering

Subversion Repogitories Backup Script

Subversion

Features

  • Full Backup (Dump)
  • Incremental Backup (Dump)
  • Local Backup & Remote(AWS S3) Upload

Sample Environments

  • REPOS_ROOT=/home/svn/repos
  • BACKUP_ROOT=/home/backup/svn
  • S3_PATH=s3://svn-backup/svn
#!/bin/bash
#----------------------------------------
# Subversion Repogitories Backup Script
# Revision 20160531
# Copyright (c) Aiden Kim
# http://www.daemon.pe.kr
#----------------------------------------

DSTR=$(date +"%Y%m%d")
ARCHIVE_PERIOD=30
REPOS_ROOT=/home/svn/repos
BACKUP_ROOT=/home/backup/svn
S3_PATH=s3://svn-backup/svn

# Each repogitories
REPOS_DIRS=`ls -t ${REPOS_ROOT} 2>/dev/null`
for DIR in ${REPOS_DIRS}; do

    # Create directory
    BACKUP_PATH=${BACKUP_ROOT}/repo-${DIR}
    if ! [ -d ${BACKUP_PATH} ]; then
        /bin/mkdir -p ${BACKUP_PATH}
    fi

    # Get lastest revision
    CURR_VER=`/usr/bin/svnlook youngest ${REPOS_ROOT}/${DIR} 2>/dev/null`
    if ! [[ ${CURR_VER} =~ ^[0-9]+$ ]] ; then
        echo "Could not get revision of repogitory ${DIR}. Skipped..."
        continue
    fi

    if [[ "$1" == "full" ]]; then
        # Full dump
        /usr/bin/svnadmin dump -q -r0:${CURR_VER} ${REPOS_ROOT}/${DIR} > ${BACKUP_PATH}/${DIR}-${DSTR}-full-${CURR_VER}.dump && /usr/bin/gzip -f ${BACKUP_PATH}/${DIR}-${DSTR}-full-${CURR_VER}.dump
    else
        # Find last full dump
        LAST_VER=`/usr/bin/ls -d ${BACKUP_PATH}/*-full-*.dump.gz | sed -r 's/^.*-full-([0-9]+)\.dump\.gz$/\1/' | sort -g | tail -n 1 2>/dev/null`
        if [[ ${LAST_VER} =~ ^[0-9]+$ ]] ; then
            if [ ${CURR_VER} -gt ${LAST_VER} ] ; then
                /usr/bin/svnadmin dump -q --incremental -r${LAST_VER}:${CURR_VER} ${REPOS_ROOT}/${DIR} > ${BACKUP_PATH}/${DIR}-${DSTR}-incremental-${LAST_VER}-${CURR_VER}.dump && /usr/bin/gzip -f ${BACKUP_PATH}/${DIR}-${DSTR}-incremental-${LAST_VER}-${CURR_VER}.dump
            else
                echo "Revision of ${DIR} was not changed. Current ${CURR_VER}, Last full-dump ${LAST_VER}. Skipped..."
            fi
        else
            echo "Could not file previous full-dump of ${DIR}. Skipped..."
        fi
    fi

    # Garbage
    rm -f ${BACKUP_PATH}/*.dump
done

# Sync to s3 bucket
/usr/bin/aws s3 sync ${BACKUP_ROOT}/ ${S3_PATH}/

# Remove old files
oldFiles=($(/bin/find ${BACKUP_ROOT} -type f -mtime +${ARCHIVE_PERIOD}))
oldFilesLen=${#oldFiles[@]}
for (( i=0; i<${oldFilesLen}; i++ )); do
    /bin/rm -rf ${oldFiles[$i]}
done

exit 0

Executing

# Full Backup
./backup.svn.sh full

# Incremental Backup
./backup.svn.sh

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.