63 lines
2.4 KiB
Bash
63 lines
2.4 KiB
Bash
|
#!/bin/bash
|
||
|
|
||
|
# Validate input parameters
|
||
|
if [ "$#" -ne 2 ]; then
|
||
|
echo "Usage: $0 <snapshot_id> <restic_password>"
|
||
|
exit 1
|
||
|
fi
|
||
|
|
||
|
# Assign input to variables
|
||
|
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'
|
||
|
|
||
|
# Set up the environment
|
||
|
export RESTIC_PASSWORD
|
||
|
mkdir -p "$LOG_DIR" # Ensure the log directory exists
|
||
|
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"
|
||
|
}
|
||
|
|
||
|
log "Starting restoration for snapshot $SNAPSHOT_ID..."
|
||
|
|
||
|
# Attempt to extract the path and tags for the specified snapshot ID
|
||
|
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[]")
|
||
|
|
||
|
echo "Snapshot data: Path=$SNAPSHOT_PATH, Tags=$SNAPSHOT_TAGS" | tee -a "$LOG_FILE"
|
||
|
|
||
|
if [[ "$SNAPSHOT_TAGS" == *"wordpress_db"* ]] && [[ "$SNAPSHOT_PATH" == "/stdin" ]]; then
|
||
|
log "Detected database backup. Starting restoration process for 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."
|
||
|
else
|
||
|
log "Failed to restore database from Snapshot ID: $SNAPSHOT_ID. Please check the log file for details: $LOG_FILE"
|
||
|
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."
|
||
|
else
|
||
|
log "Failed to restore Snapshot ID: $SNAPSHOT_ID to PATH: $SNAPSHOT_PATH. Please check the log file for details: $LOG_FILE"
|
||
|
exit 1
|
||
|
fi
|
||
|
fi
|
||
|
|