Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Volume list file will have volume-id:Volume-name format
  4.  
  5. VOLUMES_LIST = /var/log/volumes-list
  6. SNAPSHOT_INFO = /var/log/snapshot_info
  7. DATE = `date +%Y-%m-%d`
  8. REGION = "eu-west-1"
  9.  
  10. # Snapshots Retention Period for each volume snapshot
  11. RETENTION=6
  12.  
  13. SNAP_CREATION = /var/log/snap_creation
  14. SNAP_DELETION = /var/log/snap_deletion
  15.  
  16. EMAIL_LIST = abc@domain.com
  17.  
  18. echo "List of Snapshots Creation Status" > $SNAP_CREATION
  19. echo "List of Snapshots Deletion Status" > $SNAP_DELETION
  20.  
  21. # Check whether the volumes list file is available or not?
  22.  
  23. if [ -f $VOLUMES_LIST ]; then
  24.  
  25. # Creating Snapshot for each volume using for loop
  26.  
  27. for VOL_INFO in `cat $VOLUMES_LIST`
  28. do
  29. # Getting the Volume ID and Volume Name into the Separate Variables.
  30.  
  31. VOL_ID = `echo $VOL_INFO | awk -F":" '{print $1}'`
  32. VOL_NAME = `echo $VOL_INFO | awk -F":" '{print $2}'`
  33.  
  34. # Creating the Snapshot of the Volumes with Proper Description.
  35.  
  36. DESCRIPTION = "${VOL_NAME}_${DATE}"
  37.  
  38. /usr/local/bin/aws ec2 create-snapshot --volume-id $VOL_ID --description "$DESCRIPTION" --region $REGION &>> $SNAP_CREATION
  39. done
  40. else
  41. echo "Volumes list file is not available : $VOLUMES_LIST Exiting." | mail -s "Snapshots Creation Status" $EMAIL_LIST
  42. exit 1
  43. fi
  44.  
  45. echo >> $SNAP_CREATION
  46. echo >> $SNAP_CREATION
  47.  
  48. # Deleting the Snapshots which are 10 days old.
  49.  
  50. for VOL_INFO in `cat $VOLUMES_LIST`
  51. do
  52.  
  53. # Getting the Volume ID and Volume Name into the Separate Variables.
  54.  
  55. VOL_ID = `echo $VOL_INFO | awk -F":" '{print $1}'`
  56. VOL_NAME = `echo $VOL_INFO | awk -F":" '{print $2}'`
  57.  
  58. # Getting the Snapshot details of each volume.
  59.  
  60. /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
  61.  
  62. # Snapshots Retention Period Checking and if it crosses delete them.
  63.  
  64. while read SNAP_INFO
  65. do
  66. SNAP_ID=`echo $SNAP_INFO | awk '{print $1}'`
  67. echo $SNAP_ID
  68. SNAP_DATE=`echo $SNAP_INFO | awk '{print $4}' | awk -F"T" '{print $1}'`
  69. echo $SNAP_DATE
  70.  
  71. # Getting the no.of days difference between a snapshot and present day.
  72.  
  73. RETENTION_DIFF = `echo $(($(($(date -d "$DATE" "+%s") - $(date -d "$SNAP_DATE" "+%s"))) / 86400))`
  74. echo $RETENTION_DIFF
  75.  
  76. # Deleting the Snapshots which are older than the Retention Period
  77.  
  78. if [ $RETENTION -lt $RETENTION_DIFF ];
  79. then
  80. /usr/local/bin/aws ec2 delete-snapshot --snapshot-id $SNAP_ID --region $REGION --output text> /tmp/snap_del
  81. echo DELETING $SNAP_INFO >> $SNAP_DELETION
  82. fi
  83. done < $SNAPSHOT_INFO
  84. done
  85.  
  86. echo >> $SNAP_DELETION
  87.  
  88. # Merging the Snap Creation and Deletion Data
  89.  
  90. cat $SNAP_CREATION $SNAP_DELETION > /var/log/mail_report
  91.  
  92. # Sending the mail Update
  93.  
  94. cat /var/log/mail_report | mail -s "Volume Snapshots Status" $EMAIL_LIST
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement