Create Automatic EBS snapshot












0















I was looking to setup automatic EBS snapshot at a particular time interval (let say once every week), for this I did Google and found that this task can be done using shell script and I found the same at this link.



    #!/bin/bash

# Volume list file will have volume-id:Volume-name format

VOLUMES_LIST = /var/log/volumes-list
SNAPSHOT_INFO = /var/log/snapshot_info
DATE = `date +%Y-%m-%d`
REGION = "ap-south-1a"

# Snapshots Retention Period for each volume snapshot
RETENTION=6

SNAP_CREATION = /var/log/snap_creation
SNAP_DELETION = /var/log/snap_deletion

EMAIL_LIST = shishupal.shakya@itsmysun.com

echo "List of Snapshots Creation Status" > $SNAP_CREATION
echo "List of Snapshots Deletion Status" > $SNAP_DELETION

# Check whether the volumes list file is available or not?

if [ -f $VOLUMES_LIST ]; then

# Creating Snapshot for each volume using for loop

for VOL_INFO in `cat $VOLUMES_LIST`
do
# Getting the Volume ID and Volume Name into the Separate Variables.

VOL_ID = `echo $VOL_INFO | awk -F":" '{print $1}'`
VOL_NAME = `echo $VOL_INFO | awk -F":" '{print $2}'`

# Creating the Snapshot of the Volumes with Proper Description.

DESCRIPTION = "${VOL_NAME}_${DATE}"

/usr/local/bin/aws ec2 create-snapshot --volume-id $VOL_ID --description "$DESCRIPTION" --region $REGION &>> $SNAP_CREATION
done
else
echo "Volumes list file is not available : $VOLUMES_LIST Exiting." | mail -s "Snapshots Creation Status" $EMAIL_LIST
exit 1
fi

echo >> $SNAP_CREATION
echo >> $SNAP_CREATION

# Deleting the Snapshots which are 10 days old.

for VOL_INFO in `cat $VOLUMES_LIST`
do

# Getting the Volume ID and Volume Name into the Separate Variables.

VOL_ID = `echo $VOL_INFO | awk -F":" '{print $1}'`
VOL_NAME = `echo $VOL_INFO | awk -F":" '{print $2}'`

# Getting the Snapshot details of each volume.

/usr/local/bin/aws ec2 describe-snapshots --query Snapshots[*].[SnapshotId,VolumeId,Description,StartTime] --output text --filters "Name=status,Values=completed" "Name=volume-id,Values=$VOL_ID" | grep -v "CreateImage" > $SNAPSHOT_INFO

# Snapshots Retention Period Checking and if it crosses delete them.

while read SNAP_INFO
do
SNAP_ID=`echo $SNAP_INFO | awk '{print $1}'`
echo $SNAP_ID
SNAP_DATE=`echo $SNAP_INFO | awk '{print $4}' | awk -F"T" '{print $1}'`
echo $SNAP_DATE

# Getting the no.of days difference between a snapshot and present day.

RETENTION_DIFF = `echo $(($(($(date -d "$DATE" "+%s") - $(date -d "$SNAP_DATE" "+%s"))) / 86400))`
echo $RETENTION_DIFF

# Deleting the Snapshots which are older than the Retention Period

if [ $RETENTION -lt $RETENTION_DIFF ];
then
/usr/local/bin/aws ec2 delete-snapshot --snapshot-id $SNAP_ID --region $REGION --output text> /tmp/snap_del
echo DELETING $SNAP_INFO >> $SNAP_DELETION
fi
done < $SNAPSHOT_INFO
done

echo >> $SNAP_DELETION

# Merging the Snap Creation and Deletion Data

cat $SNAP_CREATION $SNAP_DELETION > /var/log/mail_report

# Sending the mail Update

cat /var/log/mail_report | mail -s "Volume Snapshots Status" $EMAIL_LIST


But when I ran it over terminal, it is showing me following errors.



enter image description here



Since I am new in this type of work so I am little uncomfortable in resolving this. Please suggest the fix, I am on this since last few days.










share|improve this question





























    0















    I was looking to setup automatic EBS snapshot at a particular time interval (let say once every week), for this I did Google and found that this task can be done using shell script and I found the same at this link.



        #!/bin/bash

    # Volume list file will have volume-id:Volume-name format

    VOLUMES_LIST = /var/log/volumes-list
    SNAPSHOT_INFO = /var/log/snapshot_info
    DATE = `date +%Y-%m-%d`
    REGION = "ap-south-1a"

    # Snapshots Retention Period for each volume snapshot
    RETENTION=6

    SNAP_CREATION = /var/log/snap_creation
    SNAP_DELETION = /var/log/snap_deletion

    EMAIL_LIST = shishupal.shakya@itsmysun.com

    echo "List of Snapshots Creation Status" > $SNAP_CREATION
    echo "List of Snapshots Deletion Status" > $SNAP_DELETION

    # Check whether the volumes list file is available or not?

    if [ -f $VOLUMES_LIST ]; then

    # Creating Snapshot for each volume using for loop

    for VOL_INFO in `cat $VOLUMES_LIST`
    do
    # Getting the Volume ID and Volume Name into the Separate Variables.

    VOL_ID = `echo $VOL_INFO | awk -F":" '{print $1}'`
    VOL_NAME = `echo $VOL_INFO | awk -F":" '{print $2}'`

    # Creating the Snapshot of the Volumes with Proper Description.

    DESCRIPTION = "${VOL_NAME}_${DATE}"

    /usr/local/bin/aws ec2 create-snapshot --volume-id $VOL_ID --description "$DESCRIPTION" --region $REGION &>> $SNAP_CREATION
    done
    else
    echo "Volumes list file is not available : $VOLUMES_LIST Exiting." | mail -s "Snapshots Creation Status" $EMAIL_LIST
    exit 1
    fi

    echo >> $SNAP_CREATION
    echo >> $SNAP_CREATION

    # Deleting the Snapshots which are 10 days old.

    for VOL_INFO in `cat $VOLUMES_LIST`
    do

    # Getting the Volume ID and Volume Name into the Separate Variables.

    VOL_ID = `echo $VOL_INFO | awk -F":" '{print $1}'`
    VOL_NAME = `echo $VOL_INFO | awk -F":" '{print $2}'`

    # Getting the Snapshot details of each volume.

    /usr/local/bin/aws ec2 describe-snapshots --query Snapshots[*].[SnapshotId,VolumeId,Description,StartTime] --output text --filters "Name=status,Values=completed" "Name=volume-id,Values=$VOL_ID" | grep -v "CreateImage" > $SNAPSHOT_INFO

    # Snapshots Retention Period Checking and if it crosses delete them.

    while read SNAP_INFO
    do
    SNAP_ID=`echo $SNAP_INFO | awk '{print $1}'`
    echo $SNAP_ID
    SNAP_DATE=`echo $SNAP_INFO | awk '{print $4}' | awk -F"T" '{print $1}'`
    echo $SNAP_DATE

    # Getting the no.of days difference between a snapshot and present day.

    RETENTION_DIFF = `echo $(($(($(date -d "$DATE" "+%s") - $(date -d "$SNAP_DATE" "+%s"))) / 86400))`
    echo $RETENTION_DIFF

    # Deleting the Snapshots which are older than the Retention Period

    if [ $RETENTION -lt $RETENTION_DIFF ];
    then
    /usr/local/bin/aws ec2 delete-snapshot --snapshot-id $SNAP_ID --region $REGION --output text> /tmp/snap_del
    echo DELETING $SNAP_INFO >> $SNAP_DELETION
    fi
    done < $SNAPSHOT_INFO
    done

    echo >> $SNAP_DELETION

    # Merging the Snap Creation and Deletion Data

    cat $SNAP_CREATION $SNAP_DELETION > /var/log/mail_report

    # Sending the mail Update

    cat /var/log/mail_report | mail -s "Volume Snapshots Status" $EMAIL_LIST


    But when I ran it over terminal, it is showing me following errors.



    enter image description here



    Since I am new in this type of work so I am little uncomfortable in resolving this. Please suggest the fix, I am on this since last few days.










    share|improve this question



























      0












      0








      0








      I was looking to setup automatic EBS snapshot at a particular time interval (let say once every week), for this I did Google and found that this task can be done using shell script and I found the same at this link.



          #!/bin/bash

      # Volume list file will have volume-id:Volume-name format

      VOLUMES_LIST = /var/log/volumes-list
      SNAPSHOT_INFO = /var/log/snapshot_info
      DATE = `date +%Y-%m-%d`
      REGION = "ap-south-1a"

      # Snapshots Retention Period for each volume snapshot
      RETENTION=6

      SNAP_CREATION = /var/log/snap_creation
      SNAP_DELETION = /var/log/snap_deletion

      EMAIL_LIST = shishupal.shakya@itsmysun.com

      echo "List of Snapshots Creation Status" > $SNAP_CREATION
      echo "List of Snapshots Deletion Status" > $SNAP_DELETION

      # Check whether the volumes list file is available or not?

      if [ -f $VOLUMES_LIST ]; then

      # Creating Snapshot for each volume using for loop

      for VOL_INFO in `cat $VOLUMES_LIST`
      do
      # Getting the Volume ID and Volume Name into the Separate Variables.

      VOL_ID = `echo $VOL_INFO | awk -F":" '{print $1}'`
      VOL_NAME = `echo $VOL_INFO | awk -F":" '{print $2}'`

      # Creating the Snapshot of the Volumes with Proper Description.

      DESCRIPTION = "${VOL_NAME}_${DATE}"

      /usr/local/bin/aws ec2 create-snapshot --volume-id $VOL_ID --description "$DESCRIPTION" --region $REGION &>> $SNAP_CREATION
      done
      else
      echo "Volumes list file is not available : $VOLUMES_LIST Exiting." | mail -s "Snapshots Creation Status" $EMAIL_LIST
      exit 1
      fi

      echo >> $SNAP_CREATION
      echo >> $SNAP_CREATION

      # Deleting the Snapshots which are 10 days old.

      for VOL_INFO in `cat $VOLUMES_LIST`
      do

      # Getting the Volume ID and Volume Name into the Separate Variables.

      VOL_ID = `echo $VOL_INFO | awk -F":" '{print $1}'`
      VOL_NAME = `echo $VOL_INFO | awk -F":" '{print $2}'`

      # Getting the Snapshot details of each volume.

      /usr/local/bin/aws ec2 describe-snapshots --query Snapshots[*].[SnapshotId,VolumeId,Description,StartTime] --output text --filters "Name=status,Values=completed" "Name=volume-id,Values=$VOL_ID" | grep -v "CreateImage" > $SNAPSHOT_INFO

      # Snapshots Retention Period Checking and if it crosses delete them.

      while read SNAP_INFO
      do
      SNAP_ID=`echo $SNAP_INFO | awk '{print $1}'`
      echo $SNAP_ID
      SNAP_DATE=`echo $SNAP_INFO | awk '{print $4}' | awk -F"T" '{print $1}'`
      echo $SNAP_DATE

      # Getting the no.of days difference between a snapshot and present day.

      RETENTION_DIFF = `echo $(($(($(date -d "$DATE" "+%s") - $(date -d "$SNAP_DATE" "+%s"))) / 86400))`
      echo $RETENTION_DIFF

      # Deleting the Snapshots which are older than the Retention Period

      if [ $RETENTION -lt $RETENTION_DIFF ];
      then
      /usr/local/bin/aws ec2 delete-snapshot --snapshot-id $SNAP_ID --region $REGION --output text> /tmp/snap_del
      echo DELETING $SNAP_INFO >> $SNAP_DELETION
      fi
      done < $SNAPSHOT_INFO
      done

      echo >> $SNAP_DELETION

      # Merging the Snap Creation and Deletion Data

      cat $SNAP_CREATION $SNAP_DELETION > /var/log/mail_report

      # Sending the mail Update

      cat /var/log/mail_report | mail -s "Volume Snapshots Status" $EMAIL_LIST


      But when I ran it over terminal, it is showing me following errors.



      enter image description here



      Since I am new in this type of work so I am little uncomfortable in resolving this. Please suggest the fix, I am on this since last few days.










      share|improve this question
















      I was looking to setup automatic EBS snapshot at a particular time interval (let say once every week), for this I did Google and found that this task can be done using shell script and I found the same at this link.



          #!/bin/bash

      # Volume list file will have volume-id:Volume-name format

      VOLUMES_LIST = /var/log/volumes-list
      SNAPSHOT_INFO = /var/log/snapshot_info
      DATE = `date +%Y-%m-%d`
      REGION = "ap-south-1a"

      # Snapshots Retention Period for each volume snapshot
      RETENTION=6

      SNAP_CREATION = /var/log/snap_creation
      SNAP_DELETION = /var/log/snap_deletion

      EMAIL_LIST = shishupal.shakya@itsmysun.com

      echo "List of Snapshots Creation Status" > $SNAP_CREATION
      echo "List of Snapshots Deletion Status" > $SNAP_DELETION

      # Check whether the volumes list file is available or not?

      if [ -f $VOLUMES_LIST ]; then

      # Creating Snapshot for each volume using for loop

      for VOL_INFO in `cat $VOLUMES_LIST`
      do
      # Getting the Volume ID and Volume Name into the Separate Variables.

      VOL_ID = `echo $VOL_INFO | awk -F":" '{print $1}'`
      VOL_NAME = `echo $VOL_INFO | awk -F":" '{print $2}'`

      # Creating the Snapshot of the Volumes with Proper Description.

      DESCRIPTION = "${VOL_NAME}_${DATE}"

      /usr/local/bin/aws ec2 create-snapshot --volume-id $VOL_ID --description "$DESCRIPTION" --region $REGION &>> $SNAP_CREATION
      done
      else
      echo "Volumes list file is not available : $VOLUMES_LIST Exiting." | mail -s "Snapshots Creation Status" $EMAIL_LIST
      exit 1
      fi

      echo >> $SNAP_CREATION
      echo >> $SNAP_CREATION

      # Deleting the Snapshots which are 10 days old.

      for VOL_INFO in `cat $VOLUMES_LIST`
      do

      # Getting the Volume ID and Volume Name into the Separate Variables.

      VOL_ID = `echo $VOL_INFO | awk -F":" '{print $1}'`
      VOL_NAME = `echo $VOL_INFO | awk -F":" '{print $2}'`

      # Getting the Snapshot details of each volume.

      /usr/local/bin/aws ec2 describe-snapshots --query Snapshots[*].[SnapshotId,VolumeId,Description,StartTime] --output text --filters "Name=status,Values=completed" "Name=volume-id,Values=$VOL_ID" | grep -v "CreateImage" > $SNAPSHOT_INFO

      # Snapshots Retention Period Checking and if it crosses delete them.

      while read SNAP_INFO
      do
      SNAP_ID=`echo $SNAP_INFO | awk '{print $1}'`
      echo $SNAP_ID
      SNAP_DATE=`echo $SNAP_INFO | awk '{print $4}' | awk -F"T" '{print $1}'`
      echo $SNAP_DATE

      # Getting the no.of days difference between a snapshot and present day.

      RETENTION_DIFF = `echo $(($(($(date -d "$DATE" "+%s") - $(date -d "$SNAP_DATE" "+%s"))) / 86400))`
      echo $RETENTION_DIFF

      # Deleting the Snapshots which are older than the Retention Period

      if [ $RETENTION -lt $RETENTION_DIFF ];
      then
      /usr/local/bin/aws ec2 delete-snapshot --snapshot-id $SNAP_ID --region $REGION --output text> /tmp/snap_del
      echo DELETING $SNAP_INFO >> $SNAP_DELETION
      fi
      done < $SNAPSHOT_INFO
      done

      echo >> $SNAP_DELETION

      # Merging the Snap Creation and Deletion Data

      cat $SNAP_CREATION $SNAP_DELETION > /var/log/mail_report

      # Sending the mail Update

      cat /var/log/mail_report | mail -s "Volume Snapshots Status" $EMAIL_LIST


      But when I ran it over terminal, it is showing me following errors.



      enter image description here



      Since I am new in this type of work so I am little uncomfortable in resolving this. Please suggest the fix, I am on this since last few days.







      scripts backup amazon-ec2






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 8 at 12:39







      Android Dev

















      asked Jan 8 at 11:57









      Android DevAndroid Dev

      1015




      1015






















          1 Answer
          1






          active

          oldest

          votes


















          0














          I followed below steps to resolve it.




          1. Created /var/log/volumes-list and wrote volume-id:Volume-name inside it.

          2. Installed AWS CLI using this link.

          3. Changed /usr/local/bin/aws with /home/centos/.local/bin/aws (in my case).

          4. Ran it using following command.



          bash myscript.sh




          It worked like a charm.






          share|improve this answer























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "89"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1107961%2fcreate-automatic-ebs-snapshot%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            I followed below steps to resolve it.




            1. Created /var/log/volumes-list and wrote volume-id:Volume-name inside it.

            2. Installed AWS CLI using this link.

            3. Changed /usr/local/bin/aws with /home/centos/.local/bin/aws (in my case).

            4. Ran it using following command.



            bash myscript.sh




            It worked like a charm.






            share|improve this answer




























              0














              I followed below steps to resolve it.




              1. Created /var/log/volumes-list and wrote volume-id:Volume-name inside it.

              2. Installed AWS CLI using this link.

              3. Changed /usr/local/bin/aws with /home/centos/.local/bin/aws (in my case).

              4. Ran it using following command.



              bash myscript.sh




              It worked like a charm.






              share|improve this answer


























                0












                0








                0







                I followed below steps to resolve it.




                1. Created /var/log/volumes-list and wrote volume-id:Volume-name inside it.

                2. Installed AWS CLI using this link.

                3. Changed /usr/local/bin/aws with /home/centos/.local/bin/aws (in my case).

                4. Ran it using following command.



                bash myscript.sh




                It worked like a charm.






                share|improve this answer













                I followed below steps to resolve it.




                1. Created /var/log/volumes-list and wrote volume-id:Volume-name inside it.

                2. Installed AWS CLI using this link.

                3. Changed /usr/local/bin/aws with /home/centos/.local/bin/aws (in my case).

                4. Ran it using following command.



                bash myscript.sh




                It worked like a charm.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 22 at 9:11









                Android DevAndroid Dev

                1015




                1015






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Ask Ubuntu!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1107961%2fcreate-automatic-ebs-snapshot%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

                    ComboBox Display Member on multiple fields

                    Is it possible to collect Nectar points via Trainline?