Automate RDS DB backup and store to S3
Use case: Perform daily backup of DB and need to store at S3
To perform the above task We will use shell script and AWS cli to automate the DB backup and copy to S3 bucket
Create a IAM user, which will have put, get object permissions or else give full bucket permissions
configure AWS IAM user using below command:
aws configure
create a shell script file
$vim perform_DB_backup.sh
enter the below content into the file
#!/bin/bash
set -e
DATE=`date ‘+%Y-%m-%d-%H’`
MONTH=`date ‘+%m’`
HOUR=`date ‘+%H’`
YEAR=`date ‘+%Y’`
FILE=”RDSDB_backup_$DATE.sql”
username=<DB_user>
password=<password>
dbhost=<db_host>
dbname=<db_name>
echo “performing DB backup at /tmp directory”
mysqldump -u$username -p$password -h$dbhost $dbname > /tmp/$FILE
status=$?
echo “$status”
if [ $status -eq 0 ]
then
aws s3 cp /tmp/$FILE s3://<destination_bucket_name>/<directory_path>/$YEAR/$MONTH/
else
echo “db back has failed”
fi
setup a cron job for the above, it will automate and push the files to S3 bucket on regular basis