Skip to content

Instantly share code, notes, and snippets.

@miend
Created November 4, 2011 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miend/1340421 to your computer and use it in GitHub Desktop.
Save miend/1340421 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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
Copy link

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