TwitterCounter for @bigclick_dean

dBlog.com.au

My Development Blog

Posts Tagged ‘ Apache ’

While setting up some dedicated servers at the office I needed a quick and easy way to backup each server to an offsite location. After looking around for a while and trying some of the existing S3 backup scripts I couldn’t find anything that satisfied my needs (Apache htdocs & MySQL databases).

So after playing around a bit I decided to write up my own quick and dirty bash script. It doesn’t do incremental backups, rotation or any of that fancy stuff but it does everything I needed and I may expand on it later to remove any backups more than 30 days old but for now I will just delete them manually.

After scrounging around a bit I decided to go with the s3cmd command line tool to provide the access to the S3 service. If you use debian you can simply install it from the default repositories by running “apt-get install s3cmd”

You will need to configure s3cmd before using this script otherwise your backup transfers will fail. Follow the instructions over at http://s3tools.org/s3cmd

Now onto the script!

#!/bin/bash
unset PATH

# S3 VARIABLES
S3BUCKET=my-backups # S3 Bucket name

# LOCATION TO STORE BACKUPS WHILE PROCESSING
BACKUPDIR=./backup

# MYSQL DETAILS
MYSQLUSER=root
MYSQLPWD=password
MYSQLHOST=localhost

# APACHE FILES TO BACKUP
HTDOCS=/var/www

# PATH VARIABLES
MK=/bin/mkdir;
TAR=/bin/tar;
GZ=/bin/gzip;
RM=/bin/rm;
GREP=/bin/grep;
MYSQL=/usr/bin/mysql;
MYSQLDUMP=/usr/bin/mysqldump;
DATE=/bin/date;
FIND=/usr/bin/find;
S3CMD=/usr/bin/s3cmd

# OTHER VARIABLES
NOW=$($DATE +_%b_%d_%y);

#REMOVE EXISTING BACKUP DIRECTORY
$RM -Rf $BACKUPDIR$NOW

# Create new backup dir
$MK $BACKUPDIR$NOW

# FILE BACKUP
for i in $($FIND $HTDOCS/* -maxdepth 0 -type d -printf '%f\n'); do
   $TAR -czf $BACKUPDIR$NOW/httpdocs_$i.tar.gz $HTDOCS/$i;
done

# MYSQL BACKUP
for i in $(echo 'SHOW DATABASES;' | $MYSQL -u$MYSQLUSER -p$MYSQLPWD -h$MYSQLHOST|$GREP -v '^Database$'); do
  $MYSQLDUMP                                                    \
  -u$MYSQLUSER -p$MYSQLPWD -h$MYSQLHOST                         \
  -Q -c -C --add-drop-table --add-locks --quick --lock-tables   \
  $i | $GZ -9 > $BACKUPDIR$NOW/mysql_$i.sql.gz
done;

# ARCHIVE ALL FILES FROM THIS BACKUP
$TAR -czvf fullbackup$NOW.tar.gz $BACKUPDIR$NOW
$RM -Rf $BACKUPDIR$NOW

# UPLOAD THE BACKUP TO S3
$S3CMD put fullbackup$NOW.tar.gz s3://$S3BUCKET/fullbackup$NOW.tar.gz
$RM -Rf fullbackup$NOW.tar.gz

Simply put the above contents into a file (e.g. /root/s3backup.sh) and chmod it to 755.

Now to give it a test just execute the file s3backup.sh from the command line and check the output for errors and check your S3 account to make sure the file has arrived.

I know it’s not elegant, it’s not anything special but it does exactly what I need and I couldn’t find anything that did MySQL databases and Apache htdocs in the one foul swoop.

Good luck and any suggestions would be greatly appreciated.

Popularity: 1% [?]