Merge pull request #25 from davestern/copy-volume-regional

Copy Snapshots to different regions
This commit is contained in:
Colin Johnson 2013-04-10 13:12:10 -07:00
commit 9ba549ad00
2 changed files with 105 additions and 18 deletions

View File

@ -24,10 +24,12 @@ ec2-automate-backup requires one of the following two parameters be provided:
`-v <volumeid>` - the "volumeid" parameter is required to select EBS volumes for snapshot if ec2-automate-backup is run using the "volumeid" selection method - the "volumeid" selection method is the default selection method. `-v <volumeid>` - the "volumeid" parameter is required to select EBS volumes for snapshot if ec2-automate-backup is run using the "volumeid" selection method - the "volumeid" selection method is the default selection method.
`-t <tag>` - the "tag" parameter is required if the "method" of selecting EBS volumes for snapshot is by tag (-s tag). The format for tag is key=value (example: Backup=true) and the correct method for running ec2-automate-backup in this manner is ec2-automate-backup -s tag -t Backup=true. `-t <tag>` - the "tag" parameter is required if the "method" of selecting EBS volumes for snapshot is by tag (-s tag). The format for tag is key=value (example: Backup=true) and the correct method for running ec2-automate-backup in this manner is ec2-automate-backup -s tag -t Backup=true.
`-d <destination region(s)>` - the "destination region" parameter is required if the "method" of selecting EBS snapshots for copy to another region is by region (-s regioncopy). The format for `-d` is space-delimited region names such as ec2-automate-backup.sh -s regioncopy -d "us-west-1 us-west-2". This selection method only operates on snapshots.
## Optional Parameters: ## Optional Parameters:
`-r <region>` - the region that contains the EBS volumes for which you wish to have a snapshot created. `-r <region>` - the region that contains the EBS volumes for which you wish to have a snapshot created.
`-s <selection_method>` - the selection method by which EBS volumes will be selected. Currently supported selection methods are "volumeid" and "tag." The selection method "volumeid" identifies EBS volumes for which a snapshot should be taken by volume id whereas the selection method "tag" identifies EBS volumes for which a snapshot should be taken by a key=value format tag. `-s <selection_method>` - the selection method by which EBS volumes will be selected. Currently supported selection methods are "volumeid", "tag" and "regioncopy." The selection method "volumeid" identifies EBS volumes for which a snapshot should be taken by volume id whereas the selection method "tag" identifies EBS volumes for which a snapshot should be taken by a key=value format tag. The selection method "regioncopy" identifies regions as destinations for EBS volumes that have been scheduled for copy to that region.
`-c <cron_primer_file>` - running with the -c option and a providing a file will cause ec2-automate-backup to source a file for environmental configuration - ideal for running ec2-automate-backup under cron. An example cron primer file is located in the "Resources" directory and is called cron-primer.sh. `-c <cron_primer_file>` - running with the -c option and a providing a file will cause ec2-automate-backup to source a file for environmental configuration - ideal for running ec2-automate-backup under cron. An example cron primer file is located in the "Resources" directory and is called cron-primer.sh.
@ -36,14 +38,18 @@ ec2-automate-backup requires one of the following two parameters be provided:
`-k <purge_after_period>` - the period after which a snapshot can be purged. For example, running "ec2-automate-backup.sh -v "vol-6d6a0527 vol-636a0112" -k 31" would allow snapshots to be removed after 31 days. purge_after_period creates two tags for each volume that was backed up - a PurgeAllow tag which is set to PurgeAllow=true and a PurgeAfterFE tag which is set to the number of minutes from epoch after which a snapshot can be purged. Values can also be entered as -k 60m (this would purge snapshots after 60 minutes or -k 2h (this would purge snapshots after 2 hours) or -k 7d (this would purge snapshots after 7 days). With no trailing characters the purge after period defaults to days. `-k <purge_after_period>` - the period after which a snapshot can be purged. For example, running "ec2-automate-backup.sh -v "vol-6d6a0527 vol-636a0112" -k 31" would allow snapshots to be removed after 31 days. purge_after_period creates two tags for each volume that was backed up - a PurgeAllow tag which is set to PurgeAllow=true and a PurgeAfterFE tag which is set to the number of minutes from epoch after which a snapshot can be purged. Values can also be entered as -k 60m (this would purge snapshots after 60 minutes or -k 2h (this would purge snapshots after 2 hours) or -k 7d (this would purge snapshots after 7 days). With no trailing characters the purge after period defaults to days.
`-p` - the -p flag will purge (meaning delete) all snapshots that where the current date is passed the volumes "PurgeAfterFE" tag. ec2-automate-backup looks at two tags to determine which snapshots should be deleted - the PurgeAllow and PurgeAfterFE tags. The tags must be set as follows: PurgeAllow=true and PurgeAfterFE=xxxxxxxxxx where xxxxxxxxxx is a UNIX time that is before the current date. `-p` - the -p flag will purge (meaning delete) all snapshots that where the current date is passed the volumes "PurgeAfterFE" tag. ec2-automate-backup looks at two tags to determine which snapshots should be deleted - the PurgeAllow and PurgeAfterFE tags. The tags must be set as follows: PurgeAllow=true and PurgeAfterFE=xxxxxxxxxx where xxxxxxxxxx is a UNIX time that is before the current date.
`-g <scheduled destination regions>` - Space-delimited destination regions for the volumes selected. This is added when the `-v` or `-t` selection method is used. Note that this only adds a tag that schedules the snapshot to be copied to the destinations. On subsequent calls, the `-d` parameter will intiate copies to the specified regions.
# Potential Uses and Methods of Use: # Potential Uses and Methods of Use:
* To backup multiple EBS volumes use ec2-automate-backup as follows: `ec2-automate-backup -v "vol-6d6a0527 vol-636a0112"` * To backup multiple EBS volumes use ec2-automate-backup as follows: `ec2-automate-backup -v "vol-6d6a0527 vol-636a0112"`
* To backup multiple EBS volumes and schedule them for copy to two other regions: `ec2-automate-backup -v "vol-6d6a0527 vol-636a0112" -g "us-west-1 us-west-2"`
* To backup a selected group of EBS volumes on a daily schedule tag each volume you wish to backup with the tag "Backup=true" and run ec2-automate-backup using cron as follows: `0 0 * * 0 ec2-automate-backup -s tag -t "Backup=true"` * To backup a selected group of EBS volumes on a daily schedule tag each volume you wish to backup with the tag "Backup=true" and run ec2-automate-backup using cron as follows: `0 0 * * 0 ec2-automate-backup -s tag -t "Backup=true"`
* To backup a selected group of EBS volumes on a daily and/or monthly schedule tag each volume you wish to backup with the tag "Backup-Daily=true" and/or "Backup-Monthly=true" and run ec2-automate-backup using cron as follows: * To backup a selected group of EBS volumes on a daily and/or monthly schedule tag each volume you wish to backup with the tag "Backup-Daily=true" and/or "Backup-Monthly=true" and run ec2-automate-backup using cron as follows:
- `0 0 * * 0 ec2-user /home/ec2-user/ec2-automate-backup.sh -s tag -t "Backup-Daily=true"` - `0 0 * * 0 ec2-user /home/ec2-user/ec2-automate-backup.sh -s tag -t "Backup-Daily=true"`
- `0 0 1 * * ec2-user /home/ec2-user/ec2-automate-backup.sh -s tag -t "Backup-Monthly=true"` - `0 0 1 * * ec2-user /home/ec2-user/ec2-automate-backup.sh -s tag -t "Backup-Monthly=true"`
* To perform daily backup using cron and to load environment configuration with a "cron-primer" file: * To perform daily backup using cron and to load environment configuration with a "cron-primer" file:
- `0 0 * * 0 ec2-user /home/ec2-user/ec2-automate-backup.sh -c /home/ec2-user/cron-primer.sh -s tag -t "Backup=True"` - `0 0 * * 0 ec2-user /home/ec2-user/ec2-automate-backup.sh -c /home/ec2-user/cron-primer.sh -s tag -t "Backup=True"`
* To initiate the copy of scheduled snapshots to their destination regions: `ec2-automate-backup.sh -s regioncopy -d "us-west-1 us-west-2"`
`-u` - the -u flag will tag snapshots with additional data so that snapshots can be more easily located. Currently the two user tags created are Volume="ebs_volume" and Created="date." These can be easily modified in code. `-u` - the -u flag will tag snapshots with additional data so that snapshots can be more easily located. Currently the two user tags created are Volume="ebs_volume" and Created="date." These can be easily modified in code.

View File

@ -7,7 +7,14 @@
#confirms that executables required for succesful script execution are available #confirms that executables required for succesful script execution are available
prerequisite_check() prerequisite_check()
{ {
for prerequisite in basename ec2-create-snapshot ec2-create-tags ec2-describe-snapshots ec2-delete-snapshot date check_values=$1
if [[ -z $check_values ]]
then
check_values='ec2-create-snapshot ec2-create-tags ec2-describe-snapshots ec2-delete-snapshot date'
fi
for prerequisite in basename $check_values
do do
#use of "hash" chosen as it is a shell builtin and will add programs to hash table, possibly speeding execution. Use of type also considered - open to suggestions. #use of "hash" chosen as it is a shell builtin and will add programs to hash table, possibly speeding execution. Use of type also considered - open to suggestions.
hash $prerequisite &> /dev/null hash $prerequisite &> /dev/null
@ -33,8 +40,16 @@ get_EBS_List()
fi fi
ebs_selection_string="--filter tag:$tag" ebs_selection_string="--filter tag:$tag"
;; ;;
regioncopy)
if [[ -z $region_copy_destinations ]]
then echo "The selected selection_method \"regioncopy\" (-s regioncopy) requires valid region names (-d '[region name(s)]') or 'all' for operation. Correct usage is as follows: \"-s regioncopy -d 'us-west-1'\" or \"-s regioncopy -d 'us-west-1 us-west-2'.\"" 1>&2 ; exit 64
fi
;;
*) echo "If you specify a selection_method (-s selection_method) for selecting EBS volumes you must select either \"volumeid\" (-s volumeid) or \"tag\" (-s tag)." 1>&2 ; exit 64 ;; *) echo "If you specify a selection_method (-s selection_method) for selecting EBS volumes you must select either \"volumeid\" (-s volumeid) or \"tag\" (-s tag)." 1>&2 ; exit 64 ;;
esac esac
if [[ -n $ebs_selection_string ]]
then
#creates a list of all ebs volumes that match the selection string from above #creates a list of all ebs volumes that match the selection string from above
ebs_backup_list_complete=`ec2-describe-volumes --show-empty-fields --region $region $ebs_selection_string 2>&1` ebs_backup_list_complete=`ec2-describe-volumes --show-empty-fields --region $region $ebs_selection_string 2>&1`
#takes the output of the previous command #takes the output of the previous command
@ -44,10 +59,13 @@ get_EBS_List()
fi fi
ebs_backup_list=`echo "$ebs_backup_list_complete" | grep ^VOLUME | cut -f 2` ebs_backup_list=`echo "$ebs_backup_list_complete" | grep ^VOLUME | cut -f 2`
#code to right will output list of EBS volumes to be backed up: echo -e "Now outputting ebs_backup_list:\n$ebs_backup_list" #code to right will output list of EBS volumes to be backed up: echo -e "Now outputting ebs_backup_list:\n$ebs_backup_list"
fi
} }
create_EBS_Snapshot_Tags() create_EBS_Snapshot_Tags()
{ {
echo "create_EBS_Snapshot_Tags region_copy_scheduled_destinations=$region_copy_scheduled_destinations"
#snapshot tags holds all tags that need to be applied to a given snapshot - by aggregating tags we ensure that ec2-create-tags is called only onece #snapshot tags holds all tags that need to be applied to a given snapshot - by aggregating tags we ensure that ec2-create-tags is called only onece
snapshot_tags="" snapshot_tags=""
#if $name_tag_create is true then append ec2ab_${ebs_selected}_$current_date to the variable $snapshot_tags #if $name_tag_create is true then append ec2ab_${ebs_selected}_$current_date to the variable $snapshot_tags
@ -68,6 +86,15 @@ create_EBS_Snapshot_Tags()
snapshot_tags="$snapshot_tags --tag Volume=${ebs_selected} --tag Created=$current_date" snapshot_tags="$snapshot_tags --tag Volume=${ebs_selected} --tag Created=$current_date"
fi fi
#if $name_tag_create is true then append RegionCopy=[region]:scheduled,[region]:scheduled to the variable $snapshot_tags
if [[ -n $region_copy_scheduled_destinations ]]
then
ec2_snapshot_resource_id=`echo "$ec2_create_snapshot_result" | cut -f 2`
regions=$(echo $region_copy_scheduled_destinations | sed 's/ */:scheduled,/g')
snapshot_tags="$snapshot_tags --tag RegionCopy=${regions}:scheduled"
echo "tagging $ec2_snapshot_resource_id with $snapshot_tags"
fi
#if $snapshot_tags is not zero length then set the tag on the snapshot using ec2-create-tags #if $snapshot_tags is not zero length then set the tag on the snapshot using ec2-create-tags
if [[ -n $snapshot_tags ]] if [[ -n $snapshot_tags ]]
then echo "Tagging Snapshot $ec2_snapshot_resource_id with the following Tags:" then echo "Tagging Snapshot $ec2_snapshot_resource_id with the following Tags:"
@ -133,6 +160,50 @@ purge_EBS_Snapshots()
done done
} }
region_copy_EBS_Snapshots()
{
#snapshots_to_region_copy is a list of all snapshot_ids with RegionCopy=.*:scheduled.*
# tr and $'n' substitution used so $IFS doesn't have to be
# manipulated with for loops and the cut command
snapshot_tag_list=$(ec2-describe-tags --show-empty-fields --region $region --filter resource-type=snapshot --filter key=RegionCopy | grep ':scheduled' | cut -f3,5- | tr '[\t ]' '~')
for snapshot_data in $(echo ${snapshot_tag_list//$'\n'/ } | cut -f1-)
do
snapshot_id=$(echo $snapshot_data | cut -d'~' -f1)
regions=$(echo $snapshot_data | cut -d'~' -f2-)
original_regions=$regions
region_copy_tag=''
for region_data in ${regions//,/ }
do
destination_region=$(echo $region_data | cut -d: -f1)
status=$(echo $region_data | cut -d: -f2-)
region_copy_tag_append="$destination_region:$status"
if [[ "$region_copy_destinations" = "all" || -n $(echo "$region_copy_destinations"| grep $destination_region) ]]
then
current_datetime=$(date +%Y-%m-%d_%H:%M:%S)
if [ "$status" = "scheduled" ]
then
ec2_copy_snapshot_complete=$(ec2-copy-snapshot -r $region -s $snapshot_id --region $destination_region)
ec2_copy_snapshot_result=`echo $?`
if [[ $ec2_copy_snapshot_result -gt 0 ]]
then
echo -e "An error occured when running ec2-copy-snapshot. The error returned is below:\n$ec2_copy_snapshot_complete" 1>&2 ; exit 70
else
region_copy_tag_append="$destination_region:$current_datetime"
echo "The snapshot \"$snapshot_id\" is being copied to $destination_region."
fi
fi
fi
region_copy_tag="${region_copy_tag},$region_copy_tag_append"
done
if [[ "${region_copy_tag#,}" != "$original_regions" ]]
then
ec2-create-tags $snapshot_id --region $region --tag RegionCopy=${region_copy_tag#,}
fi
done
}
app_name=`basename $0` app_name=`basename $0`
#sets defaults #sets defaults
@ -148,7 +219,7 @@ user_tags=false
#sets the Purge Snapshot feature to false - this feature will eventually allow the removal of snapshots that have a "PurgeAfterFE" tag that is earlier than current date #sets the Purge Snapshot feature to false - this feature will eventually allow the removal of snapshots that have a "PurgeAfterFE" tag that is earlier than current date
purge_snapshots=false purge_snapshots=false
#handles options processing #handles options processing
while getopts :s:c:r:v:t:k:pnu opt while getopts :s:c:r:v:t:k:g:d:l:pnu opt
do do
case $opt in case $opt in
s) selection_method="$OPTARG";; s) selection_method="$OPTARG";;
@ -157,6 +228,8 @@ while getopts :s:c:r:v:t:k:pnu opt
v) volumeid="$OPTARG";; v) volumeid="$OPTARG";;
t) tag="$OPTARG";; t) tag="$OPTARG";;
k) purge_after_input="$OPTARG";; k) purge_after_input="$OPTARG";;
g) region_copy_scheduled_destinations="$OPTARG";;
d) region_copy_destinations="$OPTARG";;
n) name_tag_create=true;; n) name_tag_create=true;;
p) purge_snapshots=true;; p) purge_snapshots=true;;
u) user_tags=true;; u) user_tags=true;;
@ -222,3 +295,11 @@ if $purge_snapshots
then echo "Snapshot Purging is Starting Now." then echo "Snapshot Purging is Starting Now."
purge_EBS_Snapshots purge_EBS_Snapshots
fi fi
#if region_copy_destinations is true, then run region_copy_EBS_Snapshots function
if [[ -n $region_copy_destinations ]]
then echo "Snapshot Copying to regions $region_copy_destinations is Starting Now."
prerequisite_check ec2-copy-snapshot
region_copy_EBS_Snapshots
fi