2025-06-02 18:03:06 +00:00
|
|
|
#!/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"
|
|
|
|
|
|
2025-10-02 16:10:01 +00:00
|
|
|
# Create password file with persistence across reinstalls
|
2025-06-02 18:03:06 +00:00
|
|
|
echo "[INSTALL] Setting up password file..."
|
2025-10-02 16:10:01 +00:00
|
|
|
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"
|
2025-06-02 18:03:06 +00:00
|
|
|
fi
|
2025-10-02 16:10:01 +00:00
|
|
|
chmod 644 "$LOCAL_PASSWORD"
|
2025-06-02 18:03:06 +00:00
|
|
|
|
|
|
|
|
# Create directories
|
|
|
|
|
echo "[INSTALL] Creating directories..."
|
|
|
|
|
mkdir -p /home/litespeed/mb-backups/logs
|
|
|
|
|
chmod -R 755 /home/litespeed/mb-backups/logs
|
|
|
|
|
|
2025-10-02 16:10:01 +00:00
|
|
|
# Initialize repository (SAFE - preserves existing backups)
|
2025-06-02 18:03:06 +00:00
|
|
|
echo "[INSTALL] Initializing repository..."
|
|
|
|
|
export RESTIC_PASSWORD=$(cat /etc/restic-password)
|
2025-07-17 17:25:40 +00:00
|
|
|
export RESTIC_REPOSITORY=/data
|
2025-06-02 18:03:06 +00:00
|
|
|
|
2025-10-02 16:45:54 +00:00
|
|
|
# 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"
|
|
|
|
|
|
2025-10-02 16:10:01 +00:00
|
|
|
# Check if repository is accessible with current password
|
2025-10-02 16:45:54 +00:00
|
|
|
echo "[INSTALL] Checking for existing repository..."
|
2025-06-02 18:03:06 +00:00
|
|
|
if restic snapshots >/dev/null 2>&1; then
|
2025-10-02 16:45:54 +00:00
|
|
|
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)"
|
2025-06-02 18:03:06 +00:00
|
|
|
else
|
2025-10-02 16:45:54 +00:00
|
|
|
# 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
|
2025-10-02 16:10:01 +00:00
|
|
|
else
|
2025-10-02 16:45:54 +00:00
|
|
|
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
|
2025-10-02 16:10:01 +00:00
|
|
|
fi
|
2025-06-02 18:03:06 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
echo "[INSTALL] Installation completed successfully!"
|