Skip to content

Instantly share code, notes, and snippets.

@chirag-chhajed
Created July 26, 2024 06:52
Show Gist options
  • Save chirag-chhajed/43c8dc950f073a5e4ed368f46f9e54ad to your computer and use it in GitHub Desktop.
Save chirag-chhajed/43c8dc950f073a5e4ed368f46f9e54ad to your computer and use it in GitHub Desktop.
Sample bash script for Azure to take backups of your MySQL DB
#!/bin/bash
# Sample backup file
# Azure cli needs to be installed and configured first
# https://learn.microsoft.com/en-us/cli/azure/install-azure-cli
# above link will help you to install and configure azure cli
# This script only works on the same machine where the MySQL server is running,
# If you want to use this script on a different machine, you need to also add the hostname and port to the mysqldump command
# e.g mysqldump -h hostname -P port -u $DB_USER -p$DB_PASS $db_name | gzip > $backup_file
# because at default it assumes that the MySQL server is running on the same machine
# MySQL credentials
DB_USER="your_db_user"
DB_PASS="your_db_password"
DB_NAME_1="DB_NAME_1"
DB_NAME_2="DB_NAME_2"
DB_NAME_3="DB_NAME_3"
# Backup directory
BACKUP_DIR="/storage/backup"
# Azure Blob Storage details
AZURE_STORAGE_ACCOUNT="your_storage_account"
AZURE_CONTAINER="your_container"
AZURE_PREFIX="mysql_backups" # Optional: Azure prefix for organizing backups
# Create backup directory if it doesn't exist
mkdir -p $BACKUP_DIR
# Generate timestamp
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
# Function to backup a database and upload to Azure
backup_and_upload() {
local db_name=$1
local backup_file="$BACKUP_DIR/${db_name}_${TIMESTAMP}.sql.gz"
echo "Backing up $db_name..."
mysqldump -u $DB_USER -p$DB_PASS $db_name | gzip > $backup_file
echo "Uploading $db_name backup to Azure..."
az storage blob upload --account-name $AZURE_STORAGE_ACCOUNT --container-name $AZURE_CONTAINER --name "${AZURE_PREFIX}/${db_name}_${TIMESTAMP}.sql.gz" --file $backup_file
if [ $? -eq 0 ]; then
echo "Successfully backed up and uploaded $db_name"
else
echo "Error backing up or uploading $db_name"
fi
}
# Backup and upload each database
backup_and_upload $DB_NAME_1
backup_and_upload $DB_NAME_2
backup_and_upload $DB_NAME_3
# Delete local backups older than 7 days
echo "Cleaning up local backups older than 7 days..."
find $BACKUP_DIR -name "*.sql.gz" -type f -mtime +7 -delete
# Delete Azure blobs older than 7 days
echo "Cleaning up Azure backups older than 7 days..."
SEVEN_DAYS_AGO=$(date -u -d '7 days ago' +'%Y-%m-%dT%H:%M:%SZ')
OLD_BLOBS=$(az storage blob list --account-name $AZURE_STORAGE_ACCOUNT --container-name $AZURE_CONTAINER --prefix $AZURE_PREFIX --query "[?properties.creationTime<='$SEVEN_DAYS_AGO'].name" -o tsv)
if [ -n "$OLD_BLOBS" ]; then
for blob in $OLD_BLOBS; do
az storage blob delete --account-name $AZURE_STORAGE_ACCOUNT --container-name $AZURE_CONTAINER --name $blob
echo "Deleted old Azure backup: $blob"
done
else
echo "No old Azure backups to delete"
fi
echo "Backup process completed"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment