#!/bin/bash # Simple Restic Installation Script # Following Cloud Scripting best practices set -e echo "[INSTALL] Starting Restic installation..." # Download and install Restic echo "[INSTALL] Downloading Restic..." curl -L https://github.com/restic/restic/releases/download/v0.15.2/restic_0.15.2_linux_amd64.bz2 -o restic.bz2 bzip2 -d restic.bz2 chmod +x restic mv restic /usr/local/bin/ echo "[INSTALL] Restic installed successfully" # Create password file with persistence across reinstalls echo "[INSTALL] Setting up password file..." SHARED_PASSWORD="/data/.restic-password" LOCAL_PASSWORD="/etc/restic-password" # Ensure /data directory exists mkdir -p /data # Strategy: Store password in shared storage (/data) to survive reinstalls # Priority: shared storage > local existing > generate new if [ -f "$SHARED_PASSWORD" ]; then echo "[INSTALL] Using existing password from shared storage" cp "$SHARED_PASSWORD" "$LOCAL_PASSWORD" echo "[INSTALL] Password restored from shared storage" elif [ -f "$LOCAL_PASSWORD" ]; then echo "[INSTALL] Backing up existing local password to shared storage" cp "$LOCAL_PASSWORD" "$SHARED_PASSWORD" chmod 600 "$SHARED_PASSWORD" echo "[INSTALL] Password backed up to shared storage" else echo "[INSTALL] Creating new password (first installation)" head /dev/urandom | tr -dc A-Za-z0-9 | head -c 16 > "$LOCAL_PASSWORD" cp "$LOCAL_PASSWORD" "$SHARED_PASSWORD" chmod 600 "$SHARED_PASSWORD" echo "[INSTALL] New password created and stored in shared storage" fi chmod 644 "$LOCAL_PASSWORD" # Create directories echo "[INSTALL] Creating directories..." mkdir -p /home/litespeed/mb-backups/logs chmod -R 755 /home/litespeed/mb-backups/logs # Initialize repository (SAFE - preserves existing backups) echo "[INSTALL] Initializing repository..." export RESTIC_PASSWORD=$(cat /etc/restic-password) export RESTIC_REPOSITORY=/data # Verify /data exists and check if it's mounted storage echo "[INSTALL] Checking /data directory..." if [ ! -d "/data" ]; then echo "[INSTALL] Creating /data directory..." mkdir -p /data fi # Check if /data is on a mount point (shared storage) MOUNT_INFO=$(df /data | tail -1) echo "[INSTALL] Storage info: $MOUNT_INFO" # Verify /data has proper permissions echo "[INSTALL] Checking /data permissions..." if [ ! -w "/data" ]; then echo "[INSTALL] /data is not writable, fixing permissions..." chown -R root:root /data chmod 755 /data fi # Test write access echo "[INSTALL] Testing write access to /data..." if ! touch /data/.write_test 2>/dev/null; then echo "[INSTALL] ERROR: Cannot write to /data!" echo "[INSTALL] Please ensure /data is mounted to Shared Storage" exit 1 fi rm -f /data/.write_test echo "[INSTALL] ✓ /data is writable" # Check if repository is accessible with current password echo "[INSTALL] Checking for existing repository..." if restic snapshots >/dev/null 2>&1; then echo "[INSTALL] ✓ Repository already exists and is accessible" SNAPSHOT_COUNT=$(restic snapshots --json 2>/dev/null | jq '. | length' 2>/dev/null || echo "0") echo "[INSTALL] ✓ Found $SNAPSHOT_COUNT existing snapshot(s)" else # Try to initialize - show errors if it fails echo "[INSTALL] No existing repository found, initializing new repository..." INIT_OUTPUT=$(restic init 2>&1) INIT_RESULT=$? if [ $INIT_RESULT -eq 0 ]; then echo "[INSTALL] ✓ New repository initialized successfully" # Verify it works if restic snapshots >/dev/null 2>&1; then echo "[INSTALL] ✓ Repository verified and accessible" else echo "[INSTALL] ERROR: Repository created but not accessible!" exit 1 fi else echo "[INSTALL] ERROR: Repository initialization failed!" echo "[INSTALL] Error details: $INIT_OUTPUT" echo "[INSTALL] This usually means:" echo "[INSTALL] - /data is not persistent storage" echo "[INSTALL] - Permission issues" echo "[INSTALL] - Existing repository with different password" exit 1 fi fi echo "[INSTALL] Installation completed successfully!"