Skip to content

Instantly share code, notes, and snippets.

@gcgbarbosa
Created May 12, 2025 13:13
Show Gist options
  • Save gcgbarbosa/66fb3cc7752bfe9495827206345992b7 to your computer and use it in GitHub Desktop.
Save gcgbarbosa/66fb3cc7752bfe9495827206345992b7 to your computer and use it in GitHub Desktop.
Backup MySQL on GCP to CloudStorage
#!/bin/bash
# Database credentials
USER="root"
PASSWORD="garimpypsiu"
DB_NAME="psiubotprod"
# Backup directory (create this directory if it doesn't exist)
BACKUP_DIR=""
# Get the current timestamp for the filename
TIMESTAMP=$(date +%Y%m%d-%H%M%S)
# Create the backup file name
BACKUP_FILE="${BACKUP_DIR}/${DB_NAME}_${TIMESTAMP}.sql.gz"
# Run mysqldump to create the backup
mysqldump -u "$USER" -p"$PASSWORD" "$DB_NAME" | gzip > "$BACKUP_FILE"
# (Optional) Remove old backups
find "$BACKUP_DIR" -type f -mtime +30 -delete # Delete backups older than 7 days
# Transfer backed files to cloudstorage
gsutil -m rsync -d -r $BACKUP_DIR gs://<bucket-address>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment