Thursday, July 17, 2025
HomeGuest BlogsHow 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: neveropen
## Website: https://neveropen.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

RELATED ARTICLES

Most Popular

Dominic
32143 POSTS0 COMMENTS
Milvus
67 POSTS0 COMMENTS
Nango Kala
6526 POSTS0 COMMENTS
Nicole Veronica
11674 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11729 POSTS0 COMMENTS
Shaida Kate Naidoo
6616 POSTS0 COMMENTS
Ted Musemwa
6894 POSTS0 COMMENTS
Thapelo Manthata
6585 POSTS0 COMMENTS
Umr Jansen
6574 POSTS0 COMMENTS