diff --git a/ebs-snapshot.sh b/ebs-snapshot.sh index 89c9b64..7dcca25 100644 --- a/ebs-snapshot.sh +++ b/ebs-snapshot.sh @@ -27,6 +27,8 @@ set -o pipefail ## Variable Declartions ## +AWS_PROFILE="default" + # Get Instance Details instance_id=$(wget -q -O- http://169.254.169.254/latest/meta-data/instance-id) region=$(wget -q -O- http://169.254.169.254/latest/meta-data/placement/availability-zone | sed -e 's/\([1-9]\).$/\1/g') @@ -73,34 +75,34 @@ snapshot_volumes() { log "Volume ID is $volume_id" # Get the attched device name to add to the description so we can easily tell which volume this is. - device_name=$(aws ec2 describe-volumes --region $region --output=text --volume-ids $volume_id --query 'Volumes[0].{Devices:Attachments[0].Device}') + device_name=$(aws ec2 --profile $AWS_PROFILE describe-volumes --region $region --output=text --volume-ids $volume_id --query 'Volumes[0].{Devices:Attachments[0].Device}') # Take a snapshot of the current volume, and capture the resulting snapshot ID snapshot_description="$(hostname)-$device_name-backup-$(date +%Y-%m-%d)" - snapshot_id=$(aws ec2 create-snapshot --region $region --output=text --description $snapshot_description --volume-id $volume_id --query SnapshotId) + snapshot_id=$(aws ec2 --profile $AWS_PROFILE create-snapshot --region $region --output=text --description $snapshot_description --volume-id $volume_id --query SnapshotId) log "New snapshot is $snapshot_id" # Add a "CreatedBy:AutomatedBackup" tag to the resulting snapshot. # Why? Because we only want to purge snapshots taken by the script later, and not delete snapshots manually taken. - aws ec2 create-tags --region $region --resource $snapshot_id --tags Key=CreatedBy,Value=AutomatedBackup + aws ec2 --profile $AWS_PROFILE create-tags --region $region --resource $snapshot_id --tags Key=CreatedBy,Value=AutomatedBackup done } # Function: Cleanup all snapshots associated with this instance that are older than $retention_days cleanup_snapshots() { for volume_id in $volume_list; do - snapshot_list=$(aws ec2 describe-snapshots --region $region --output=text --filters "Name=volume-id,Values=$volume_id" "Name=tag:CreatedBy,Values=AutomatedBackup" --query Snapshots[].SnapshotId) + snapshot_list=$(aws ec2 --profile $AWS_PROFILE describe-snapshots --region $region --output=text --filters "Name=volume-id,Values=$volume_id" "Name=tag:CreatedBy,Values=AutomatedBackup" --query Snapshots[].SnapshotId) for snapshot in $snapshot_list; do log "Checking $snapshot..." # Check age of snapshot - snapshot_date=$(aws ec2 describe-snapshots --region $region --output=text --snapshot-ids $snapshot --query Snapshots[].StartTime | awk -F "T" '{printf "%s\n", $1}') + snapshot_date=$(aws ec2 --profile $AWS_PROFILE describe-snapshots --region $region --output=text --snapshot-ids $snapshot --query Snapshots[].StartTime | awk -F "T" '{printf "%s\n", $1}') snapshot_date_in_seconds=$(date "--date=$snapshot_date" +%s) - snapshot_description=$(aws ec2 describe-snapshots --snapshot-id $snapshot --region $region --query Snapshots[].Description) + snapshot_description=$(aws ec2 --profile $AWS_PROFILE describe-snapshots --snapshot-id $snapshot --region $region --query Snapshots[].Description) if (( $snapshot_date_in_seconds <= $retention_date_in_seconds )); then log "DELETING snapshot $snapshot. Description: $snapshot_description ..." - aws ec2 delete-snapshot --region $region --snapshot-id $snapshot + aws ec2 --profile $AWS_PROFILE delete-snapshot --region $region --snapshot-id $snapshot else log "Not deleting snapshot $snapshot. Description: $snapshot_description ..." fi @@ -115,7 +117,7 @@ log_setup prerequisite_check # Grab all volume IDs attached to this instance -volume_list=$(aws ec2 describe-volumes --region $region --filters Name=attachment.instance-id,Values=$instance_id --query Volumes[].VolumeId --output text) +volume_list=$(aws ec2 --profile $AWS_PROFILE describe-volumes --region $region --filters Name=attachment.instance-id,Values=$instance_id --query Volumes[].VolumeId --output text) snapshot_volumes cleanup_snapshots