2024-09-18 16:53:20 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
# 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"
|
|
|
|
backupPath='/mnt/backups'
|
2024-11-12 18:15:11 +00:00
|
|
|
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-06 16:30:34 +00:00
|
|
|
# Extract database credentials from wp-config.php
|
2025-01-06 17:22:25 +00:00
|
|
|
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
|
2024-09-18 16:53:20 +00:00
|
|
|
|
|
|
|
# Ensure log directory exists
|
|
|
|
mkdir -p "$LOG_DIR"
|
|
|
|
|
2025-01-06 16:30:34 +00:00
|
|
|
# Set Restic and MySQL environment variables
|
2024-09-18 16:53:20 +00:00
|
|
|
export RESTIC_REPOSITORY="$backupPath"
|
2025-01-06 17:22:25 +00:00
|
|
|
export RESTIC_PASSWORD
|
|
|
|
export MYSQL_PWD="$DB_PASSWORD"
|
2024-09-18 16:53:20 +00:00
|
|
|
|
|
|
|
# Logging start
|
2025-01-06 17:22:25 +00:00
|
|
|
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Starting Database Backup for $DB_NAME with tags: wordpress_db, $CUSTOM_TAG" | tee -a "$LOG_FILE"
|
2024-09-18 16:53:20 +00:00
|
|
|
|
2025-01-06 17:22:25 +00:00
|
|
|
# Verify that the password file exists
|
2024-11-12 18:15:11 +00:00
|
|
|
if [ ! -f "$password_file" ]; then
|
|
|
|
echo "ERROR: Password file not found at $password_file" | tee -a "$LOG_FILE"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2025-01-06 16:30:34 +00:00
|
|
|
# Verify backup path exists
|
|
|
|
if [ ! -d "$backupPath" ]; then
|
|
|
|
echo "ERROR: Backup path $backupPath does not exist." | tee -a "$LOG_FILE"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2025-01-06 17:22:25 +00:00
|
|
|
# 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"
|
2024-11-12 18:15:11 +00:00
|
|
|
|
2025-01-06 17:22:25 +00:00
|
|
|
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"
|
2024-09-18 16:53:20 +00:00
|
|
|
else
|
|
|
|
echo "[$(date +'%Y-%m-%d %H:%M:%S')] ERROR: Database backup failed." | tee -a "$LOG_FILE"
|
2024-11-12 18:15:11 +00:00
|
|
|
exit 1
|
2024-09-18 16:53:20 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Logging end
|
|
|
|
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Database Backup process finished." | tee -a "$LOG_FILE"
|