#!/bin/bash # Exit immediately if a command fails set -e # Validate input parameters if [ "$#" -ne 2 ]; then echo "Usage: $0 " exit 1 fi # Assign command line arguments to variables RESTIC_PASSWORD="$1" CUSTOM_TAG="$2" # 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/litespeed/mb-backups/logs" LOG_FILE="${LOG_DIR}/backup_database_$(date +'%Y-%m-%d').log" LOCK_FILE="/tmp/restic_global.lock" # Ensure log directory exists mkdir -p "$LOG_DIR" # Logging function log() { echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE" } log "Starting Database Backup process with tags: wordpress_db, $CUSTOM_TAG." # Verify that the password file exists if [ ! -f "$PASSWORD_FILE" ]; then log "ERROR: Password file not found at $PASSWORD_FILE." 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=$(grep "define('DB_NAME'" "$WP_CONFIG" | cut -d "'" -f 4) DB_USER=$(grep "define('DB_USER'" "$WP_CONFIG" | cut -d "'" -f 4) DB_PASSWORD=$(grep "define('DB_PASSWORD'" "$WP_CONFIG" | cut -d "'" -f 4) DB_HOST=$(grep "define('DB_HOST'" "$WP_CONFIG" | cut -d "'" -f 4) 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 BACKUP_TAGS="wordpress_db,$CUSTOM_TAG" DUMP_FILE="/tmp/${DB_NAME}_$(date +'%Y-%m-%d_%H-%M-%S').sql" 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" else log "ERROR: Database dump failed." exit 1 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."