Skip to content

Commit 271daf0

Browse files
rmceoinjacksegal
authored andcommitted
add project id option (#75)
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.
1 parent 52bd9b6 commit 271daf0

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ Options:
7272
Default if not set: 'gcs' [OPTIONAL]
7373
-a Service Account to use.
7474
Blank if not set [OPTIONAL]
75+
-j Project ID to use.
76+
Blank if not set [OPTIONAL]
7577
-n Dry run: causes script to print debug variables and doesn't execute any
7678
create / delete commands [OPTIONAL]
7779
```
@@ -154,6 +156,20 @@ For example:
154156

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

159+
### Project ID
160+
By default snapshots are created with the default gcloud project id. To use a custom project id use the -j flag:
161+
162+
Usage: ./gcloud-snapshot.sh [-j <project_id>]
163+
164+
Options:
165+
166+
-j Project ID to use.
167+
Blank if not set [OPTIONAL]
168+
169+
For example:
170+
171+
./gcloud-snapshot.sh -j "my-test-project"
172+
157173
### Dry Run
158174
The script can be run in dry run mode, which doesn't execute any create / delete commands, and prints out debug information.
159175

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

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

232-
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.
248+
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.

gcloud-snapshot.sh

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export PATH=$PATH:/usr/local/bin/:/usr/bin
2020
#
2121

2222
usage() {
23-
echo -e "\nUsage: $0 [-d <days>] [-r <remote_instances>] [-f <gcloud_filter_expression>] [-p <prefix>] [-a <service_account>] [-n <dry_run>]" 1>&2
23+
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
2424
echo -e "\nOptions:\n"
2525
echo -e " -d Number of days to keep snapshots. Snapshots older than this number deleted."
2626
echo -e " Default if not set: 7 [OPTIONAL]"
@@ -32,6 +32,8 @@ usage() {
3232
echo -e " Default if not set: 'gcs' [OPTIONAL]"
3333
echo -e " -a Service Account to use."
3434
echo -e " Blank if not set [OPTIONAL]"
35+
echo -e " -j Project ID to use."
36+
echo -e " Blank if not set [OPTIONAL]"
3537
echo -e " -n Dry run: causes script to print debug variables and doesn't execute any"
3638
echo -e " create / delete commands [OPTIONAL]"
3739
echo -e "\n"
@@ -45,7 +47,7 @@ usage() {
4547

4648
setScriptOptions()
4749
{
48-
while getopts ":d:rf:p:a:n" opt; do
50+
while getopts ":d:rf:p:a:j:n" opt; do
4951
case $opt in
5052
d)
5153
opt_d=${OPTARG}
@@ -62,6 +64,9 @@ setScriptOptions()
6264
a)
6365
opt_a=${OPTARG}
6466
;;
67+
j)
68+
opt_j=${OPTARG}
69+
;;
6570
n)
6671
opt_n=true
6772
;;
@@ -110,6 +115,13 @@ setScriptOptions()
110115
OPT_ACCOUNT=""
111116
fi
112117

118+
# gcloud Project
119+
if [[ -n $opt_j ]]; then
120+
OPT_PROJECT="--project $opt_j"
121+
else
122+
OPT_PROJECT=""
123+
fi
124+
113125
# Dry run
114126
if [[ -n $opt_n ]]; then
115127
DRY_RUN=$opt_n
@@ -122,6 +134,7 @@ setScriptOptions()
122134
printDebug "FILTER_CLAUSE=${FILTER_CLAUSE}"
123135
printDebug "PREFIX=${PREFIX}"
124136
printDebug "OPT_ACCOUNT=${OPT_ACCOUNT}"
137+
printDebug "OPT_PROJECT=${OPT_PROJECT}"
125138
printDebug "DRY_RUN=${DRY_RUN}"
126139
fi
127140
}
@@ -154,7 +167,7 @@ getInstanceName()
154167

155168
getDeviceList()
156169
{
157-
echo -e "$(gcloud $OPT_ACCOUNT compute disks list $1 --filter "$FILTER_CLAUSE" --format='value(name,zone,id)')"
170+
echo -e "$(gcloud $OPT_ACCOUNT compute disks list $1 --filter "$FILTER_CLAUSE" --format='value(name,zone,id)' $OPT_PROJECT)"
158171
}
159172

160173

@@ -202,9 +215,9 @@ createSnapshotName()
202215
createSnapshot()
203216
{
204217
if [ "$DRY_RUN" = true ]; then
205-
printCmd "gcloud ${OPT_ACCOUNT} compute disks snapshot $1 --snapshot-names $2 --zone $3"
218+
printCmd "gcloud ${OPT_ACCOUNT} compute disks snapshot $1 --snapshot-names $2 --zone $3 ${OPT_PROJECT}"
206219
else
207-
$(gcloud $OPT_ACCOUNT compute disks snapshot $1 --snapshot-names $2 --zone $3)
220+
$(gcloud $OPT_ACCOUNT compute disks snapshot $1 --snapshot-names $2 --zone $3 ${OPT_PROJECT})
208221
fi
209222
}
210223

@@ -233,7 +246,7 @@ deleteSnapshots()
233246
local snapshots=()
234247

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

238251
# loop through and get snapshot name from URI
239252
while read line
@@ -263,9 +276,9 @@ deleteSnapshots()
263276
deleteSnapshot()
264277
{
265278
if [ "$DRY_RUN" = true ]; then
266-
printCmd "gcloud ${OPT_ACCOUNT} compute snapshots delete $1 -q"
279+
printCmd "gcloud ${OPT_ACCOUNT} compute snapshots delete $1 -q ${OPT_PROJECT}"
267280
else
268-
$(gcloud $OPT_ACCOUNT compute snapshots delete $1 -q)
281+
$(gcloud $OPT_ACCOUNT compute snapshots delete $1 -q ${OPT_PROJECT})
269282
fi
270283
}
271284

@@ -361,4 +374,4 @@ main()
361374
## ##
362375
####################
363376

364-
main "$@"
377+
main "$@"

0 commit comments

Comments
 (0)