Backing up Data

=Intro=

This is a very useful and easy script I quickly wrote up. Use it to backup your server, even your local Linux laptop or desktop by just changing a few lines. It currently backs up your /svn, /trac MySQL, and Apache areas. Just see what i did and add your /home/ folder too, or anything else.

=Features=


 * Ability to choose to backup Apache, MySQL, SVN, Trac areas
 * Ability to add your own area to be backed up
 * Ability to compress with Gzip, Bzip2
 * Ability to upload backups to external locations - FTP, SFTP, NFS, etc..

=Directions=


 * Add the following line to your root's (or backup user's) crontab using sudo crontab -e

0 0 * * * sh /root/backup_all

This will execute it every midnight at 12:00am.


 * Next, copy and save the bottom script as backup_all into your root's (or backup user's) home folder and chmod a+x backup_all to make it executable.

That's it. Now, every night at midnight, it will backup the locations you want.

=Script=


 * 1) !/bin/bash


 * 1) Backup All - (c) Samir Greadly - xushi.xushi (@) gmail.com
 * 2) Build 20070703
 * 3) 20070930
 * 4) * Fixed wrong path of svn destination backup
 * 5) * Added the backing up of TRAC
 * 6) 20070703
 * 7) fixed mysql error line that always died no matter what...
 * 8) TODO
 * 9) * Compress the whole /backup folder when you want to upload it somewhere ?
 * 10) * Add MD5 sums ?
 * 11) * either install a slave mysql server or remove the line.
 * 1) TODO
 * 2) * Compress the whole /backup folder when you want to upload it somewhere ?
 * 3) * Add MD5 sums ?
 * 4) * either install a slave mysql server or remove the line.
 * 1) TODO
 * 2) * Compress the whole /backup folder when you want to upload it somewhere ?
 * 3) * Add MD5 sums ?
 * 4) * either install a slave mysql server or remove the line.
 * 1) * either install a slave mysql server or remove the line.


 * 1)         Basic Settings. Update what you need.
 * 1)         Basic Settings. Update what you need.

BACKUP=""
 * 1) Any extra locations you want to backup ?
 * 2) MAKE SURE you don't put an ending slash
 * 3) /home/user = YES...    /home/user/ = NO...

DESTINATION="/backup/"
 * 1) local backup area for it to store all the data

KEEPBACKUP=30
 * 1) How many backup days do you want to keep?

BACKUPAPACHE=1 APACHELOCATION="/var/www/html"
 * 1) do you want apache backups? (1=yes, 0=no)
 * 1) what's the location of your html files?

BACKUPMYSQL=1 MYSQLUSER=root MYSQLPASSWORD=ur2b69s00nmy5ql
 * 1) do you want mysql backups ? (1=yes, 0=no)
 * 1) if yes, what is the admin username/password for mysql?
 * 1) MYSQLPASSWORD=root

BACKUPSVN=1 SVNLOCATION="/svn"
 * 1) do you want SVN backups ? (1=yes, 0=no)
 * 1) if yes, where are the repositories located?

BACKUPTRAC=1 TRACLOCATION="/trac"
 * 1) do you want TRAC backups ? (1=yes, 0=no)
 * 1) if yes, where are the environments located?


 * 1)    Advanced Settings. Leave default unless you want specifics
 * 1)    Advanced Settings. Leave default unless you want specifics

# some binaries.. populate/replace if desired MYSQL="$(which mysql)" MYSQLDUMP="$(which mysqldump)" WPUT="/opt/wput/wput" # the date DATE=`date +%F` # do you want mysql to backup all databases into 1 file # or each database in its own file? # 0 = Many files, 1 = One file. MYSQLONEFILE=0 # do you want apache to backup all directories into 1 file # or each directory in its own file? # 0 = Many files, 1 = One file. APACHEONEFILE=0 # do you want SVN to backup all repositories into 1 file # or each repo in its own file? # 0 = Many files, 1 = One file. SVNONEFILE=0

# do you want TRAC to backup all environments into 1 file # or each repo in its own file? # 0 = Many files, 1 = One file. TRACONEFILE=0 # Do you want compression? # 0 = no, 1 = yes # TODO: remove this and merge action with COMPRESSTYPE DOCOMPRESS=1 # If yes, which type? # 1 = bzip2 # 2 = gzip2 # TODO: make COMPRESSTYPE=0 assigned to off. COMPRESSTYPE=1 # do you want to upload the backups somewhere? UPLOADSOMEWHERE=1 # If yes, the final destination, ftp, sftp, nfs, etc... # examples: # HELL=ftp://username:password@mysite.com/backup/ # HELL=sftp://username:password@mysite.com/backup/ # HELL=/mnt/nfs/backup/ HELL=ftp://username:password@yourbackupdomain.com/path/to/destination/
 * 1)  DO NOT EDIT UNDER THIS LINE UNDER PENALTY OF CHEMICAL CASTRATION
 * 1)  DO NOT EDIT UNDER THIS LINE UNDER PENALTY OF CHEMICAL CASTRATION

TARGET=$DESTINATION$DATE/
 * 1) Set the target directory + date

if $DOCOMPRESS = 1 then # depending on the case, select the options case $COMPRESSTYPE in 		1 )			COMPRESS='tar -cjf '			EXTENSION='tar.bz2'		;;		2 ) COMPRESS='tar -czf ' EXTENSION='tar.gz' ;;		# add more later.. like zip, rar, etc.. esac else # default, if no compression COMPRESS='tar -cvf ' EXTENSION='tar' fi
 * 1) check which compression you need if any

if -d "$TARGET"  ; then echo "Backup destination date $TARGET exists... stopping." exit 1 fi
 * 1) check if that directory exists and stop if yes

echo "Creating directory: $TARGET" mkdir $TARGET
 * 1) create the target dir

if $BACKUPAPACHE = 1 then # create an apache folder to store all the backups in it	mkdir "$TARGET"apache if $APACHEONEFILE = 1 then # tar all the folders into 1 file. CD=$(pwd) for i in $APACHELOCATION do cd $i/../ FILENAME=${i##*/} #echo Backing up $i to "$TARGET"apache/"$FILENAME"-"$DATE"."$EXTENSION" $COMPRESS "$TARGET"apache/"$FILENAME"-"$DATE"."$EXTENSION" $FILENAME done echo "Done." cd ${CD} else # tar all the folders into their own files. CD=$(pwd) for i in $APACHELOCATION do cd $i/ FILENAME=${i##*/} # get a list of all the files/folders APACHELIST="$(ls $APACHELOCATION)" for list in $APACHELIST do #echo Backing up $list to "$TARGET"apache/"$list"-"$DATE".tar.bz2 $COMPRESS "$TARGET"apache/"$list"-"$DATE"."$EXTENSION" $list done done echo "Done." cd ${CD} fi fi

if $BACKUPMYSQL = 1 then

if $MYSQLONEFILE = 1 then

#backup all databases into just 1 file. #stop the slave mysqladmin -u "$MYSQLUSER" -p"$MYSQLPASSWORD" stop-slave # backup the database mysqldump --all-databases -u "$MYSQLUSER" -p"$MYSQLPASSWORD" --lock-all-tables > "$TARGET"backup-mysql-"$DATE".sql # restart the slave so it catches up on everything that happened on the master # in the meantime so that no data is lost mysqladmin -u "$MYSQLUSER" -p"$MYSQLPASSWORD" start-slave # compress the resulting sql. bzip2 "$TARGET"backup-mysql-"$DATE".sql else # create a mysql/ folder to store all the backups in it		mkdir "$TARGET"mysql #stop the slave mysqladmin -u "$MYSQLUSER" -p"$MYSQLPASSWORD" stop-slave #for i in `mysql -B -e "show databases" | tail +2`; do 			#	mysqldump $i | gzip -9 > /var/backups/mysql/$i.`date --iso-8601`.gz 			#	ls -t /var/backups/mysql/$i.* | tail +7 | xargs rm -f #done

# get a list of all the databases DBLIST="$($MYSQL -u "$MYSQLUSER" -p"$MYSQLPASSWORD" -Bse 'show databases')" for db in $DBLIST do [ "$db" == "$i" ] # do all inone job in pipe, $MYSQLDUMP -u "$MYSQLUSER" -p"$MYSQLPASSWORD" $db > "$TARGET"mysql/$db-"$DATE".sql done # restart the slave so it catches up on everything that happened on the master # in the meantime so that no data is lost mysqladmin -u "$MYSQLUSER" -p"$MYSQLPASSWORD" start-slave # compress the resulting sql files. bzip2 "$TARGET"mysql/*.sql || { echo "Creating bzip sqls failed"; exit 1; } fi fi

if $BACKUPSVN = 1 then # create an SVN folder to store all the backups in it	mkdir "$TARGET"svn if $SVNONEFILE = 1 then # tar all the folders into 1 file. CD=$(pwd) for i in $SVNLOCATION do cd $i/../ FILENAME=${i##*/} echo Backing up $i to "$TARGET"svn/"$FILENAME"-"$DATE"."$EXTENSION" $COMPRESS "$TARGET"svn/"$FILENAME"-"$DATE"."$EXTENSION" $FILENAME done echo "SVN Backup Done." cd ${CD} else # tar all the folders into their own files. CD=$(pwd) for i in $SVNLOCATION do cd $i/ FILENAME=${i##*/} # get a list of all the files/folders SVNLIST="$(ls $SVNLOCATION)" for list in $SVNLIST do #echo Backing up $list to "$TARGET"svn/"$list"-"$DATE".tar.bz2 $COMPRESS "$TARGET"svn/"$list"-"$DATE"."$EXTENSION" $list done done echo "SVN Backup Done." cd ${CD} fi fi
 * 1) backup SVN

if $BACKUPTRAC = 1 then # create a TRAC folder to store all the backups in it	mkdir "$TARGET"trac if $TRACONEFILE = 1 then # tar all the folders into 1 file. CD=$(pwd) for i in $TRACLOCATION do cd $i/../ FILENAME=${i##*/} echo Backing up $i to "$TARGET"trac/"$FILENAME"-"$DATE"."$EXTENSION" $COMPRESS "$TARGET"trac/"$FILENAME"-"$DATE"."$EXTENSION" $FILENAME done echo "TRAC Backup Done." cd ${CD} else # tar all the folders into their own files. CD=$(pwd) for i in $TRACLOCATION do cd $i/ FILENAME=${i##*/} # get a list of all the files/folders TRACLIST="$(ls $TRACLOCATION)" for list in $TRACLIST do #echo Backing up $list to "$TARGET"trac/"$list"-"$DATE".tar.bz2 $COMPRESS "$TARGET"trac/"$list"-"$DATE"."$EXTENSION" $list done done echo "Trac Backup Done." cd ${CD} fi fi
 * 1) backup TRAC

echo "Cleaning up..."
 * 1) delete any old temp files

OLDBACKUPS=`find "$DESTINATION"/20* -maxdepth 0 -mtime +"$KEEPBACKUP"` for DIR in $OLDBACKUPS do echo Removing old backup $DIR rm -fr $DIR done

if $UPLOADSOMEWHERE = 1 then #finally, sftp it somewhere # TODO: put switch cases to see which method of upload user wants. echo "uploading all of the contents" echo $DESTINATION$DATE $WPUT "${HELL}" $DESTINATION$DATE fi
 * 1) finally, if user wants external backup, upload it..

echo "Backup Complete..."