#!/bin/bash # 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" backupPath='/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" # Extract database credentials from wp-config.php 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 # Ensure log directory exists mkdir -p "$LOG_DIR" # Set Restic and MySQL environment variables export RESTIC_REPOSITORY="$backupPath" export RESTIC_PASSWORD export MYSQL_PWD="$DB_PASSWORD" # Logging start echo "[$(date +'%Y-%m-%d %H:%M:%S')] Starting Database Backup for $DB_NAME with tags: wordpress_db, $CUSTOM_TAG" | tee -a "$LOG_FILE" # Verify that the password file exists if [ ! -f "$password_file" ]; then echo "ERROR: Password file not found at $password_file" | tee -a "$LOG_FILE" exit 1 fi # Verify backup path exists if [ ! -d "$backupPath" ]; then echo "ERROR: Backup path $backupPath does not exist." | tee -a "$LOG_FILE" exit 1 fi # Perform database backup with both static and custom tags BACKUP_TAGS="wordpress_db,$CUSTOM_TAG" DUMP_FILE="${DB_NAME}_$(date +'%Y-%m-%d_%H-%M-%S').sql" if mysqldump -h "$DB_HOST" -P "$DB_PORT" -u "$DB_USER" "$DB_NAME" > "/tmp/$DUMP_FILE" && \ restic backup --stdin --stdin-filename "$DUMP_FILE" --tag "$BACKUP_TAGS"; then echo "[$(date +'%Y-%m-%d %H:%M:%S')] Database backup completed successfully with tags: $BACKUP_TAGS." | tee -a "$LOG_FILE" rm -f "/tmp/$DUMP_FILE" else echo "[$(date +'%Y-%m-%d %H:%M:%S')] ERROR: Database backup failed." | tee -a "$LOG_FILE" exit 1 fi # Logging end echo "[$(date +'%Y-%m-%d %H:%M:%S')] Database Backup process finished." | tee -a "$LOG_FILE"