Skip to content

Instantly share code, notes, and snippets.

@dalethedeveloper
Created June 23, 2011 16:35
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dalethedeveloper/1042934 to your computer and use it in GitHub Desktop.
Save dalethedeveloper/1042934 to your computer and use it in GitHub Desktop.
Backup bash script for Webserver DB + Site Files
#!/bin/bash
# http://www.ivorde.ro/How_to_backup_all_mysql_databases_with_one_command-51.html
# http://bash.cyberciti.biz/backup/backup-mysql-database-server-2/
# http://sgowtham.net/blog/2008/04/04/backing-up-and-restoring-mysql-databases/
# Requires: http://s3tools.org/s3cmd
MyUSER="YOUR-USERNAME"
MyPASS="YOUR-PASSWORD"
MyHOST="localhost"
S3BUCKET="s3://backup/"
S3UTIL="$(which s3cmd) --acl-private"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
SMOOSH="$(which tar) cjf"
HOSTNAME="$(hostname)"
DEST="/tmp/mysqlbackup"
[ ! -d $DEST ] && mkdir -p $DEST
cd $DEST
# MySQL DB Backup
DBS="$($MYSQL -u $MyUSER -h $MyHOST -p$MyPASS -NBse 'SHOW DATABASES'|grep -v 'information_schema')"
DB_FILES=()
for db in $DBS
do
FILE="$DEST/${db}.sql"
$MYSQLDUMP -u $MyUSER -h $MyHOST -p$MyPASS $db > $FILE
DB_FILES+=(${FILE})
done
FILE="${HOSTNAME}_db.bz2"
$SMOOSH $FILE ${DB_FILES[@]}
$S3UTIL put $FILE $S3BUCKET
rm $FILE
rm -rf ${DB_FILES[@]}
# File Backups
BACKUPS=( /var/www/wordpress/wp-content/* /etc/apache2/apache2.conf )
FILE="${HOSTNAME}_sites.bz2"
$SMOOSH $FILE ${BACKUPS[@]}
$S3UTIL put $FILE $S3BUCKET
rm $FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment