How to Backup Website to Amazon S3 using Shell Script

Amazon Simple Storage Service (Amazon S3) is an cloud based object storage device. It is a low cost storage widely used for the backup or static website content.

You can use AWSCLI command line utility for managing s3 bucket and its content. In this tutorial, you will learn about backup a website to Amazon s3 bucket using a shell script.

Installing AWS CLI

The AWS CLI packages are available under the default repositories on most of the Linux systems. You can install it by running one of the following commands:

sudo dnf install awscli  ## Fedora, Redhat and CentOS sudo apt install awscli ## Ubuntu, Debian and Linux Mint

You can also another article to install latest AWS CLI on any Linux system.

Once the installation finished, check the awscli version by executing:

aws --version 

Create A Shell Script

Now, create a shell script file on your system and add the below content. For this tutorial, I created file using:

nano /scripts/s3WebsiteBackup.sh  

and added the following content:

Make sure to update S3_BUCKET_NAME and DIR_TO_BACKUP in the script. You can also change the backup file name in BACKUP_FILENAME variable.

Save file and close it. Now, you have a shell script to backup website content to s3 buckets.

Running Shell Script

Make the shell script executable by running the following command.

chmod +x /scripts/s3WebsiteBackup.sh 

Now, you can test the script by executing it manually.

bash /scripts/s3WebsiteBackup.sh 

On successful, backups will be uploaded to s3 bucket. Which you can view using aws s3 ls command.

Schedule Script in Cron

Next, schedule your script to crontab to automate this job. To edit the crontab of current user, type:

crontab -e 

Add the following entry to the crontab:

0 2 * * * bash /scripts/s3WebsiteBackup.sh 

Save file and close the editor.

Leave a Reply

Your email address will not be published.