Skip to content

Instantly share code, notes, and snippets.

@jlainezs
Created January 12, 2024 07:31
Show Gist options
  • Save jlainezs/198c6d4dffe8a1ebd855033986d06831 to your computer and use it in GitHub Desktop.
Save jlainezs/198c6d4dffe8a1ebd855033986d06831 to your computer and use it in GitHub Desktop.
Downloads last database dump from Google Cloud and restores it in a local database
#!/usr/bin/env bash
# ------------------------------------------------
# Downloads last backup from production
# and restores it into the local database
#
# requires:
# - gcloud with authenticated access
# - mysqlcredentials.cnf file
# ------------------------------------------------
# backet and path: my_bucket/a/path
BACKUP_BUCKET=XXXXXXXXXXXXXXXXX
LOCAL_BACKUP_DIRECTORY=./backup/Bdd
FILE_NAME=prod_backup
DOWNLOADED_FILE=$LOCAL_BACKUP_DIRECTORY/$FILE_NAME.gz
SQL_FILE=$LOCAL_BACKUP_DIRECTORY/$FILE_NAME
DB_CREDENTIALS_FILE=./mysqlcredentials.cnf
echo "Removing old sql files..."
rm *.sql
echo "Downloading last database backup..."
gsutil ls -r gs://$BACKUP_BUCKET | sort -k 2 |tail -2|head -1|gsutil -m cp -I $DOWNLOADED_FILE
echo "Uncompressing $DOWNLOADED_FILE..."
gzip -dkf $DOWNLOADED_FILE
echo "Archiving $DOWNLOADED_FILE..."
mv $DOWNLOADED_FILE $LOCAL_BACKUP_DIRECTORY/arxiu
echo "Restoring extracted backup file $SQL_FILE"
mysql --defaults-extra-file=$DB_CREDENTIALS_FILE<"$SQL_FILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment