How To Create Daily Automated Backups on an Amazon Lightsail Instance?

How To Create Daily Automated Backups on an Amazon Lightsail Instance

While Amazon Lightsail offers something really great, a daily snapshot feature, that helps you create an automated backup of your entire Amazon Lightsail instance without having to do anything manually, we don’t always need a daily backup of our entire website.

For a site on which only a particular set of data changes / is added, backing up only that bit would make more sense. Thankfully, it is pretty simple and straightforward to do that.

Creating an automated backup on Linux

Here’s what we are going to do:

  • Write and test command to create a zip file using the contents of the data folder
  • Create a bash script file and based on the command created above
  • Give appropriate file permissions to the bash script file
  • Create a crontab entry to run the bash script daily, at a given time

Let’s see how to create a daily backup of a particular folder on a lightsail instance using a bash script that will be hooked to crontab.

Let’s consider our folder structure to be like below:


The daily_images folder is the one that we want to create an automated daily backup of.

Write and Test command to create a zip file

Based on our folder structure given above, let’s navigate to the public folder on the terminal using the following command:

cd /opt/bitnami/apache/htdocs/website_root/app/public

Once there, type the following command to create a zip file containing the contents of our daily_images folder.

zip -r daily_images

Once completed, you will have your in the public folder.

Create a bash script for automated daily backup

Now that our main command is working, let’s start by first writing a bash script. We will store our script in the /home/scripts folder.
If the ‘scripts’ folder doesn’t exist, create one using the following command:

cd /home/bitnami
mkdir /scripts

Once done, let’s move to the scripts directory and let’s create the bash script file using the following command:

cd scripts

Once inside the folder, fire up your favorite text editor and create a new bash script file by using the following command:

sudo vim

Once in the newly created script file, add the following code to the file:

#! /bin/bash

source_folder = "/opt/bitnami/apache/htdocs/website_root/storage/app/public/daily_images"
backup_folder = "/opt/bitnami/apache/htdocs/website_root/public"
backup_file = "$backup_folder/"

zip -r "$backup_file" "$source_folder"

Save the file and exit the text editor.

Give appropriate file permissions to the script file

For the file to be run automatically, necessary permissions need to be set. Use the following command to change the file permissions of our bash script as needed.

chmod +x /home/bitnami/scripts/

Testing the script file

Once the script is in place, let’s run it once and see if we are getting our desired file or not. Run the following command, while at the ‘\home\bitnami\scripts’ folder, to run the bash script.


If all goes well, you will see files being processed on your terminal (you can use the web terminal that Lightsail provides or use PuTTY on Mac or Windows. At the end, you should have your zip file ready.

Creating a crontab entry to run the backup script automatically

Now that everything is working smoothly, it’s time to hook the bash script file to the crontab so that it can run on its own, at a predefined time. To do that, let’s open the crontab by using the following command:

crontab -e

If prompted, choose any text editor from the options given. My favorite on the terminal is vim, so, I will go with the same. In the crontab, we will create a new entry and add the following line:

40 10 * * * /bin/bash /opt/bitnami/scripts/

Once added, save and exit (!wq on vim).

Here, we are telling crontab to run our script file at 10:40 am.

BONUS: Deleting the last day’s backup file and creating a new one, automatically

Since our backup script runs to generate file daily, it would be a good idea to check if the destination folder already has a file. If so, then it makes sense to first remove it before creating a new one.

In order to do that, we will modify our bash script to look like below:

#! /bin/bash

source_folder = "/opt/bitnami/apache/htdocs/website_root/storage/app/public/daily_images"
backup_folder = "/opt/bitnami/apache/htdocs/website_root/public"
backup_file = "$backup_folder/"

# Remove old backup file if exists
if [ -f "$backup_file" ]; then
	rm "$backup_file"

zip -r "$backup_file" "$source_folder"

There, all done now!

Share This Post

2 thoughts on “How To Create Daily Automated Backups on an Amazon Lightsail Instance?”

  1. Pingback: Zipping Subfolder Content Generated in the Last 24 Hours On AWS Lightsail (Linux) - Rajiv Verma

  2. Pingback: How To Set Timezone in Linux? - Rajiv Verma

Leave a Comment

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

Subscribe To my Future Posts

Get notified whenever I post something new

More To Explore