#!/bin/sh# Copyright 2006 Bellière Ludovic <xrogaan@gmail.com># Distributed under the terms of the GNU General Public License v2# exit on any failuresset -e# report unset variablesset -uversion="1.0c"maintainer="xrogaan@gmail.com"PWD=`pwd`SVNCMD="svn co"CMD_ARCH="tar cpsf"DATE=`date +%Y%m%d`prog=`basename $0`CURRENT="$PWD"TEMPPATH="/tmp/svndatadump_$$"DUMPPATH="${PWD}/dump"PATHS=''ARCHIVETYPE="gz"evalopt=falsequietopt=falseuserev=falseEXT=''BLUE="[34;01m"CYAN="[36;01m"GREEN="[32;01m"RED="[31;01m"OFF="[0m"# synopsis: qprint "message"qprint() { $quietopt || echo "$*" >&2}# synopsis: mesg "message"# Prettily print something to stderr, honors quietoptmesg() { qprint " ${GREEN}*${OFF} $*"}# synopsis: warn "message"# Prettily print a warning to stderrwarn() { echo " ${RED}* Warning${OFF}: $*" >&2}# synopsis: error "message"# Prettily print an errorerror() { echo " ${RED}* Error${OFF}: $*" >&2}# synopsis: die "message"# Prettily print an error, then abortdie() { [ -n "$1" ] && error "$*" qprint $evalopt && { echo; echo "false;"; } exit 1}# synopsis: versinfo# Display the version informationversinfo() { qprint qprint "${GREEN}SVN Backup ${version};${OFF}" qprint "Copyright 2006 Belliere Ludovic <${maintainer}>; Distributed under the GPL" qprint}# synopsis: helpinfo# Display the help information. There's no really good way to use qprint for# this...helpinfo() { cat >&2 <<EOHELP This script will create a dump from your SVN repository. This dump will named svn-[currentDate].[ext]. By default, the datas will by compressed in gzip format and the archive will be put on dump directory.SYNOPSIS $prog [ [${BLUE}-at${OFF} |${BLUE}--archive-type=${OFF}][${CYAN}bz${OFF}|${CYAN}gz${OFF}|${CYAN}tar${OFF}|${CYAN}zip${OFF}] ] [ ${BLUE}--revision${OFF} ${CYAN}ARG${OFF} ] [ ${BLUE}--targets${OFF} ${CYAN}ARG${OFF} ] [ ${BLUE}-o${OFF}|${BLUE}--dump${OFF} ${CYAN}PATH${OFF} ] [ ${BLUE}--username${OFF} ${CYAN}ARG${OFF} ] [ ${BLUE}--password${OFF} ${CYAN}ARG${OFF} ] [ ${BLUE}--config-dir${OFF} ${CYAN}ARG${OFF} ] [ ${BLUE}--limit${OFF} ${CYAN}ARG${OFF} ] ${CYAN}URL${OFF}COMMANDS ${BLUE}-at${OFF} [${BLUE}--archive-type${OFF}] ${CYAN}FMT${OFF} Create an archive of the backup. FMT can be one of: bz2 : Creates a bzip2 compressed tar file. gz : Creates a gzip compressed tar file. tar ; Creates a compressed tar file. zip : Creates a compressed zip file. ${BLUE}?${OFF} [${BLUE}--help${OFF}|${BLUE}-h${OFF}] Get the current message. ${BLUE}-o${OFF} [${BLUE}--dump${OFF}] Set the dump directory. command: -r, --revision, --no-auth-cache, --username, --password, --no-auth-cache, --non-interactive, --config-dir, -q, --quiet, --non-recursive, --ignore-externals and -N (see \`svn help log' for more information).EOHELP}while [ $# -gt 0 ]do case "$1" in --archive-type|-at) ARCHIVETYPE="$2"; shift 2 || die "$prog: option requires an argument -- ${CYAN}$1${OFF}"; ;; --archive-type=*) ARCHIVETYPE=`echo "$1" | sed 's/^--[a-z-]*=//'` shift ;; -o|--dump) DUMPPATH="$2"; shift 2 || die "$prog: option requires an argument -- ${CYAN}$1${OFF}"; ;; --dump=*) DUMPPATH=`echo "$1" | sed 's/^--[a-z-]*=//'` shift ;; -r|--revision|--username|--password|--config-dir) # add these as extra options to the command (with argument) arg=`echo "$2" | sed "s/'/'\"'\"'/g"` SVNCMD="$SVNCMD $1 '$arg'" if [ "$arg" -eq "-r" -o "$arg" -eq "--revision" ]; then EXT="${EXT}-r$arg" userev=true fi shift 2 || die "$prog: option requires an argument -- ${CYAN}$1${OFF}"; ;; -N|--non-recursive|-q|--quiet|--non-interactive|--no-auth-cache|--ignore-externals) # add these as extra options to the command (with argument) if [ "$1" -eq "-q" -o "$1" -eq "--quiet"]; then quietopt=true fi SVNCMD="$SVNCMD $1" shift ;; -h|--help|?) # versinfo uses qprint, which honors --quiet versinfo helpinfo exit 0 ;; -*) die "invalid option -- ${CYAN}$1${OFF} Try \`$prog --help' for more information." ;; *) arg=`echo "$1" | sed "s/'/'\"'\"'/g"` SVNCMD="$SVNCMD '$arg'" PATHS="'$arg'" shift ;; esacdonecase $ARCHIVETYPE in bz|bz2) EXT="${EXT}.bz2" CMD_ARCH="${CMD_ARCH}j" ;; gz|gzip) EXT="${EXT}.tar.gz" CMD_ARCH="${CMD_ARCH}z" ;; tar) EXT="${EXT}.tar" ;; zip) EXT="${EXT}.zip" CMD_ARCH="zip -6 -r" ;; *) die "invalid argument for ${CYAN}--archive-type${OFF} : $ARCHIVETYPE Try \`$prog --help' for more information." ;;esacif [ ! -d $TEMPPATH ]; then echo -n " * Creating temporary directory ${BLUE}${TEMPPATH}${OFF}..." mkdir $TEMPPATH echo " ${GREEN}done${OFF}" echo -n " * Getting repository data ${BLUE}${PATHS}${OFF}..." SVNCMD="$SVNCMD $TEMPPATH" eval $SVNCMD echo " ${GREEN}done${OFF}"else die "Directory ${BLUE}${TEMPPATH}${OFF} already exists. Try 'rm -rf ${TEMPPATH}' and restart ${prog}."fi#compress cmdif [ userev ]; thenARCHNAME="svn${EXT}"elseARCHNAME="svn-${DATE}${EXT}"fiCMD_ARCH="${CMD_ARCH} $DUMPPATH/${ARCHNAME} ${TEMPPATH}"# remove all directory .svn/, set chmod 775 for all directory and chmod -x for all filesfind $TEMPPATH -depth -type d -name .svn -exec rm -fr {} \;find $TEMPPATH -type d -exec chmod 775 {} \;find $TEMPPATH -type f -name "*.php" -exec chmod -x {} \;#Create the dump directory if dont existsif [ ! -d '$DUMPPATH' ]; then mkdir "$DUMPPATH"ficd $DUMPPATHif [ -f "$DUMPPATH/svn-${DATE}${EXT}" ]; then warn "$DUMPPATH/svn-${DATE}${EXT} already exists, deleting ..." rm -f "$DUMPPATH/svn-${DATE}${EXT}"fiecho -n " * Creating dump archive ${BLUE}$DUMPPATH/svn-${DATE}${EXT}${OFF} ..."eval $CMD_ARCHecho " ${GREEN}done${OFF}"echo;cd $CURRENTrm -rf $TEMPPATH#EOF