Skip to content

Commit

Permalink
Merge pull request #45 from greuff/master
Browse files Browse the repository at this point in the history
Enable backups for foreign instances
  • Loading branch information
jacksegal authored Sep 5, 2018
2 parents 67ce3b8 + 37a9310 commit 05ec3d5
Showing 1 changed file with 46 additions and 13 deletions.
59 changes: 46 additions & 13 deletions gcloud-snapshot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ export PATH=$PATH:/usr/local/bin/:/usr/bin
#

usage() {
echo -e "\nUsage: $0 [-d <days>] [-t <label_name>]" 1>&2
echo -e "\nUsage: $0 [-d <days>] [-t <label_name>] [-i <instance_name>] [-i <instance_zone>]" 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 " -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."
echo -e "\n"
exit 1
}
Expand All @@ -35,14 +38,20 @@ usage() {

setScriptOptions()
{
while getopts ":d:t:" o; do
while getopts ":d:t:i:z:" o; do
case "${o}" in
d)
opt_d=${OPTARG}
;;
t)
opt_t=${OPTARG}
;;
i)
opt_i=${OPTARG}
;;
z)
opt_z=${OPTARG}
;;
*)
usage
;;
Expand All @@ -61,6 +70,18 @@ setScriptOptions()
else
LABEL_CLAUSE=""
fi

if [[ -n $opt_i ]];then
OPT_INSTANCE_NAME=$opt_i
else
OPT_INSTANCE_NAME=""
fi

if [[ -n $opt_z ]];then
OPT_INSTANCE_ZONE=$opt_z
else
OPT_INSTANCE_ZONE=""
fi
}


Expand All @@ -70,11 +91,15 @@ setScriptOptions()

getInstanceName()
{
# get the name for this vm
local instance_name="$(curl -s "http://metadata.google.internal/computeMetadata/v1/instance/hostname" -H "Metadata-Flavor: Google")"

# strip out the instance name from the fullly qualified domain name the google returns
echo -e "${instance_name%%.*}"
if [[ -z "$OPT_INSTANCE_NAME" ]];then
# get the name for this vm
local instance_name="$(curl -s "http://metadata.google.internal/computeMetadata/v1/instance/hostname" -H "Metadata-Flavor: Google")"

# strip out the instance name from the fullly qualified domain name the google returns
echo -e "${instance_name%%.*}"
else
echo $OPT_INSTANCE_NAME
fi
}


Expand All @@ -84,7 +109,11 @@ getInstanceName()

getInstanceId()
{
echo -e "$(curl -s "http://metadata.google.internal/computeMetadata/v1/instance/id" -H "Metadata-Flavor: Google")"
if [[ -z "$OPT_INSTANCE_NAME" ]];then # no typo: only when querying for the calling machine get the real instance ID
echo -e "$(curl -s "http://metadata.google.internal/computeMetadata/v1/instance/id" -H "Metadata-Flavor: Google")"
else
echo $(echo $INSTANCE_NAME | md5sum | cut -d' ' -f1)
fi
}


Expand All @@ -94,10 +123,14 @@ getInstanceId()

getInstanceZone()
{
local instance_zone="$(curl -s "http://metadata.google.internal/computeMetadata/v1/instance/zone" -H "Metadata-Flavor: Google")"

# strip instance zone out of response
echo -e "${instance_zone##*/}"
if [[ -z "$OPT_INSTANCE_ZONE" ]];then
local instance_zone="$(curl -s "http://metadata.google.internal/computeMetadata/v1/instance/zone" -H "Metadata-Flavor: Google")"

# strip instance zone out of response
echo -e "${instance_zone##*/}"
else
echo $OPT_INSTANCE_ZONE
fi
}


Expand Down Expand Up @@ -342,4 +375,4 @@ createSnapshotWrapper
deleteSnapshotsWrapper

# log time
logTime "End of Script"
logTime "End of Script"

0 comments on commit 05ec3d5

Please sign in to comment.