Amazon S3 Backup Scripts
This is a collection of Amazon S3 backup scripts for unix-based systems. The scripts make use of s3cmd (website) to backup files and databases to S3.
The s3-mysql-backup.sh script performs the following operations each time you run it:
- Checks, repairs, and optimizes each MySQL database.
- Dumps each database into its own .sql file.
- Tars all of the .sql files into a directory on the server.
- Uploads the tarred file to S3 and deletes the tarred file from the local server.
- Deletes backups older than 7 days from the S3 bucket.
Setup
- Install s3cmd. See s3tools.org/repositories for instructions.
- Configure s3cmd to work with your AWS account:
s3cmd --configure
. You can access or generate your AWS security credentials here. - Create an S3 bucket:
s3cmd mb s3://my-database-backups
. - Put the *.sh backup script somewhere on your server (ie -
/root/scripts
). - Give the *.sh backup script 755 permissions:
chmod 755 /root/scripts/backups/s3-mysql-backup.sh
. - Edit the variables near the top of backup script to match your bucket, directory, and MySQL authentication.
Example Cron Usage
-
Edit your crontab:
crontab -e
. -
Add the following line to your crontab. This will execute the backup script at 2am every day and will email you the results of the run.
0 2 * * * /path/to/your/script.sh |mail -s "Backup Cron Output" -c email@exmaple.com