diff --git a/scripts/imports/backup_media.sh b/scripts/imports/backup_media.sh index f7f8424..464598d 100644 --- a/scripts/imports/backup_media.sh +++ b/scripts/imports/backup_media.sh @@ -24,6 +24,13 @@ includePaths=("$APP_PATH/wp-content/uploads") # Ensure log directory exists mkdir -p "$LOG_DIR" +# Check and fix permissions on /mnt/backups +if [ ! -w "$backupPath/locks" ]; then + echo "[$(date +'%Y-%m-%d %H:%M:%S')] Fixing permissions on $backupPath/locks" | tee -a "$LOG_FILE" + sudo chown -R litespeed:litespeed "$backupPath/locks" + sudo chmod -R u+rw "$backupPath/locks" +fi + # Set Restic environment variables export RESTIC_REPOSITORY="$backupPath" export RESTIC_PASSWORD diff --git a/scripts/imports/restore_backup_direct.sh b/scripts/imports/restore_backup_direct.sh index 42a271e..2c2ea26 100644 --- a/scripts/imports/restore_backup_direct.sh +++ b/scripts/imports/restore_backup_direct.sh @@ -1,5 +1,8 @@ #!/bin/bash +# Exit immediately on errors +set -e + # Validate input parameters if [ "$#" -ne 2 ]; then echo "Usage: $0 " @@ -11,52 +14,107 @@ SNAPSHOT_ID=$1 RESTIC_PASSWORD=$2 RESTIC_REPOSITORY="/mnt/backups" LOG_DIR="/home/litespeed/mb-backups/logs/restore" -WP_CONFIG='/var/www/webroot/ROOT/wp-config.php' +WP_CONFIG="/var/www/webroot/ROOT/wp-config.php" # Set up the environment export RESTIC_PASSWORD -mkdir -p "$LOG_DIR" # Ensure the log directory exists +mkdir -p "$LOG_DIR" LOG_FILE="${LOG_DIR}/restore_$(date +'%Y-%m-%d_%H-%M-%S').log" -# Extracting 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) - # Logging function log() { - echo "$1" | tee -a "$LOG_FILE" + echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE" } -log "Starting restoration for snapshot $SNAPSHOT_ID..." +# Check for required dependencies +for cmd in restic jq mysql; do + if ! command -v $cmd >/dev/null 2>&1; then + log "Error: '$cmd' command not found. Please install $cmd." + exit 1 + fi +done -# Attempt to extract the path and tags for the specified snapshot ID +# Ensure there are no stale locks in the repository +log "Checking for stale locks in the repository..." +if restic -r "$RESTIC_REPOSITORY" list locks | grep -q "lock"; then + log "Stale lock detected. Unlocking repository..." + restic -r "$RESTIC_REPOSITORY" unlock + log "Repository unlocked successfully." +else + log "No stale locks found. Proceeding with restoration." +fi + +# Extract database credentials from wp-config.php +if [ ! -f "$WP_CONFIG" ]; then + log "Error: wp-config.php not found at $WP_CONFIG. Ensure WordPress is installed." + exit 1 +fi + +DB_NAME=$(awk -F"'" '/define\( *'"'"'DB_NAME'"'"'/{print $4}' "$WP_CONFIG") +DB_USER=$(awk -F"'" '/define\( *'"'"'DB_USER'"'"'/{print $4}' "$WP_CONFIG") +DB_PASSWORD=$(awk -F"'" '/define\( *'"'"'DB_PASSWORD'"'"'/{print $4}' "$WP_CONFIG") + +if [ -z "$DB_NAME" ] || [ -z "$DB_USER" ] || [ -z "$DB_PASSWORD" ]; then + log "Error: Could not extract database credentials from wp-config.php." + exit 1 +fi + +log "Starting restoration for snapshot ID: $SNAPSHOT_ID..." + +# Retrieve snapshot data SNAPSHOT_DATA=$(restic -r "$RESTIC_REPOSITORY" snapshots --json | jq -r ".[] | select(.short_id == \"$SNAPSHOT_ID\")") -SNAPSHOT_PATH=$(echo "$SNAPSHOT_DATA" | jq -r ".paths[]") -SNAPSHOT_TAGS=$(echo "$SNAPSHOT_DATA" | jq -r ".tags[]") +if [ -z "$SNAPSHOT_DATA" ]; then + log "Error: Snapshot ID $SNAPSHOT_ID not found in repository." + exit 1 +fi -echo "Snapshot data: Path=$SNAPSHOT_PATH, Tags=$SNAPSHOT_TAGS" | tee -a "$LOG_FILE" +SNAPSHOT_PATH=$(echo "$SNAPSHOT_DATA" | jq -r ".paths[] // empty") +SNAPSHOT_TAGS=$(echo "$SNAPSHOT_DATA" | jq -r ".tags[] // empty") +if [ -z "$SNAPSHOT_PATH" ]; then + log "Error: Snapshot $SNAPSHOT_ID does not contain valid paths." + exit 1 +fi + +log "Snapshot Data Retrieved: Path=$SNAPSHOT_PATH, Tags=$SNAPSHOT_TAGS" + +# Determine the restoration type based on the tags if [[ "$SNAPSHOT_TAGS" == *"wordpress_db"* ]] && [[ "$SNAPSHOT_PATH" == "/stdin" ]]; then - log "Detected database backup. Starting restoration process for database $DB_NAME..." + log "Detected database backup. Restoring database $DB_NAME..." if restic -r "$RESTIC_REPOSITORY" dump "$SNAPSHOT_ID" stdin | mysql -u "$DB_USER" -p"$DB_PASSWORD" "$DB_NAME"; then - log "Restoration of snapshot $SNAPSHOT_ID with TAG: wordpress_db completed successfully." + log "Database restoration completed successfully for $DB_NAME from Snapshot ID: $SNAPSHOT_ID." else - log "Failed to restore database from Snapshot ID: $SNAPSHOT_ID. Please check the log file for details: $LOG_FILE" + log "Error: Database restoration failed for Snapshot ID: $SNAPSHOT_ID." + exit 1 + fi +elif [[ "$SNAPSHOT_TAGS" == *"core_files"* ]]; then + RESTORE_DIR="/var/www/webroot/ROOT" + log "Detected core files backup. Restoring to $RESTORE_DIR..." + if restic -r "$RESTIC_REPOSITORY" restore "$SNAPSHOT_ID" --target "$RESTORE_DIR"; then + log "Core files restoration completed successfully for Snapshot ID: $SNAPSHOT_ID." + else + log "Error: Core files restoration failed for Snapshot ID: $SNAPSHOT_ID." + exit 1 + fi +elif [[ "$SNAPSHOT_TAGS" == *"media_themes"* ]]; then + RESTORE_DIR="/var/www/webroot/ROOT/wp-content/uploads" + log "Detected media files backup. Restoring to $RESTORE_DIR..." + if restic -r "$RESTIC_REPOSITORY" restore "$SNAPSHOT_ID" --target "$RESTORE_DIR"; then + log "Media files restoration completed successfully for Snapshot ID: $SNAPSHOT_ID." + else + log "Error: Media files restoration failed for Snapshot ID: $SNAPSHOT_ID." exit 1 fi else - if [ -z "$SNAPSHOT_PATH" ] || [ "$SNAPSHOT_PATH" = "null" ]; then - log "Failed to find a valid path for snapshot ID $SNAPSHOT_ID. Check the snapshot ID and try again." - exit 1 - fi - - log "Restoring snapshot $SNAPSHOT_ID to $SNAPSHOT_PATH..." - if restic -r "$RESTIC_REPOSITORY" restore "$SNAPSHOT_ID" --target "$SNAPSHOT_PATH"; then - log "Restoration of snapshot $SNAPSHOT_ID to $SNAPSHOT_PATH completed successfully." + log "Unknown snapshot type. Attempting general restoration to /..." + TEMP_RESTORE_DIR="/tmp/restoration_$SNAPSHOT_ID" + mkdir -p "$TEMP_RESTORE_DIR" + if restic -r "$RESTIC_REPOSITORY" restore "$SNAPSHOT_ID" --target "$TEMP_RESTORE_DIR"; then + log "General file restoration completed successfully to temporary directory: $TEMP_RESTORE_DIR." else - log "Failed to restore Snapshot ID: $SNAPSHOT_ID to PATH: $SNAPSHOT_PATH. Please check the log file for details: $LOG_FILE" + log "Error: File restoration failed for Snapshot ID: $SNAPSHOT_ID." exit 1 fi fi +log "Restoration process completed successfully." diff --git a/scripts/imports/view_snapshots.sh b/scripts/imports/view_snapshots.sh index 3e91bbe..04eabcd 100644 --- a/scripts/imports/view_snapshots.sh +++ b/scripts/imports/view_snapshots.sh @@ -1,5 +1,8 @@ #!/bin/bash +# Exit immediately on error +set -e + # Display usage if no tag is provided if [ "$#" -ne 1 ]; then echo "Usage: $0 " @@ -7,20 +10,31 @@ if [ "$#" -ne 1 ]; then exit 1 fi +# Assign input argument TAG=$1 backupPath='/mnt/backups' +password_file="/etc/restic-password" # Use a password file instead of embedding the password -# Embedded Restic repository password -RESTIC_PASSWORD="tonysite1" # Replace this with your actual password +# Validate if the password file exists +if [ ! -f "$password_file" ]; then + echo "Error: Password file not found at $password_file" + exit 1 +fi +# Set Restic environment variables export RESTIC_REPOSITORY="$backupPath" -export RESTIC_PASSWORD +export RESTIC_PASSWORD=$(cat "$password_file") # Validate the provided tag against known tags case "$TAG" in main_backup|wordpress_db|core_files|media_themes|full_backup) # Use the --json flag to output the snapshots in JSON format - restic -r $RESTIC_REPOSITORY snapshots --tag "$TAG" --json + if restic -r "$RESTIC_REPOSITORY" snapshots --tag "$TAG" --json; then + echo "Snapshots for tag '$TAG' displayed successfully." + else + echo "Error: Unable to display snapshots for tag '$TAG'." + exit 1 + fi ;; *) echo "Error: Unknown tag '$TAG'."