mb-backup-manager/scripts/imports/backup_database.sh

122 lines
3.4 KiB
Bash
Raw Normal View History

2024-09-18 16:53:20 +00:00
#!/bin/bash
# Exit immediately if a command fails
set -e
2024-09-18 16:53:20 +00:00
# Validate input parameters
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <RESTIC_PASSWORD> <ADDITIONAL_TAG>"
exit 1
fi
# Assign command line arguments to variables
RESTIC_PASSWORD="$1"
2025-01-06 17:22:25 +00:00
CUSTOM_TAG="$2"
2024-09-18 16:53:20 +00:00
# Configuration
APP_PATH='/var/www/webroot/ROOT'
WP_CONFIG="${APP_PATH}/wp-config.php"
BACKUP_PATH='/mnt/backups'
PASSWORD_FILE="/etc/restic-password"
LOG_DIR="/home/jelastic/mb-backups/logs"
2024-09-18 16:53:20 +00:00
LOG_FILE="${LOG_DIR}/backup_database_$(date +'%Y-%m-%d').log"
LOCK_FILE="/tmp/restic_global.lock"
2024-09-18 16:53:20 +00:00
# Ensure log directory exists
mkdir -p "$LOG_DIR"
# Logging function
log() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}
2024-09-18 16:53:20 +00:00
log "Starting Database Backup process with tags: wordpress_db, $CUSTOM_TAG."
2024-09-18 16:53:20 +00:00
2025-01-06 17:22:25 +00:00
# Verify that the password file exists
if [ ! -f "$PASSWORD_FILE" ]; then
log "ERROR: Password file not found at $PASSWORD_FILE."
2024-11-12 18:15:11 +00:00
exit 1
fi
# Export the password from the file to ensure Restic uses it automatically
export RESTIC_PASSWORD=$(cat "$PASSWORD_FILE")
# Verify backup path exists
if [ ! -d "$BACKUP_PATH" ]; then
log "ERROR: Backup path $BACKUP_PATH does not exist."
exit 1
fi
# Acquire a global lock to serialize Restic operations
log "Acquiring global lock for Restic operations..."
exec 9>"$LOCK_FILE"
if ! flock -n 9; then
log "Another Restic operation is running. Exiting."
exit 1
fi
log "Global lock acquired."
# Check and remove stale locks
log "Checking for stale locks in the repository..."
if restic -r "$BACKUP_PATH" list locks | grep -q "lock"; then
log "Stale locks detected. Unlocking the repository..."
restic -r "$BACKUP_PATH" unlock
log "Repository unlocked successfully."
else
log "No stale locks found."
fi
# Extract database credentials from wp-config.php
if [ ! -f "$WP_CONFIG" ]; then
log "ERROR: wp-config.php not found at $WP_CONFIG."
exit 1
fi
DB_NAME=$(awk -F"'" '/define\( *'"'"'DB_NAME'"'"'/{print $4}' "$WP_CONFIG")
DB_USER=$(awk -F"'" '/define\( *'"'"'DB_USER'"'"'/{print $4}' "$WP_CONFIG")
DB_PASSWORD=$(awk -F"'" '/define\( *'"'"'DB_PASSWORD'"'"'/{print $4}' "$WP_CONFIG")
DB_HOST=$(awk -F"'" '/define\( *'"'"'DB_HOST'"'"'/{print $4}' "$WP_CONFIG")
# Set default DB_HOST if empty
if [ -z "$DB_HOST" ]; then
DB_HOST="localhost"
fi
DB_PORT=3306 # Default MySQL port
# Validate database credentials
if [ -z "$DB_NAME" ] || [ -z "$DB_USER" ] || [ -z "$DB_PASSWORD" ] || [ -z "$DB_HOST" ]; then
log "ERROR: Failed to extract database credentials from wp-config.php."
exit 1
fi
# Set MySQL environment variable
export MYSQL_PWD="$DB_PASSWORD"
# Perform database backup with Restic
2025-01-06 17:22:25 +00:00
BACKUP_TAGS="wordpress_db,$CUSTOM_TAG"
DUMP_FILE="/tmp/${DB_NAME}_$(date +'%Y-%m-%d_%H-%M-%S').sql"
2024-11-12 18:15:11 +00:00
log "Performing database dump for $DB_NAME..."
if mysqldump -h "$DB_HOST" -P "$DB_PORT" -u "$DB_USER" "$DB_NAME" > "$DUMP_FILE"; then
log "Database dump created successfully: $DUMP_FILE"
2024-09-18 16:53:20 +00:00
else
log "ERROR: Database dump failed."
2024-11-12 18:15:11 +00:00
exit 1
2024-09-18 16:53:20 +00:00
fi
log "Backing up database dump to Restic repository with tags: $BACKUP_TAGS..."
if restic -r "$BACKUP_PATH" backup --stdin --stdin-filename "$(basename "$DUMP_FILE")" --tag "$BACKUP_TAGS" < "$DUMP_FILE"; then
log "Database backup completed successfully with tags: $BACKUP_TAGS."
rm -f "$DUMP_FILE"
else
log "ERROR: Restic backup failed."
rm -f "$DUMP_FILE"
exit 1
fi
# Release global lock
exec 9>&-
log "Database Backup process finished successfully."