Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Programming»Bash Shell»How to Backup Website to Amazon S3 using Shell Script

    How to Backup Website to Amazon S3 using Shell Script

    By RahulMarch 24, 20222 Mins Read

    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.

    Advertisement

    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:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    #/usr/bin/env bash
     
    ################################################################
    ##
    ## Shell script to archive website code and upload to S3 bucket.
    ## Written by: Rahul Kumar
    ## Website: https://tecadmin.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 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.

    Wrap Up

    This tutorial provides you a shell script to backup website content to the S3 bucket. Also includes the instruction to run this script.

    backup bash s3 script
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    S3FS: Mounting Amazon S3 Buckets on Ubuntu & Debian Systems

    Backing Up Your Linux System with Rsync: A Step-by-Step Guide

    10 Most Popular Linux Shells

    10 Most Popular Open Source Linux Shells

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • How to Split Large Archives in Linux using the Command Line
    • System.out.println() Method in Java: A Beginner’s Guide
    • Split Command in Linux With Examples (Split Large Files)
    • Test Your Internet Speed from the Linux Terminal
    • 11 Practical Example of cat Command in Linux
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.