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

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including 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. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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