Skip to content

Instantly share code, notes, and snippets.

@miend

miend/db-backup.sh

Created Nov 4, 2011
Embed
What would you like to do?
full mysql backups with push to s3
#!/bin/sh
# Dumps all MySQL databases, each to its own file, rotates out old backups.
# set correct dates
NOWDATE=`date +%Y-%m-%d`
LASTDATE=$(date +%Y-%m-%d --date='1 week ago')
# dump each database to its own sql file
for DB in $(echo 'show databases' | mysql -hhostname -uuser -password=password --batch -N)
do
mysqldump -hhostname -uuser --password='password' --quote-names --create-options --force $DB > /backup-directory/$DB.sql
done
# tar all the databases into $NOWDATE-backups.tar.gz
tar -czf /destination-directory/$NOWDATE-backup.tar.gz /backup-directory/
# upload all databases
/usr/bin/s3cmd put /backup-directory/$NOWDATE-backup.tar.gz s3://my_bucket/$NOWDATE/
# rotate out old backups
/usr/bin/s3cmd del --recursive s3://my_bucket/$LASTDATE/
# remove all local dumps
rm /destination-directory/$NOWDATE-backup.tar.gz
for FILE in $(echo $(ls /backup-directory/))
do
rm /backup-directory/$FILE
done
@miend

This comment has been minimized.

Copy link
Owner Author

@miend miend commented Nov 4, 2011

Sorry-- this one isn't exactly clear with its directory names.

Edit: Sorry again for not clarifying use of s3cmd (thanks joshuakraemer).

@joshkraemer

This comment has been minimized.

Copy link

@joshkraemer joshkraemer commented Nov 8, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment