Skip to content

Commit

Permalink
Merge branch 'pr/56'
Browse files Browse the repository at this point in the history
  • Loading branch information
jacksegal committed Oct 8, 2018
2 parents 46dd37f + 2b81e83 commit 4dc45dd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@ Example: If you set the label to "auto_snapshot", only disks matching this key/v

auto_snapshot=true

Use -T for a more flexible way to specify the disks to snapshot.
Usage: ./gcloud-compute-snapshot.sh [-T <gcloud_filter_expression>]

Options:

-T Only back up disks returned from querying with this filter. Uses gcloud filter expressions"
If both -t and -T are used, both terms are joined by the operator AND"

Example: `./gcloud-compute-snapshot.sh -t auto_snapshot -T "sizeGb = 10 AND name: ubuntu"`. Attached disks matching this expression will be snapshotted

--format="labels.auto_snapshot=true AND sizeGb = 10 AND name: ubuntu"

This also allows you to bake snapshotting into your Google images by setting a cron job with a label on every image you create, and then you can set a label on the volumes you want to
snapshot in your infrastructure management tool (Terraform) to selectively snapshot them.

Expand Down
15 changes: 12 additions & 3 deletions gcloud-snapshot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ export PATH=$PATH:/usr/local/bin/:/usr/bin
#

usage() {
echo -e "\nUsage: $0 [-d <days>] [-t <label_name>] [-i <instance_name>] [-z <instance_zone>] [-p <prefix>] [-a <service_account>]" 1>&2
echo -e "\nUsage: $0 [-d <days>] [-t <label_name>] [-T <gcloud_filter_expression>] [-i <instance_name>] [-z <instance_zone>] [-p <prefix>] [-a <service_account>]" 1>&2
echo -e "\nOptions:\n"
echo -e " -d Number of days to keep snapshots. Snapshots older than this number deleted."
echo -e " Default if not set: 7 [OPTIONAL]"
echo -e " -t Only back up disks that have this specified label with value set to 'true'."
echo -e " -T Only back up disks returned from querying with this filter. Uses gcloud filter expressions"
echo -e " If both -t and -T are used, both terms are joined by the operator AND"
echo -e " -i Instance name to create backups for. If empty, makes backup for the calling"
echo -e " host."
echo -e " -z Instance zone. If empty, uses the zone of the calling host."
Expand All @@ -40,14 +42,17 @@ usage() {

setScriptOptions()
{
while getopts ":d:t:i:z:p:a:" o; do
while getopts ":d:t:T:i:z:p:a:" o; do
case "${o}" in
d)
opt_d=${OPTARG}
;;
t)
opt_t=${OPTARG}
;;
T)
opt_T=${OPTARG}
;;
i)
opt_i=${OPTARG}
;;
Expand Down Expand Up @@ -79,6 +84,10 @@ setScriptOptions()
LABEL_CLAUSE=""
fi

if [[ -n $opt_T ]];then
FILTER_CLAUSE="$LABEL_CLAUSE AND $opt_T"
fi

if [[ -n $opt_i ]];then
OPT_INSTANCE_NAME=$opt_i
else
Expand Down Expand Up @@ -162,7 +171,7 @@ getInstanceZone()

getDeviceList()
{
echo -e "$(gcloud $OPT_INSTANCE_SERVICE_ACCOUNT compute disks list --filter "users~instances/$1\$ $LABEL_CLAUSE" --format='value(name)')"
echo -e "$(gcloud $OPT_INSTANCE_SERVICE_ACCOUNT compute disks list --filter "users~instances/$1\$ $FILTER_CLAUSE" --format='value(name)')"
}


Expand Down

0 comments on commit 4dc45dd

Please sign in to comment.