Last active
October 1, 2018 11:20
-
-
Save kheengz/4ccfb5c40da0fc333ca924ee9b0e3580 to your computer and use it in GitHub Desktop.
Auto Backup DB to AWS S3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. Install pip > See http://www.saltycrane.com/blog/2010/02/how-install-pip-ubuntu/ | |
2. Install AWS CLI > use: `$ pip install awscli` | |
3. Go to AWS Console to create a IAM user and download the credentials (Access Key ID and Secret Access Key) | |
4. Run `$ aws configure` and follow the prompts | |
*NB: If you get a permission denied error while running *4* : run `$ chmod u+x /usr/bin/aws`* | |
5. Create S3 bucket on AWS | |
6. Copy and modify the following commands into a file called `mysql_backup.sh` and using the folder structure 7. below. | |
```sh | |
#!/usr/bin/env bash | |
cd /backups | |
BACKUP_FILENAME=./databases/NAME_OF_FILE_TO_SAVE_$(date +%s%3N).sql | |
mysqldump -u YOUR_USERNAME -pYOUR_PASSWORD YOUR_DATABASE > $BACKUP_FILENAME | |
aws s3 sync ./databases s3://s3-bucket-name | |
rm $BACKUP_FILENAME | |
``` | |
*NB: Your password characters shouldnt contain bash special characters* | |
7. Directory structure should be similar to the below. Otherwise, edit the script above accordingly | |
``` | |
-- backups [folder] | |
---- mysql_backup.sh | |
---- databases [folder] | |
``` | |
Also, create the log file by running `$ touch /var/logs/mysql_backup.log` | |
8. Set up a cron job to back up the database every 30 minutes. | |
``` | |
$ crontab -e | |
$ */30 * * * * PATH=/sbin:/usr/bin:/bin:/usr/local/bin /backups/mysql_backup.sh >> /var/log/mysql_backup.log 2>& | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment