#!/bin/bash # exit on any non-zero exit code: set -e # keep track of the last executed command trap 'last_command=$current_command; current_command=$BASH_COMMAND' DEBUG # echo an error message before exiting trap 'echo "\"${last_command}\" command filed with exit code $?."' EXIT if [[ -z "${GCP_GCS_BUCKET}" ]]; then echo "GCP_GCS_BUCKET env var not set" exit 1 fi if [[ -z "${MYSQL_ROOT_PASSWORD}" ]]; then echo "MYSQL_ROOT_PASSWORD env var not set" exit 1 fi if [[ -z "${GOOGLE_APPLICATION_CREDENTIALS}" ]]; then echo "GOOGLE_APPLICATION_CREDENTIALS env var not set" exit 1 fi if [[ ! -f "$GOOGLE_APPLICATION_CREDENTIALS" ]]; then echo "File $GOOGLE_APPLICATION_CREDENTIALS doesn't exist!" exit 1 fi gcloud auth activate-service-account \ --key-file $GOOGLE_APPLICATION_CREDENTIALS verificationDate=$(date +"%Y-%m-%d") # extract last filename from listing: # 1. list all files in bucket # 2. sort by date (2nd column) ascending # 3. get last 2 lines and remove the last one (it consists of summary) OUT=$(gsutil ls -l gs://$GCP_GCS_BUCKET/$BACKUPS_PATH | sort -k 2 | tail -2 | head -1) BFILE=$(echo $OUT | grep $verificationDate | cut -f3 -d ' ') if [ "${BFILE}" == "" ]; then echo -e "Can't extract filename (date: $verificationDate) from:\n\n$OUT" exit 1 fi echo "Fetching $BFILE" gsutil cp $BFILE /home/svc/backup.sql.gz echo "Extracting..." gunzip /home/svc/backup.sql.gz MYSQL_CONN="-h 127.0.0.1 --port=3306 --user=root \ --password="$MYSQL_ROOT_PASSWORD"" echo "Importing databases" mysql $MYSQL_CONN < /home/svc/backup.sql echo "Databases imported. Starting verification" # based on https://dba.stackexchange.com/a/60774 SQL="SET SESSION group_concat_max_len = 1048576;" SQL="${SQL} SELECT CONCAT('CHECK TABLE ',dbtblist,';') FROM" SQL="${SQL} (SELECT GROUP_CONCAT(table_schema,'.',table_name) dbtblist FROM" SQL="${SQL} information_schema.tables WHERE table_schema NOT IN" SQL="${SQL} ('information_schema','performance_schema','mysql')) A" mysql $MYSQL_CONN -ANe"${SQL}" | mysql ${MYSQL_CONN} echo "Shutting down the server" mysql $MYSQL_CONN -e "SHUTDOWN" echo "Verification finished. Looks good."