Created
May 12, 2025 13:13
-
-
Save gcgbarbosa/66fb3cc7752bfe9495827206345992b7 to your computer and use it in GitHub Desktop.
Backup MySQL on GCP to CloudStorage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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