Skip to content

Commit

Permalink
add project id option (#75)
Browse files Browse the repository at this point in the history
Added a project id option -j so that the script can be run to backup multiple projects without needing to change the gcloud config core.project each time. Here's an example of how we'll be using the feature.
  • Loading branch information
rmceoin authored and jacksegal committed Nov 20, 2018
1 parent 52bd9b6 commit 271daf0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ Options:
Default if not set: 'gcs' [OPTIONAL]
-a Service Account to use.
Blank if not set [OPTIONAL]
-j Project ID to use.
Blank if not set [OPTIONAL]
-n Dry run: causes script to print debug variables and doesn't execute any
create / delete commands [OPTIONAL]
```
Expand Down Expand Up @@ -154,6 +156,20 @@ For example:

./gcloud-snapshot.sh -a "[email protected]"

### Project ID
By default snapshots are created with the default gcloud project id. To use a custom project id use the -j flag:

Usage: ./gcloud-snapshot.sh [-j <project_id>]

Options:

-j Project ID to use.
Blank if not set [OPTIONAL]

For example:

./gcloud-snapshot.sh -j "my-test-project"

### Dry Run
The script can be run in dry run mode, which doesn't execute any create / delete commands, and prints out debug information.

Expand Down Expand Up @@ -229,4 +245,4 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 changes: 22 additions & 9 deletions gcloud-snapshot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export PATH=$PATH:/usr/local/bin/:/usr/bin
#

usage() {
echo -e "\nUsage: $0 [-d <days>] [-r <remote_instances>] [-f <gcloud_filter_expression>] [-p <prefix>] [-a <service_account>] [-n <dry_run>]" 1>&2
echo -e "\nUsage: $0 [-d <days>] [-r <remote_instances>] [-f <gcloud_filter_expression>] [-p <prefix>] [-a <service_account>] [-n <dry_run>] [-j <project_id>]" 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]"
Expand All @@ -32,6 +32,8 @@ usage() {
echo -e " Default if not set: 'gcs' [OPTIONAL]"
echo -e " -a Service Account to use."
echo -e " Blank if not set [OPTIONAL]"
echo -e " -j Project ID to use."
echo -e " Blank if not set [OPTIONAL]"
echo -e " -n Dry run: causes script to print debug variables and doesn't execute any"
echo -e " create / delete commands [OPTIONAL]"
echo -e "\n"
Expand All @@ -45,7 +47,7 @@ usage() {

setScriptOptions()
{
while getopts ":d:rf:p:a:n" opt; do
while getopts ":d:rf:p:a:j:n" opt; do
case $opt in
d)
opt_d=${OPTARG}
Expand All @@ -62,6 +64,9 @@ setScriptOptions()
a)
opt_a=${OPTARG}
;;
j)
opt_j=${OPTARG}
;;
n)
opt_n=true
;;
Expand Down Expand Up @@ -110,6 +115,13 @@ setScriptOptions()
OPT_ACCOUNT=""
fi

# gcloud Project
if [[ -n $opt_j ]]; then
OPT_PROJECT="--project $opt_j"
else
OPT_PROJECT=""
fi

# Dry run
if [[ -n $opt_n ]]; then
DRY_RUN=$opt_n
Expand All @@ -122,6 +134,7 @@ setScriptOptions()
printDebug "FILTER_CLAUSE=${FILTER_CLAUSE}"
printDebug "PREFIX=${PREFIX}"
printDebug "OPT_ACCOUNT=${OPT_ACCOUNT}"
printDebug "OPT_PROJECT=${OPT_PROJECT}"
printDebug "DRY_RUN=${DRY_RUN}"
fi
}
Expand Down Expand Up @@ -154,7 +167,7 @@ getInstanceName()

getDeviceList()
{
echo -e "$(gcloud $OPT_ACCOUNT compute disks list $1 --filter "$FILTER_CLAUSE" --format='value(name,zone,id)')"
echo -e "$(gcloud $OPT_ACCOUNT compute disks list $1 --filter "$FILTER_CLAUSE" --format='value(name,zone,id)' $OPT_PROJECT)"
}


Expand Down Expand Up @@ -202,9 +215,9 @@ createSnapshotName()
createSnapshot()
{
if [ "$DRY_RUN" = true ]; then
printCmd "gcloud ${OPT_ACCOUNT} compute disks snapshot $1 --snapshot-names $2 --zone $3"
printCmd "gcloud ${OPT_ACCOUNT} compute disks snapshot $1 --snapshot-names $2 --zone $3 ${OPT_PROJECT}"
else
$(gcloud $OPT_ACCOUNT compute disks snapshot $1 --snapshot-names $2 --zone $3)
$(gcloud $OPT_ACCOUNT compute disks snapshot $1 --snapshot-names $2 --zone $3 ${OPT_PROJECT})
fi
}

Expand Down Expand Up @@ -233,7 +246,7 @@ deleteSnapshots()
local snapshots=()

# get list of snapshots from gcloud for this device
local gcloud_response="$(gcloud $OPT_ACCOUNT compute snapshots list --filter="name~'"$1"' AND creationTimestamp<'$2' AND sourceDiskId='$3'" --uri)"
local gcloud_response="$(gcloud $OPT_ACCOUNT compute snapshots list --filter="name~'"$1"' AND creationTimestamp<'$2' AND sourceDiskId='$3'" --uri ${OPT_PROJECT})"

# loop through and get snapshot name from URI
while read line
Expand Down Expand Up @@ -263,9 +276,9 @@ deleteSnapshots()
deleteSnapshot()
{
if [ "$DRY_RUN" = true ]; then
printCmd "gcloud ${OPT_ACCOUNT} compute snapshots delete $1 -q"
printCmd "gcloud ${OPT_ACCOUNT} compute snapshots delete $1 -q ${OPT_PROJECT}"
else
$(gcloud $OPT_ACCOUNT compute snapshots delete $1 -q)
$(gcloud $OPT_ACCOUNT compute snapshots delete $1 -q ${OPT_PROJECT})
fi
}

Expand Down Expand Up @@ -361,4 +374,4 @@ main()
## ##
####################

main "$@"
main "$@"

0 comments on commit 271daf0

Please sign in to comment.