Skip to content
This repository was archived by the owner on Sep 3, 2022. It is now read-only.

Commit 690b9f4

Browse files
author
Yasser Elsayed
authored
Cleanup backup tar after copying to GCS (#1152)
Cleanup backup tar after copying to GCS, and try using project_id as bucket name if other options failed
1 parent 2f9c035 commit 690b9f4

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

containers/datalab/content/GCSbackup.sh

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,28 @@ timestamp=$(date "+%Y%m%d%H%M%S")
9696
project_id=${project_id:-$VM_PROJECT}
9797
zone=${zone:-${VM_ZONE}}
9898
machine_name=${machine_name:-$VM_NAME}
99-
default_bucket="${project_id}.appspot.com"
10099
tag="${tag:-backup}"
101100
num_backups=${num_backups:-10}
102-
gcs_bucket=${gcs_bucket:-$default_bucket}
101+
102+
# If no bucket is provided, try $project_id.appspot.com, then try $project_id
103+
if [ -z "${gcs_bucket}" ]; then
104+
default_bucket="${project_id}.appspot.com"
105+
echo "Will use ${default_bucket} for bucket name"
106+
gsutil ls "gs://${default_bucket}" &>/dev/null && gcs_bucket="${default_bucket}" || {
107+
# We cannot create the $project_id.appspot.com bucket, so don't try that
108+
gcs_bucket=${project_id}
109+
echo "Could not list bucket ${default_bucket}. Next will use ${gcs_bucket} for bucket name"
110+
gsutil ls "gs://${gcs_bucket}" &>/dev/null || {
111+
gsutil mb gs://"${gcs_bucket}"
112+
}
113+
}
114+
else
115+
gsutil ls gs://"${gcs_bucket}" &>/dev/null || {
116+
echo "Could not list bucket '${gcs_bucket}'. Will try to create it.."
117+
gsutil mb gs://"${gcs_bucket}"
118+
}
119+
fi
120+
103121
backup_path=`readlink -f "${backup_path:-.}"`
104122

105123
echo "tag: ${tag}"
@@ -120,12 +138,6 @@ if [[ -z $machine_name || -z $project_id || -z $zone ]]; then
120138
exit 1
121139
fi
122140

123-
# test and create bucket if necessary
124-
gsutil ls gs://${gcs_bucket} &>/dev/null || {
125-
echo "Bucket '${gcs_bucket}' was not found. Creating it.."
126-
gsutil mb gs://"${gcs_bucket}"
127-
}
128-
129141
# create an archive of the backup path
130142
archive_name=$(mktemp -d)"/archive.tar"
131143
echo "Creating archive: $archive_name"
@@ -159,11 +171,13 @@ echo "New archive md5 hash: ${new_backup_hash}"
159171
echo "Last backup md5 hash: ${last_backup_hash}"
160172
if [[ $new_backup_hash == $last_backup_hash ]]; then
161173
echo "Hash not different from last backup. Skipping this backup round." | tee -a $log_file
174+
rm -f "${archive_name}"
162175
exit 0
163176
fi
164177

165178
# copying backup to GCS
166179
gsutil cp ${archive_name} "gs://${backup_id}"
180+
rm -f "${archive_name}"
167181

168182
# remove excessive backups
169183
all_backups=($(gsutil ls "gs://${gcs_bucket}/datalab-backups/${zone}/${machine_name}${backup_path}/${tag}-*"))

0 commit comments

Comments
 (0)