Shell Script to take MySQL Database Dump and Push It to AWS S3

By | October 11, 2019

There will a multiple cases when you will be asked to Automate the backup of mysl dump and store somewhere. there will be a multiple way of doing this but in this article we will see how we can take the msql dump of a database and push it to the AWS S3 bucket.
Just to make Sure that the backup is successful we will be using the slack for receiving the notification regarding the backup completion. We will be using the shell Script to Automate this process. 

Prerequisite,

  1. Need an AWS Account.
  2. Server With attached Role or AWS Cli Configured.
  3. Slack Account with incoming webook for channel.
  4. Existing database, who’s backup need to be taken.

Create a bucket in aws s3 which you will be using to store the mysql db Dump. Below Shell Script Will perform below things,

  1. Take the Backup of mysql dump on the Server with proper timestamp
  2. Push the mysql to S3 on defined bucket name
  3. delete the dump file from the server
  4. Push Notification to Slack saying backup is done.
  5. generate proper log to troubleshoot in case of failure.

Note: Script will automatically create the required directories if not present at the path. 
Make Sure you have configured the CLI or Attached role to the Server with required permission and also created the required bucket.
Copy the below Script and paste in a file called say, db-dump.sh

# !/bin/bash
 This script will take the backup of a mysql database and store dumps to the
 /tmp/lab_dumps directory and push to devopsagebucket s3 bucket.
 Database connection details
 db_host="localhost"   ## Replace the Database endpoint here
 db_user="wp-user"      ## replace with your database user
 db_pass="secure-password" ## Replace with your daabase password
 db_name="wordpress"  ## Replace with your database name
 Dump location on server and other variables
 server_dump_location="/tmp/mysql-dump/"
 log_path="/tmp/script_log/"
 log_location="/tmp/script_log/db_dump.log"
 s3_bucket="s3://devopsagebucket"
 time_stamp="$(date +"%d-%b-%Y-%H_%M_%S")"
 Check If diretory is present or not, If not then create it.
 echo "##################################################################" >> $log_location
 if [ -d $server_dump_location ]; then
     echo "Directory Alredy Exists" >> $log_location
 elif [ -d $log_path ]; then
     echo "Log Directory Alredy Exists" >> $log_location
 else
     mkdir $server_dump_location $log_path
     echo "Directory Was not there, hence created $server_dump_location and $log_path" >> $log_location
 fi
 Slack Incoming Web hook for db_dump channel
 slack_url=https://hooks.slack.com/services/TKKRQR12B/XXXXXXXXXXXXXXXXXXXXXXXXXXXXX  ## Replace you Slack Incoming hook here.
 Server backup initialization
 echo "Taking the backup of sample wordpress database, Started at: $time_stamp" >> $log_location
 mysqldump -h $db_host -u $db_user -p$db_pass $db_name > $server_dump_location/$db_name-$time_stamp.sql
 if [ $? -eq 0 ]; then
     echo "Backup Successfully Done" >> $log_location
 else
     echo "Backup Failed, Please check" >> $log_location
     exit 1
 fi
 echo "Backup Completed at: $time_stamp" >> $log_location
 Push Dump to S3 bucket.
 echo "Pushing test wordpress db dump to S3 at $time_stamp" >> $log_location
 aws s3 cp $server_dump_location $s3_bucket --recursive
 echo "Moved mysql dump to S3 at $time_stamp" >> $log_location
 echo "#################################################################" >> $log_location
 Delete the Mysql Dump from the Server
 sudo rm -rf $server_dump_location/$db_name-*
 Or, # Clear Dumps from the Server Older than 1 Weeks.
 find $server_dump_location/* -mtime +7 -exec rm {} \;
 Notification to Slack
 curl -X POST -H 'Content-type: application/json' --data '{"text":"'"Backup of Sample WordPress database has completed at $time_stamp"'"}' $slack_url
 complete command
 curl -X POST -H 'Content-type: application/json' --data '{"text":"'"Message $time_stamp"'"}' https://hooks.slack.com/services/xxxxxx/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 

Note: Don’t Forget to change your database details at the top of script before executing it. Please find the same script in devopsage official git repository.

Once You run this script, the backup will be taken and pushed to the S3.
Also, you will get a Notification on Slack Channel as shown in the Screenshot below.

Verify the S3 Bucket and also check logs on the Server.

Disclaimer: This Script does not comes with any guaranty, please go through it carefully before executing it. DevOpsAGE Will not be responsible for any loss of data or any Issues happened 

Thats It for this Post!!


If you Like Our Content here at Devopsage, then please support us by sharing this post.

Please Like and follow us at, LinkedInFacebookTwitter, and GitHub

Also, Please comment on the post with your views and let us know if any changes need to be done.
Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *