Thursday, December 1, 2016

bash scripts to backup svn server

there is nothing nearer and dearer to my heart than my svn server. if i lost it i would be unhappy for a very long time.
i have a bunch of scripts here:
/nfserver/bin
why? because if i lost my nfs mounts, my scripts would not work and i would not have to deal with my fs filling up.
yes, i could check for the mount being active, but why bother? i like keeping all my eggs in one basket.
 
 svn_backup.sh
  
 #!/bin/bash  
   
 # set values  
 repos=( repo1 repo2 repo3 )  
 rpath=/var/svn/repositories  
 opath=/nfsmount/svn  
 tpath=/tmp/svn  
 suffix=$(date +%Y-%m-%d)  
   
     #check if we need to make output path  
     if [ -d $opath ]  
     then  
         # directory exists, we are good to continue  
         filer="just some action to prevent syntax error"  
     else  
         #we need to make the directory  
         echo Creating $opath  
         mkdir -p $opath  
     fi  
   
     # remove contents of tmp  
     rm -rf $tpath  
     mkdir -p $tpath  
   
 for (( i = 0 ; i < ${#repos[@]} ; i++ ))  
 do  
   
     svnadmin hotcopy $rpath/${repos[$i]} ${tpath}/${repos[$i]}_$suffix.hotcopy  
   
     #now compress them  
     tar -czf ${opath}/${repos[$i]}_$suffix.hotcopy.tar.gz -C ${tpath}/${repos[$i]}_$suffix.hotcopy .  
   
     if [ -s error ]  
     then  
         printf "WARNING: An error occured while attempting to backup %s \n\tError:\n\t" ${repos[$i]}  
         cat error  
         rm -f er  
     else  
         printf "%s was backed up successfully \n\n" ${repos[$i]} $SVNDUMP  
     fi  
 done  
   
let's backup the individual hooks and conf directories. and apache conf, too.
 hotcopy will backup db, and that's about it.  
 we need confs. hooks. and stuff. logs meh.  
   
 the svn server has the following layout:  
   
 > hookscripts  
  mailer.conf  
  no-archives.py  
  post-commit  
  pre-commit  
  pre-revprop-change  
  readme.txt  
  svnperms.conf  
  svnperms.py  
   
 > logs  
  commit-email.log  
  repo-pre-commit  
  svn_logfile  
   
 > repositories  
  > repo  
   > conf  
   > dav  
   > db  
   > format  
   > hooks  
    > locks  
 
svn_apacheconf_backup.sh

#!/bin/bash  
   
 # set values  
 apacheconf=( /etc/apache2 )  
 svnconf=( /var/svn/hookscripts )
 repos=( repo1 repo2 repo3 )  
 confdirs=( conf hooks )  
 rpath=/var/svn/repositories  
 opath=/nfsmount/svn  
 suffix=$(date +%Y-%m-%d)  
   
 #check if we need to make path  
 if [ -d $opath ]  
 then  
     # directory exists, we are good to continue  
     filler="just some action to prevent syntax error"  
 else  
     #we need to make the directory  
     echo Creating $opath  
     mkdir -p $opath  
 fi  
   
 #now do the apache backup  
 APACHECONFDUMP=${opath}/apacheconf_$suffix.tar.gz  
   
 tar -zcvf $APACHECONFDUMP $apacheconf 2>&1  
   
 if [ -s error ]  
 then  
     printf "WARNING: An error occured while attempting to backup %s \n\tError:\n\t" $apacheconf  
     cat error  
     rm -f er  
 else  
     printf "%s was backed up successfully \n\n" $APACHECONFDUMP  
 fi  
 

 #now do the svn conf backup
 SVNCONFDUMP=${opath}/svnconf_$suffix.tar.gz

 tar -zcvf $SVNCONFDUMP $svnconf 2>&1

 if [ -s error ]
 then
        printf "WARNING: An error occured while attempting to backup %s  \n\tEr$
        cat error
        rm -f er
 else
        printf "%s was backed up successfully \n\n" $SVNCONFDUMP
 fi

 #now to do the config backups  
 for (( i = 0; i < ${#repos[@]} ; i++ ))  
 do  
     for (( j = 0 ; j < ${#confdirs[@]} ; j++ ))  
     do  
         CONFDUMP=${opath}/${repos[i]}_${confdirs[j]}_$suffix.tar.gz  
         CONFDIR=${rpath}/${repos[i]}/${confdirs[j]}  
   
         tar -zcvf $CONFDUMP $CONFDIR 2>&1  
   
         if [ -s error ]  
         then  
             printf "WARNING: An error occured while attempting to backup %s \n\tError:\n\t" $CONFDIR  
             cat error  
             rm -f er  
         else  
             printf "%s was backed up successfully \n\n" $CONFDUMP  
         fi  
     done  
 done  
   
   
let's purge our old backups. i keep a week of them.
 
 svn_purgebackups.sh 

 #!/bin/bash  
   
 #this script will run through all nested directories of a parent just killing off all matching files.  
   
 ######  
 ### Set these values  
 ######  
   
 ## default days to retain (override with .RETAIN_RULE in specific directory  
 DEFRETAIN=7  
   
 #want to append the activity to a log? good idea, add its location here  
 LOGFILE=/nfsmount/svn/removed.log  
   
 # enter the distinguishing extension, or portion of the filename here (eg. log, txt, etc.)  
 EXTENSION=gz  
   
   
 #the absolute path of folder to begin purging  
 #this is the top most file to begin the attack, all sub directories contain lowercase letters and periods are game.  
 DIRECTORY=/nfsmount/svn  
   
 #####  
 ##  End user configuartion  
 #####  
   
   
 #this note will remind you that you have a log in case your getting emails from a cron job or something  
 echo see $LOGFILE for details  
   
 #jump to working directory  
 cd $DIRECTORY  
   
 #if your sub-dirs have some crazy characters you may adjust this regex  
 DIRS=`ls | grep ^[a-z.]*$`  
   
   
 TODAY=`date`  
   
 printf "\n\n********************************************\n\tSVN Purge Log for:\n\t" | tee -a $LOGFILE  
 echo $TODAY | tee -a $LOGFILE  
 printf "********************************************\n" $TODAY | tee -a $LOGFILE  
   
 for DIR in $DIRS  
 do  
     pushd $DIR >/dev/null  
     HERE=`pwd`  
     printf "\n\n%s\n" $HERE | tee -a $LOGFILE  
     if [ -f .RETAIN_RULE ]  
     then  
         printf "\tdefault Retain period being overridden\n" | tee -a $LOGFILE  
         read RETAIN < .RETAIN_RULE  
     else  
         RETAIN=$DEFRETAIN  
     fi  
   
     printf "\tpurging files older than %s days\n" ${RETAIN} | tee -a $LOGFILE  
   
     OLDFILES=`find -mtime +${RETAIN} -regex .*${EXTENSION}.*`  
   
     set -- $OLDFILES  
   
    if [ -z $1 ]  
     then  
         printf "\tNo files matching purge criteria\n" | tee -a $LOGFILE  
     else  
         printf "\tDump Files being deleted from $HERE\n" | tee -a $LOGFILE  
         printf "\t\t%s\n" $OLDFILES | tee -a $LOGFILE  
     fi  
   
     rm -f $OLDFILES  
     if [ $? -ne 0 ]  
     then  
         echo "Error while deleting last set" | tee -a $LOGFILE  
         exit 2  
     else  
         printf "\tSuccess\n" | tee -a $LOGFILE  
     fi  
     popd >/dev/null  
 done  
   
   
in priv user crontab, i have these entries:
   
 15 0  * * *  /nfsmount/bin/svn_backup.sh | mail -s "svn hotcopy report" me@there.com 2>&1  
 25 0  * * *  /nfsmount/bin/svn_apacheconf_backup.sh | mail -s "svn apacheconf report" me@there.com 2>&1  
 45 1  * * *  /nfsmount/bin/svn_purgebackups.sh | mail -s "purge archive report" me@there.com 2>&1  
   
   
   

No comments: