Guest User

Untitled

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