Created
November 20, 2017 13:13
-
-
Save eekfonky/e8b6f4a8efda9d18d2fb5df8197ce250 to your computer and use it in GitHub Desktop.
AWS Snapshot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # error handling | |
| error_handler() { | |
| echo "Error occurred in script at line: ${1}" | |
| echo "Line exited with status: ${2}" | |
| } | |
| trap 'error_handler ${LINENO} $?' ERR | |
| set -o errexit | |
| set -o errtrace | |
| set -o nounset | |
| # check for dependencies | |
| jq_check () { | |
| # check Linux package manager (yum or apt) | |
| if ! type "jq" > /dev/null 2>&1; then | |
| if type "yum" > /dev/null 2>&1; then | |
| sudo yum install -y jq | |
| else | |
| sudo apt install -y jq | |
| fi | |
| fi | |
| } | |
| jq_check | |
| # set variables | |
| SOURCE_REGION="eu-central-1" | |
| DESTINATION_REGION="eu-west-1" | |
| TIMESTAMP=$(date +%F-%a) # year-month-date-day format | |
| VOL="vol-086f83557928e0540" # Volume to backup | |
| ORIG_SNAPSHOT="$TIMESTAMP-$VOL.json" | |
| # How many days do you wish to retain backups for? Default: 7 days | |
| RETENTION_DAYS="7" | |
| RETENTION_DATE_IN_SECONDS=$(date +%s --date "$RETENTION_DAYS days ago") | |
| # create snapshot of attached EBS and get SnapShotId | |
| aws --region "$SOURCE_REGION" ec2 create-snapshot --volume-id "$VOL" --description "$TIMESTAMP Jenkins Docker home temporary snapshot" > "$ORIG_SNAPSHOT" | |
| ORIG_ID=$(jq -r '.SnapshotId' "$ORIG_SNAPSHOT") | |
| COPY_SNAPSHOT="$TIMESTAMP-$ORIG_ID.json" | |
| # check snapshot status | |
| ORIG_PROGRESS=$(aws --region "$SOURCE_REGION" ec2 describe-snapshots --snapshot-id "$ORIG_ID" | jq -r '.Snapshots[].Progress') | |
| while [[ "$ORIG_PROGRESS" != "100%" ]]; do | |
| sleep 20 | |
| ORIG_PROGRESS=$(aws --region "$SOURCE_REGION" ec2 describe-snapshots --snapshot-id "$ORIG_ID" | jq -r '.Snapshots[].Progress') | |
| done | |
| # copy snapshot to another region for fault tolerance | |
| aws --region "$DESTINATION_REGION" ec2 copy-snapshot --source-region "$SOURCE_REGION" --source-snapshot-id "$ORIG_ID" --description "$TIMESTAMP Jenkins Docker home directories" > "$COPY_SNAPSHOT" | |
| COPY_ID=$(jq -r '.SnapshotId' "$COPY_SNAPSHOT") | |
| # check snapshot copy status | |
| COPY_PROGRESS=$(aws --region "$DESTINATION_REGION" ec2 describe-snapshots --snapshot-id "$COPY_ID" | jq -r '.Snapshots[].Progress') | |
| while [[ "$COPY_PROGRESS" != "100%" ]]; do | |
| sleep 60 | |
| COPY_PROGRESS=$(aws --region "$DESTINATION_REGION" ec2 describe-snapshots --snapshot-id "$COPY_ID" | jq -r '.Snapshots[].Progress') | |
| done | |
| # Add a "CreatedBy:AutomatedJenkinsBackup" tag to the resulting snapshot. | |
| aws ec2 create-tags --region "$DESTINATION_REGION" --resource "$COPY_ID" --tags Key=CreatedBy,Value=AutomatedJenkinsBackup | |
| # cleanup | |
| aws --region "$SOURCE_REGION" ec2 delete-snapshot --snapshot-id "$ORIG_ID" | |
| rm -rf "$ORIG_SNAPSHOT" "$COPY_SNAPSHOT" | |
| # Check age of snapshot | |
| SNAPSHOT_DATE=$(aws ec2 describe-snapshots --region eu-west-1 --output=text --snapshot-ids "$COPY_ID" --query Snapshots[].StartTime | awk -F "T" '{printf "%s\n", $1}') | |
| SNAPSHOT_DATE_IN_SECONDS=$(date "--date=$SNAPSHOT_DATE" +%s) | |
| if (( SNAPSHOT_DATE_IN_SECONDS <= RETENTION_DATE_IN_SECONDS )); then | |
| aws ec2 delete-snapshot --region "$DESTINATION_REGION" --snapshot-id "$COPY_ID" | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment