-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_backup.sh
More file actions
23 lines (19 loc) · 902 Bytes
/
run_backup.sh
File metadata and controls
23 lines (19 loc) · 902 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/bin/bash
# Must set:
# SQL_HOST, SQL_USER, SQL_PASS, SQL_DB, GCP_GS_BUCKET
gcloud auth activate-service-account --key-file /etc/gcp-sa/service-account.json
PREFIX=bs12
DATE=$(date '+%Y%m%d%H%M')
FILENAME=`echo ${PREFIX}_${DATE}.sql`
mysqldump -h ${SQL_HOST} --user=${SQL_USER} --password=${SQL_PASS} ${SQL_DB} > /tmp/${FILENAME}
# remove last line so diffs can function properly
sed -i '$ d' tmp/${FILENAME}
LAST_GS_FILE=`gsutil ls -l gs://${GCP_GS_BUCKET}/ | sort -k 2 | tail -n 2 | head -n 1 | awk '{ print $3 }'`
LAST_GS_HASH=`gsutil stat ${LAST_GS_FILE} | grep crc32 | awk '{ print $3 }'`
NEW_BACKUP_HASH=`gsutil hash -c /tmp/${FILENAME} | head -2 | tail -1 | awk '{ print $3 }'`
if [ "${NEW_BACKUP_HASH}" == "${LAST_GS_HASH}" ]; then
echo "Hashes are equal, exiting..."
else
echo "Hashes are unique, uploading..."
gsutil cp /tmp/${FILENAME} gs://${GCP_GS_BUCKET}/${FILENAME}
fi