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.

Here is whole script, I am using :

    #!/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.

Shishupal Shakya
  • 1,632
  • 2
  • 18
  • 41
  • Possible duplicate of [Command not found error in Bash variable assignment](https://stackoverflow.com/q/2268104/608639). Also see [How to use Shellcheck](http://github.com/koalaman/shellcheck), [How to debug a bash script?](http://unix.stackexchange.com/q/155551/56041) (U&L.SE), [How to debug a bash script?](http://stackoverflow.com/q/951336/608639) (SO), [How to debug bash script?](http://askubuntu.com/q/21136) (AskU), [Debugging Bash scripts](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html), etc. – jww Jan 09 '19 at 00:33
  • Please show the code and/or state the errors. Please don't use links to images. The text is missing from the question. The text on the picture is too small for some people to read. The text on the image cannot be indexed by search engines for future visitors. Also see [Why not upload images of code on SO when asking a question?](https://meta.stackoverflow.com/q/285551/608639) – jww Jan 09 '19 at 00:33
  • @jww Thanks for your suggestions, now my problem has resolved after reading all the links you mentioned. – Shishupal Shakya Jan 15 '19 at 09:12

2 Answers2

1

There should not be spaces around the equals (=) signs.

FOO = 1
-bash: FOO: command not found

The correct syntax is:

FOO=1

Go through the script and remove all the spaces in the statements that assign values to variables.

But there is another error "expecting do" -- this makes me think the script is not being run with the correct shell. Instead of 'sh ec2.sh', try running it with bash explicitly: bash ec2.sh

Will Sheppard
  • 3,272
  • 2
  • 31
  • 41
0

It will be more easy to do it through AWS console.

In cloudwatch you create a new event rule. The event source is "scheduled". For the target you pick up "EC2 Createsnapshot API call". Enter the volume ID (you can find it in the ec2 instances console). Let AWS create a new role for this specific resource.

That's it !

ben11
  • 104
  • 3