How to periodically Backup your website and MySQL database to local server as well as S3 bucket using Bash script (video)

Read Time: 4 mins

You might have gone through situation where you need simple script to backup your website and database server. In this article, I will explain how you can periodically Backup your MySQL database and website to local server as well as S3 bucket using Bash script. The reason I am writing the backup script in bash is that, bash is flexible to handle and we don’t need any other programming language where bash excels.

Video Demonstration:

This is a video demonstration of this article. Please Subscribe , like  and comment  🙂

Subscribe EasyAsLinux channel  

Step1: Let’s write the bash script:

Let’s write the backup script. You don’t have to write one, I have written one for you 🙂 . create a file  in your home directory and save the code given below(choose the one as per your need). Generally this script create tar of your website directory and database dump. The location of the website content and database details should be given to variables webapp_path  and database_* respectively. The backups are created at directory mentioned by variable backup_dir  with retention period retention_days .

Backup your website and MySQL database to local server only:

This script backups your website and database to local server only, not to s3 bucket.


Backup your website and MySQL database to local server as well as s3 bucket:

This script backup your website and database to local server as well as s3 bucket. You need to configure awscli  on your server before running this script. Learn how you can configure AWSCLI from here >>

Mention the S3 bucket name in variable s3_bucket_name  where you want to backup the data.


Step2: Set a cronjob to run the bash script:

So we have the script ready. Let’s run it daily by setting cron job. Run command crontab -e  and append line shown below.

Here we have scheduled the cronjob to run the script daily at 1AM. The script logs both stdout and stderr at   /path_to_the_log/backup.log so that you can check the file incase of any failure.

Ideally the log file would look like this.


If you need a mail  notification when the backup script is failed, modify your cronjob as below.


If you get following error in the log file even after AWSCLI  is configured, then it means you don’t have AWSCLI  in the environment path $PATH of crontab.

The simple fix is, get the path of AWSCLI.

And add the path  PATH=$PATH:/usr/local/bin/  in the beginning of the backup script(after shebang).

You can see the backups created in the s3 bucket by running below command.

Thanks for the time taken to read my blog. Subscribe to this blog so that you don’t miss out anything useful (Checkout Right Sidebar for the Subscription Form) . Please also put your thoughts on this article as   comments .


A Site Reliability Engineer and an automation enthusiast.
Posts created 36

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top