2024-09-18 16:53:20 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2025-01-07 14:48:04 +00:00
|
|
|
# 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"
|
2025-01-07 14:48:04 +00:00
|
|
|
BACKUP_PATH='/mnt/backups'
|
|
|
|
PASSWORD_FILE="/etc/restic-password"
|
2025-01-06 17:22:25 +00:00
|
|
|
LOG_DIR="/home/litespeed/mb-backups/logs"
|
2024-09-18 16:53:20 +00:00
|
|
|
LOG_FILE="${LOG_DIR}/backup_database_$(date +'%Y-%m-%d').log"
|
2025-01-07 14:48:04 +00:00
|
|
|
LOCK_FILE="/tmp/restic_global.lock"
|
2024-09-18 16:53:20 +00:00
|
|
|
|
|
|
|
# Ensure log directory exists
|
|
|
|
mkdir -p "$LOG_DIR"
|
|
|
|
|
2025-01-07 14:48:04 +00:00
|
|
|
# Logging function
|
|
|
|
log() {
|
|
|
|
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
|
|
|
|
}
|
2024-09-18 16:53:20 +00:00
|
|
|
|
2025-01-07 14:48:04 +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
|
2025-01-07 14:48:04 +00:00
|
|
|
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
|
|
|
|
|
2025-01-07 14:48:04 +00:00
|
|
|
# Export the password from the file to ensure Restic uses it automatically
|
|
|
|
export RESTIC_PASSWORD=$(cat "$PASSWORD_FILE")
|
|
|
|
|
2025-01-06 16:30:34 +00:00
|
|
|
# Verify backup path exists
|
2025-01-07 14:48:04 +00:00
|
|
|
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."
|
2025-01-06 16:30:34 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2025-01-07 14:48:04 +00:00
|
|
|
# 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"
|
2025-01-07 14:48:04 +00:00
|
|
|
DUMP_FILE="/tmp/${DB_NAME}_$(date +'%Y-%m-%d_%H-%M-%S').sql"
|
2024-11-12 18:15:11 +00:00
|
|
|
|
2025-01-07 14:48:04 +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
|
2025-01-07 14:48:04 +00:00
|
|
|
log "ERROR: Database dump failed."
|
2024-11-12 18:15:11 +00:00
|
|
|
exit 1
|
2024-09-18 16:53:20 +00:00
|
|
|
fi
|
|
|
|
|
2025-01-07 14:48:04 +00:00
|
|
|
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."
|