How to Backup Website to Amazon S3 using Shell Script

How to Backup Website to Amazon S3 using Shell Script

Backup website to amazon AWS s3 using shell script; Through this tutorial, we will learn how to take backup website to amazon AWS s3 using shell script.

How to Backup Website to Amazon S3 using Shell Script

Follow the following steps to take backup website to amazon aws s3 using shell script:

  • Step 1 – Install AWS CLI
  • Step 2 – Create A Shell Script
  • Step 3 – Execute Backup Script
  • Step 4 – Schedule Backup Script

Step 1 – Install AWS CLI

Just use the following tutorial to learn how to install aws cli on unix/linux system:

How to Install AWS CLI on Linux

Step 2 – 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:

#/usr/bin/env bash
 
################################################################
##
## Shell script to archive website code and upload to S3 bucket.
## Written by: tutsmake
## Website: https://tutsmake.net
##
#################################################################
 
 
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

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

Step 3 – Execute Backup Script

And execute the following command on the command line to the backup script:

chmod +x /scripts/s3WebsiteBackup.sh 

Then run the backup script.

bash /scripts/s3WebsiteBackup.sh 

Step 4 – Schedule Backup Script

Then use the following command to schedule the shell script using crontab to run on a daily basis.

crontab -e 

Add the below settings to end of the file:

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

Save the file and close it.

Conclusion

Through this tutorial, we have learned how to take the backup website to amazon AWS s3 using shell script.

Recommended Linux Tutorials

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published.