Automate MySQLdump Backup from server(backend) to Amazon S3
Please follow the below steps to setup backup of mysql database from server to Amazon s3.
- Login to AWS console, create IAM user, download the credentials and attach AmazonS3Full access policy to the IAM user.
- Install AWS CLI and configure by following the below commands.
apt install awscli -y
aws configure - It will ask for Access Id and secret key. Provide the same which we downloaded in step-1 and proceed.
- Verify AWS S3 list by following the below command.
aws s3 ls - Install S3fs and Mount S3 Bucket by following the below commands.
apt install s3fs
echo ACCESS_KEY_ID:SECRET_ACCESS_KEY > /home/ubuntu/.s3fs- creds - Your file will be saved at /home/ubuntu/.s3fs-creds.
- chmod 600 /home/ubuntu/.s3fs-creds
- Create a new folder from your home directory.
mkdir /home/ubuntu/s3_uploads - Create an Automation Backup Script for MySQLdump
vim /home/ubuntu/.config.cnf - Add the following below:
[client]
user = dbuser
password = dbpassword
host = localhost
Save and close the file. - create an automation backup script for MySQLdump
vim /home/ubuntu/mysqldump.sh - Add the following script
!/bin/bash
DBNAME=yourdbname
BACKUP=/home/ubuntu/s3_uploads
Backup date
date=$(date +’%Y%m%d%H%M%S’);
Start exec backup..
mysqldump –defaults-extra-file=/home/ubuntu/.config.cnf $DBNAME > $BACKUP/mysqldump_$date.sql - Save and close the file.
- To make sure the file is executable, run the below command.
chmod +x /home/ubuntu/mysqldump.sh - To automate your backup, use to command below to assign the mysql dump script on cron scheduling jobs.
crontab -e - If your first time to open the crontab, you can choose the vim editor on the screen and then add the following command to the bottom line.
@daily sh /home/ubuntu/mysqldump.sh - Save and close the file.
- Testing MySQLdump to S3 Bucket
- Go to the directory where mysqldump.sh is located and run the script.
./mysqldump.sh - After the backups completes, open the mounted
s3_uploads
folder and type the below command.
ls -lah - On the screen, you can see the files which were uploaded.
- Verify the files on S3 Bucket Console.