backup website to amazon s3

How to Backup Website to Amazon S3

Amazon S3 is an affordable cloud-based object storage device that is widely used for backing up data or to serve static website content such as CSS, JS, Image files. In this article, we will learn how to backup website to Amazon S3 bucket. We will use AWSCLI that is the command line utility to manage AWS services, for this purpose.


How to Backup Website to Amazon S3

Here are the steps to backup website to Amazon S3.


1. Install AWSCLI

AWSCLI is available in almost every default Linux repository. So you can easily install it with the following commands, depending on your Linux distribution.

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

Run the following command to verify installation.

$ aws --version  


2. Create Shell Script

Run the following command to create an empty shell script.

$ vi /scripts/website_backup.sh   

Add the following lines to it.

#/usr/bin/env bash 
 
S3_BUCKET_NAME=""
DIR_TO_BACKUP="/var/www/html"
BACKUP_FILENAME='website'
 
TODAY=`date +%Y%m%d`
YY=`date +%Y`
MM=`date +%m`
AWSCMD="/usr/local/bin/aws"
TARCMD="/usr/bin/tar"
 
${TARCMD} czf /tmp/${BACKUP_FILENAME}-${TODAY}.tar.gz
 
${AWSCMD} cp /tmp/${BACKUP_FILENAME}-${TODAY}.tar.gz s3://${S3_BUCKET_NAME}/${YY}/${MM}/
 
 
if [ $? -eq 0 ]; then
 echo "Backup successfully uploaded to s3 bucket"
else
    echo "Error in s3 backup"
fi

Save and close the file.

In the above code, we basically archive the entire website’s code and upload it to Amazon S3 bucket. It means we archive all files and folders located at /var/www/html, using tar command and create a .tar.gz file which is further uploaded to Amazon S3 bucket. Please update S3_BUCKET_NAME and BACKUP_FILENAME as per your requirement.

The AWS command to backup data to S3 bucket is

aws cp /path/to/file s3://bucket_name/subfolder

So we are storing the .tar.gz file of our website’s root folder in S3, under subfolders YY/MM where YY is last two digit of current year and MM is the month number. Whenever you run the above script it will store the backup in that month’s folder.


3. Make Shell Script Executable

Run the following command to make shell script executable.

$ chmod +x /scripts/website_backup.sh 

Now you can run the script with the following command.

$ bash /scripts/website_backup.sh 


4. Create Cronjob

Open Crontab with the following command.

$ crontab -e

Add the following entry to your crontab to run website_backup.sh script every day at 10 a.m.

0 10 * * * bash /scripts/website_backup.sh 

In this article, we have learnt how to backup website to Amazon S3. You can use the same approach to backup blogs, applications, any directory or folder to Amazon S3.

Also read:

How to Check MD5 Checksum of Installed Packages in Ubuntu/Debian
How to Check Bad Sectors in HDD in Ubuntu
How to Encrypt & Decrypt Files Using OpenSSL
How to Add New SSH Key in GitHub
pgAdmin Connect Via SSH Tunnel

Leave a Reply

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