#!/bin/bash # Check if Restic password was supplied if [ "$#" -ne 1 ]; then echo "Usage: $0 RESTIC_PASSWORD" exit 1 fi # Configuration RESTIC_PASSWORD="$1" APP_PATH='/var/www/webroot/ROOT' WP_CONFIG="${APP_PATH}/wp-config.php" backupPath='/mnt/backups' LOG_DIR="/home/jelastic/mb-backups/logs" 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) BACKUP_USER='wp_backup' BACKUP_PASSWORD='gaQjveXl24Xo66w' export RESTIC_REPOSITORY="$backupPath" export RESTIC_PASSWORD # Ensure log directory exists mkdir -p "$LOG_DIR" # Backup functions backup_core_files() { local log_file="${LOG_DIR}/backup_core_files_$(date +'%Y-%m-%d').log" echo "[$(date +'%Y-%m-%d %H:%M:%S')] Starting Core Files Backup" | tee -a "$log_file" local excludePaths=("$APP_PATH/wp-content/uploads") local excludeOptions="" for path in "${excludePaths[@]}"; do excludeOptions+="--exclude $path " done if restic backup $excludeOptions "$APP_PATH" --tag core_files --tag full_backup; then echo "[$(date +'%Y-%m-%d %H:%M:%S')] Core files backup completed successfully." | tee -a "$log_file" else echo "[$(date +'%Y-%m-%d %H:%M:%S')] ERROR: Core files backup failed." | tee -a "$log_file" fi } backup_media_themes() { local log_file="${LOG_DIR}/backup_media_themes_$(date +'%Y-%m-%d').log" echo "[$(date +'%Y-%m-%d %H:%M:%S')] Starting Media and Themes Backup" | tee -a "$log_file" local includePaths=("$APP_PATH/wp-content/uploads") for path in "${includePaths[@]}"; do if restic backup "$path" --tag media_themes --tag full_backup; then echo "[$(date +'%Y-%m-%d %H:%M:%S')] Backup completed successfully for $path." | tee -a "$log_file" else echo "[$(date +'%Y-%m-%d %H:%M:%S')] ERROR: Backup failed for $path." | tee -a "$log_file" fi done } backup_database() { local log_file="${LOG_DIR}/backup_database_$(date +'%Y-%m-%d').log" echo "[$(date +'%Y-%m-%d %H:%M:%S')] Starting Database Backup" | tee -a "$log_file" # Correctly pass the password to mysqldump if mysqldump -u "$BACKUP_USER" --password="$BACKUP_PASSWORD" "$DB_NAME" | restic backup --stdin --tag wordpress_db --tag full_backup; then echo "[$(date +'%Y-%m-%d %H:%M:%S')] Database backup completed successfully." | tee -a "$log_file" else echo "[$(date +'%Y-%m-%d %H:%M:%S')] ERROR: Database backup failed." | tee -a "$log_file" fi } # Execute backup functions backup_core_files backup_media_themes backup_database