Skip to content

Instantly share code, notes, and snippets.

@brock
Created September 20, 2012 01:03
Show Gist options
  • Save brock/3753359 to your computer and use it in GitHub Desktop.
Save brock/3753359 to your computer and use it in GitHub Desktop.
Backup All DB's and transfer to S3 with s3cmd
#!/bin/bash
BACKUP_DIR="~/db_backups"
DBUSER=""
DBPASSWORD=""
BUCKET=""
FROM='"Backups" <backups@localhost>'
TO='"Admin" <admin@localhost>'
SUBJECT='Backup Log'
NEW_FOLDER=$(date +"%m-%d-%y")/$(date +"%s")
TIME=$(date +"%s")
TMPFILE="$(mktemp)"
echo "From: $FROM" > $TMPFILE
echo "To: $TO" >> $TMPFILE
echo "Subject: $SUBJECT" >> $TMPFILE
echo "MIME-Version: 1.0" >> $TMPFILE
echo "Content-Type: text/plain" >> $TMPFILE
if [ ! -d $BACKUP_DIR/$NEW_FOLDER ]; then
mkdir -p $BACKUP_DIR/$NEW_FOLDER
fi;
for DB in $(mysql --user=$DBUSER --password=$DBPASSWORD -e 'show databases' -s --skip-column-names); do
if [[ $DB != *schema* && $DB != *mysql* ]]; then
FILE="$DB.$TIME.sql.gz"
mysqldump --user=$DBUSER --password=$DBPASSWORD --databases $DB | gzip -9 > $BACKUP_DIR/$NEW_FOLDER/$FILE;
echo "************* $DB *************" >> $TMPFILE
echo -e "DB $FILE Exported" >> $TMPFILE
s3cmd put $BACKUP_DIR/$NEW_FOLDER/$FILE s3://$BUCKET/$NEW_FOLDER/$FILE >> $TMPFILE
echo -e "DB $FILE Transferred to S3\r\n" >> $TMPFILE
fi
done
cat $TMPFILE | /usr/sbin/sendmail -i -t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment