-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsync_buckets.sh
More file actions
74 lines (62 loc) · 2.09 KB
/
sync_buckets.sh
File metadata and controls
74 lines (62 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/bin/bash -eu
#
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Function to print usage
usage() {
echo "Usage: $0 GCP_PROJECT_ID SRC_BUCKET DEST_BUCKET SECRET_NAME"
exit 1
}
# Check if all environment variables are set
if [ -z "$GCP_PROJECT_ID" ] || [ -z "$SRC_BUCKET" ] || [ -z "$DEST_BUCKET" ] || [ -z "$SECRET_NAME" ]; then
echo "Error: Missing environment variables."
usage
fi
echo "Batch process started."
# Record the start time
start_time=$(date +%s)
# set project to access the secret
gcloud config set project $GCP_PROJECT_ID
if [ $? -ne 0 ]; then
echo "Error: Setting up the prject $GCP_PROJECT_ID"
exit 1
fi
# Fetch the secret and save it to /tmp/key.json
gcloud secrets versions access latest --secret="$SECRET_NAME" --quiet > /tmp/key.json
if [ $? -ne 0 ]; then
echo "Error: Failed to fetch the secret."
exit 1
fi
# Activate gcloud with the key
gcloud auth activate-service-account --key-file=/tmp/key.json
if [ $? -ne 0 ]; then
echo "Error: Failed to activate service account."
rm -f /tmp/key.json
exit 1
fi
# Run gcloud rsync command
gcloud storage rsync -r "gs://$SRC_BUCKET" "gs://$DEST_BUCKET"
if [ $? -ne 0 ]; then
echo "Error: Failed to sync buckets."
rm -f /tmp/key.json
exit 1
fi
# Clean up
rm -f /tmp/key.json
# Record the end time
end_time=$(date +%s)
elapsed_time=$((end_time - start_time))
echo "Batch process completed successfully in $elapsed_time seconds."
exit 0
34,1 96%