| Login

Search this Blog

Here are the most recent postings to this blog. Use the archive calendar or search to find other entries.
Jul8

Written by:R Teachout
7/8/2010 11:22 AM 

I take no (ok, very little) credit for this script.. It is a combination of a number of scripts I merged together, and tested, to archive a Linux based SVN server.  I use this script to create the backups, and after it runs, it has a “post” script which I have spawning a ncftpput script to ftp it to another internal server.  You could have the post script ssh/sftp it somewhere, or just copy it to a nfs share, whatever you want.

 

Here’s the bash script I run:

#!/bin/bash

# Backup directory location e.g /backups
BACKUPDIR="/backup"
DATEST=`date +%Y-%m-%d_%Hh%Mm`

# Mail setup
# What would you like to be mailed to you?
# - log   : send only log file
# - files : send log file and sql files as attachments (see docs)
# - stdout : will simply output the log to the screen if run manually.
# - quiet : Only send logs if an error occurs to the MAILADDR.
MAILCONTENT="log"
#MAILCONTENT="stdout"

# Set the maximum allowed email size in k. (4000 = approx 5MB email [see docs])
MAXATTSIZE="4000"

HOST=`hostname`

# Email Address to send mail to? (user@domain.com)
MAILADDR="successfullarchiveemailbox@yourdomain.com"
MAILADDRSUPPORT="errorsgoto@domain.com"

# Command run after backups (uncomment to use)
POSTBACKUP="/etc/admin/svn-backup-post"
## THIS IS THE SCRIPT YOU CAN USE TO FTP/SSH THE FILES SOMEWHERE ELSE AFTER
## JUST PUT THE COMMANDS YOU WANT TO RUN IN THE POST FILE

TODAY=`date +%a`                                        # Today's day as string
DATE=`date +%Y-%m-%d_%Hh%Mm`                            # Datestamp e.g 2002-09-21
DOW=`date +%A`                                                  # Day of the week e.g. Monday
DNOW=`date +%u`                                         # Day number of the week 1 to 7 where 1 represents Monday
DOM=`date +%d`                                                  # Date of the Month e.g. 27
M=`date +%B`                                                    # Month e.g January
W=`date +%V`                                                    # Week Number e.g 37
VER=2.5                                                                 # Version Number
# CLEAR PREVIOUS LOGS AND BACKUPS
rm -rf $BACKUPDIR/*
LOGFILE=$BACKUPDIR/LOG_`hostname`-`date +%N`.log          # Logfile Name
LOGERR=$BACKUPDIR/ERRORS_`hostname`-`date +%N`.log            # Logfile Name
BACKUPFILES=""

# IO redirection for logging.
touch $LOGFILE
exec 6>&1           # Link file descriptor #6 with stdout.
                    # Saves stdout.
exec > $LOGFILE     # stdout replaced with file $LOGFILE.
touch $LOGERR
exec 7>&2           # Link file descriptor #7 with stderr.
                    # Saves stderr.
exec 2> $LOGERR     # stderr replaced with file $LOGERR.

echo "# REMINDER:  THE REPOSITORIES MUST BE CREATED BEFORE LOADING.. 
  echo "----------------------------------------------------------"
  echo "----------------------------------------------------------"
cd /var/lib/subversion/repositories/

## Note here.. none of our SVN databases have the words “backup” or “lost+found” in them.. if yours do, revise this
## If someone knows of a better way to do this, please let me know in the comments –
## this was just a down and dirty way i could do it quickly!

for i in `ls | grep -v backup| grep -v lost+found`
do
  echo ""
  echo Backing up SVN Repository /var/lib/subversion/repositories/$i to $BACKUPDIR/$i.repo.dump
  svnadmin dump /var/lib/subversion/repositories/$i > $BACKUPDIR/$i.repo.dump  2>&1
  echo Compressing $i Repository Backup as $i.repo.dump.$DATEST.tar.gz
  tar -zcvf $BACKUPDIR/$i.repo.dump.$DATEST.tar.gz $BACKUPDIR/$i.repo.dump --remove-files  2>&1
  echo Adding Restore Routines to a restore Script for repository $i
  echo "tar zxvfp $BACKUPDIR/$i.repo.dump.$DATEST.tar.gz"589132 >> $BACKUPDIR/restorescript.sh.txt
  echo "svnadmin load /var/lib/subversion/repositories/$i < $BACKUPDIR/$i.repo.dump" >> $BACKUPDIR/restorescript.sh.txt
  echo ""
  echo "----------------------------------------------------------"
done

echo Backup Completed.
echo .

echo Total disk space used for backup storage..
echo Size - Location
echo `du -hs "$BACKUPDIR/"`
echo
echo ======================================================================

# Run command when we're done
if [ "$POSTBACKUP" ]
        then
        echo ======================================================================
        echo "Postbackup command output."
        echo
        eval $POSTBACKUP
        echo
        echo ======================================================================
fi

#Clean up IO redirection
exec 1>&6 6>&-      # Restore stdout and close file descriptor #6.
exec 1>&7 7>&-      # Restore stdout and close file descriptor #7.

if [ "$MAILCONTENT" = "files" ]
then
        if [ -s "$LOGERR" ]
        then
                # Include error log if is larger than zero.
                BACKUPFILES="$BACKUPFILES $LOGERR"
                ERRORNOTE="WARNING: Error Reported - "
        fi
        #Get backup size
        ATTSIZE=`du -c $BACKUPFILES | grep "[[:digit:][:space:]]total$" |sed s/\s*total//`
        if [ $MAXATTSIZE -ge $ATTSIZE ]
        then
                BACKUPFILES=`echo "$BACKUPFILES" | sed -e "s# # -a #g"` #enable multiple attachments
                mutt -s "$ERRORNOTE SVN Backup Log Files for $HOST - $DATE" $BACKUPFILES $MAILADDRSUPPORT < $LOGFILE            #send via mutt
        else
                cat "$LOGFILE" | mail -s "WARNING! - SVN Backup exceeds set maximum attachment size on $HOST - $DATE" $MAILADDRSUPPORT
        fi

elif [ "$MAILCONTENT" = "log" ]
then
        cat "$LOGFILE" | mail -s "SVN Backup Log for $HOST - $DATE" $MAILADDR
        if [ -s "$LOGERR" ]
                then
                        cat "$LOGERR" | mail -s "ERRORS REPORTED: SVN Backup error Log for $HOST - $DATE" $MAILADDRSUPPORT
        fi
elif [ "$MAILCONTENT" = "quiet" ]
then
        if [ -s "$LOGERR" ]
                then
                        cat "$LOGERR" | mail -s "ERRORS REPORTED: SVN Backup error Log for $HOST - $DATE" $MAILADDRSUPPORT
                        cat "$LOGFILE" | mail -s "SVN Backup Log for $HOST - $DATE" $MAILADDR
        fi
else
        if [ -s "$LOGERR" ]
                then
                        cat "$LOGFILE"
                        echo
                        echo "###### WARNING ######"
                        echo "Errors reported during SVNBackup execution.. Backup failed"
                        echo "Error log below.."
                        cat "$LOGERR"
        else
                cat "$LOGFILE"
        fi
fi

if [ -s "$LOGERR" ]
        then
                STATUS=1
        else
                STATUS=0
fi

# Clean up Logfile
#eval rm -f "$LOGFILE"
#eval rm -f "$LOGERR"
exit $STATUS

Tags:

Your name:
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Security Code
CAPTCHA image
Enter the code shown above in the box below
Add Comment  Cancel 

If a problem can be solved, there's no use worrying about it. If it can't be solved, worrying will do no good.
-Unknown

Inspired by Nina