#!/bin/bash # Restore script for JMP Server backups # Usage: ./restore.sh [backup-file.tar.gz] set -e set -u SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" cd "$SCRIPT_DIR" BACKUP_DIR="$SCRIPT_DIR/backups" RESTORE_DIR="$SCRIPT_DIR/restore-temp" # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color echo -e "${YELLOW}=== JMP Server Restore ===${NC}" echo "" # List available backups if no argument provided if [ -z "${1:-}" ]; then echo "Available backups:" echo "" ls -lht "$BACKUP_DIR"/*.tar.gz 2>/dev/null | head -10 || echo "No backups found in $BACKUP_DIR" echo "" echo -e "${YELLOW}Usage:${NC} $0 " echo -e "${YELLOW}Example:${NC} $0 backup-2025-01-05T03-00-00.tar.gz" exit 1 fi BACKUP_FILE="$1" # Check if backup file exists (support both full path and filename only) if [ -f "$BACKUP_FILE" ]; then BACKUP_PATH="$BACKUP_FILE" elif [ -f "$BACKUP_DIR/$BACKUP_FILE" ]; then BACKUP_PATH="$BACKUP_DIR/$BACKUP_FILE" else echo -e "${RED}Error: Backup file not found: $BACKUP_FILE${NC}" exit 1 fi # Validate backup file integrity echo "Validating backup file integrity..." if ! tar -tzf "$BACKUP_PATH" >/dev/null 2>&1; then echo -e "${RED}Error: Backup file is corrupted or not a valid tar.gz archive${NC}" exit 1 fi echo "✓ Backup file is valid" echo "" echo -e "${YELLOW}WARNING: This will restore data from backup and overwrite current data!${NC}" echo "Backup file: $BACKUP_PATH" echo "Backup size: $(du -h "$BACKUP_PATH" | cut -f1)" echo "" read -p "Are you sure you want to continue? (yes/no): " CONFIRM if [ "$CONFIRM" != "yes" ]; then echo "Restore cancelled." exit 0 fi echo "" echo "Step 1: Stopping all services..." docker compose down echo "" echo "Step 2: Creating restore directory..." rm -rf "$RESTORE_DIR" mkdir -p "$RESTORE_DIR" echo "" echo "Step 3: Extracting backup..." tar -xzf "$BACKUP_PATH" -C "$RESTORE_DIR" echo "" echo "Step 4: Restoring data..." # Restore each backup source restore_dir() { local src="$1" local dest="$2" if [ -d "$RESTORE_DIR/$src" ]; then echo " Restoring $src -> $dest" rm -rf "$dest" cp -a "$RESTORE_DIR/$src" "$dest" else echo " Skipping $src (not in backup)" fi } restore_dir "gitea-data" "./gitea/data" restore_dir "gitea-db" "./gitea/db" restore_dir "bookstack-app" "./bookstack/app" restore_dir "bookstack-db" "./bookstack/db" restore_dir "vikunja-files" "./vikunja/files" restore_dir "vikunja-db" "./vikunja/db" restore_dir "radicale-data" "./radicale/data" restore_dir "homepage" "./homepage" echo "" echo "Step 5: Cleaning up..." rm -rf "$RESTORE_DIR" echo "" echo "Step 6: Starting services..." docker compose up -d echo "" echo -e "${GREEN}Restore completed successfully!${NC}" echo "Services are starting up. Check status with: docker compose ps"