Backup MySQL Database automatically to Amazon S3
Amazon S3 services is a wonderful fast and good service to backup your databases. This script dumps currently only one, database and uploads the file to a s3 bucket.
0. Register and prepare AWS S3
- Make your self an Amazon AWS account if you dont already have one. 2.Create a new bucket for your backups (https://console.aws.amazon.com/s3/home).
- (optional) Maybe you want also create a lifecycle policy for example to delete all backups older than 30 days or so. Just edit the properties of the bucket.
- Create a new user in the IAM an give him the policy AmazonS3FullAccess.
- Remember to download the access key and the secret key, you will need them soon.
1. Install s3cmd
This are the instructions for Ubuntu, for other distributions they may vary.
# Install s3cmd sudo apt-get -y install python-setuptools wget http://netix.dl.sourceforge.net/project/s3tools/s3cmd/1.6.1/s3cmd-1.6.1.tar.gz tar xvfz s3cmd-1.6.1.tar.gz cd s3cmd-1.6.1 sudo python setup.py install # Configure s3cm (have your access keys ready!) s3cmd --configure
2. Add this script
Upload a copy of s3backup.sh (make sure you edit the configurations in the first lines), make it executable and test it
# Add the executable bit chmod +x s3backup.sh # Run the script to make sure it's all working flawless ./s3backup.sh
3. Set up a automatic backup every night
Edit the crontab (if you first start crontab it will ask you to choose an editor, just use nano it is the best)
crontab -e # Add the following lines: # Run the database backup script at 3am every night 0 3 * * * bash /var/www/s3backup.sh >/dev/null 2>&1
4. Now go sleep without being scared of data loss. ;)
If you have found an error or have an idea to improve the script feel free to comment or make a fork. And please please give me a star.