Skip to content

Commit

Permalink
Add option to provide patterns for storage backups.
Browse files Browse the repository at this point in the history
  • Loading branch information
sKoreman committed Nov 14, 2024
1 parent f0ec86f commit f6f0fe4
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions util/backup-cloud-storage/backup-cloud-storage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,48 @@ if [ -z "${B2_APPLICATION_KEY}" ]; then
has_errors=true
fi

if [ -z "${BACKUP_PATTERNS}" ]; then
echo "BACKUP_PATTERNS is not set."
has_errors=true
fi

if [ "${has_errors}" = true ]; then
exit 1
fi

b2 account authorize

DATE=$(date +"%Y-%m-%d_%H:%M:%S")

mkdir "/tmp/${DATE}"
BUCKETS=`gsutil ls`
BUCKETS=$(gsutil ls)

IFS=',' read -r -a PATTERNS <<< "$BACKUP_PATTERNS"

for bucket in $BUCKETS; do
BUCKETNAME=$(echo "${bucket}" | sed 's/gs:\/\/*//g' | sed 's/.$//')
FILENAME="${BUCKETNAME}_${DATE}"
if [[ $bucket =~ "-prd/" || $bucket =~ "-production/" ]]; then
if [[ "$(gsutil du -s ${bucket})" == 0* ]]; then

match=false

for pattern in "${PATTERNS[@]}"; do
if [[ $BUCKETNAME == "$pattern" || $BUCKETNAME == *"${pattern#\*}" ]]; then
match=true
break
fi
done

if [ "$match" = true ]; then
if [[ "$(gsutil du -s "${bucket}")" == 0* ]]; then
echo "Skipping empty bucket '${BUCKETNAME}'"
else
echo "Backing up bucket '${BUCKETNAME}'"
gsutil -m cp -r "${bucket}" "/tmp/${DATE}"

# Remove files that are too big, because they cause zip to fail!
# http://infozip.sourceforge.net/FAQ.html#limits
find "/tmp/${DATE}" -size +209715200c -exec rm {} \;

zip -r9 "/tmp/${FILENAME}".zip "/tmp/${DATE}/${BUCKETNAME}"
zip -r9 "/tmp/${FILENAME}.zip" "/tmp/${DATE}/${BUCKETNAME}"
openssl aes-256-cbc -md md5 -in "/tmp/${FILENAME}.zip" -out "/tmp/${FILENAME}.zip.encrypted" -pass "pass:${B2_ENCRYPTION_KEY}"
b2 file upload "${B2_BUCKET}" "/tmp/${FILENAME}.zip.encrypted" "${BUCKETNAME}/${FILENAME}.zip.encrypted"
rm "/tmp/${FILENAME}".*
Expand All @@ -56,4 +73,4 @@ for bucket in $BUCKETS; do
fi
done

echo "Backup of cloud storage buckets completed."
echo "Backup of cloud storage buckets completed!"

0 comments on commit f6f0fe4

Please sign in to comment.