Skip to content

Instantly share code, notes, and snippets.

@dwdraju
Last active May 7, 2022 18:04
Show Gist options
  • Select an option

  • Save dwdraju/d6bde53d16735bf2ae661be042a694e4 to your computer and use it in GitHub Desktop.

Select an option

Save dwdraju/d6bde53d16735bf2ae661be042a694e4 to your computer and use it in GitHub Desktop.
Send MySQL Backup to Google Cloud Storage(GCS)
#!/bin/bash
TIMESTAMP=$(date +"%Y-%m-%d_%H-%M-%S")
BACKUP_DIR="/home/ubuntu/backup/$TIMESTAMP"
MYSQL_USER="****"
MYSQL=/usr/bin/mysql
MYSQL_PASSWORD="****"
MYSQLDUMP=/usr/bin/mysqldump
mkdir -p "$BACKUP_DIR/"
databases="db1 db2 db3 db4"
for db in $databases; do
$MYSQLDUMP --force --opt --user=$MYSQL_USER -p$MYSQL_PASSWORD --databases $db --routines | gzip > "$BACKUP_DIR/$db-$TIMESTAMP.gz"
gsutil cp $BACKUP_DIR/$db-$TIMESTAMP.gz gs://mydb-$db-backup/
done
@ChekhWasTaken

Copy link
Copy Markdown

in case if someone wants to run this script with cron, gsutil would need additional context. add following to the beginning of the script.

export HOME=/home/user
export PATH=/snap/bin # otherwise need to call gsutil with full path
export BOTO_CONFIG=/home/user/.boto # for authentication

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment