#!/bin/bash # Citadel Installation Script # Consolidates configuration generation and repository initialization set -e # Get the directory where this script is located (project root) SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # Auto-source configuration if [ -f "$SCRIPT_DIR/backup.env" ]; then source "$SCRIPT_DIR/backup.env" else echo "ERROR: Configuration file backup.env not found in $SCRIPT_DIR" echo "Please ensure backup.env exists in project root directory" exit 1 fi show_help() { echo "Usage: $0 [command]" echo "" echo "Commands:" echo " config Generate Restic configuration with secure password" echo " repo Initialize Restic repository" echo " setup Complete setup (config + repo)" echo " help Show this help message" echo "" echo "Examples:" echo " $0 setup # Full installation (recommended)" echo " $0 config # Generate configuration only" echo " $0 repo # Initialize repository only" } generate_config() { local CONFIG_FILE="$RESTIC_CONFIG_FILE" local REPO_PATH="$BACKUP_REPOSITORY" echo "=== Generating Restic Configuration ===" # Check if config already exists if [ -f "$CONFIG_FILE" ]; then echo "Configuration file already exists at $CONFIG_FILE" read -p "Do you want to overwrite it? (y/N): " -n 1 -r echo if [[ ! $REPLY =~ ^[Yy]$ ]]; then echo "Aborted." return 1 fi fi # Ensure config directory exists mkdir -p "$(dirname "$CONFIG_FILE")" # Generate secure password (32 characters) echo "Generating secure password..." local RESTIC_PASSWORD=$(openssl rand -base64 32) # Create configuration file echo "Creating configuration file..." cat > "$CONFIG_FILE" << EOF # Restic configuration for citadel backups # Generated on $(date) # Repository path export RESTIC_REPOSITORY="$REPO_PATH" # Repository password export RESTIC_PASSWORD="$RESTIC_PASSWORD" # Cache directory (optional) export RESTIC_CACHE_DIR="$RESTIC_CACHE_DIR" EOF # Set secure permissions chmod 600 "$CONFIG_FILE" echo "✅ Configuration file created at $CONFIG_FILE" echo "🔒 Password generated and saved securely" echo "" echo "⚠️ IMPORTANT: Save this password somewhere safe!" echo " If you lose it, you won't be able to restore your backups!" echo "" echo "📁 Password saved in: $CONFIG_FILE" echo " Use 'cat $CONFIG_FILE' to view the password if needed" echo "" return 0 } initialize_repository() { local REPO_PATH="$BACKUP_REPOSITORY" local CONFIG_FILE="$RESTIC_CONFIG_FILE" echo "=== Initializing Restic Repository ===" # Check if repository directory exists if [ ! -d "$(dirname "$REPO_PATH")" ]; then echo "Creating backup directory..." sudo mkdir -p "$(dirname "$REPO_PATH")" sudo chown $(whoami):$(whoami) "$(dirname "$REPO_PATH")" fi # Check if config file exists if [ ! -f "$CONFIG_FILE" ]; then echo "Error: Configuration file $CONFIG_FILE not found!" echo "Please run '$0 config' first to create the configuration." return 1 fi # Source the configuration source "$CONFIG_FILE" # Check if repository is already initialized if restic -r "$REPO_PATH" cat config &>/dev/null; then echo "Repository already initialized at $REPO_PATH" return 0 fi echo "Initializing new Restic repository at $REPO_PATH" # Initialize the repository restic init -r "$REPO_PATH" if [ $? -eq 0 ]; then echo "✅ Repository successfully initialized!" echo "Repository path: $REPO_PATH" echo "Configuration: $CONFIG_FILE" echo "" echo "=== Repository Information ===" restic -r "$REPO_PATH" stats return 0 else echo "❌ Failed to initialize repository" return 1 fi } complete_setup() { echo "=== Citadel Complete Setup ===" echo "" # Step 1: Generate configuration if ! generate_config; then echo "❌ Configuration generation failed" return 1 fi echo "" # Step 2: Initialize repository if ! initialize_repository; then echo "❌ Repository initialization failed" return 1 fi echo "" echo "🎉 Citadel setup completed successfully!" echo "" echo "Next steps:" echo "1. Install a service timer: ./manage install " echo "2. Check available services: ./manage available" echo "3. View backup status: ./manage list" } # Main script logic case "${1:-setup}" in config) generate_config ;; repo) initialize_repository ;; setup) complete_setup ;; help|--help|-h) show_help ;; *) echo "ERROR: Unknown command '${1:-}'" echo "" show_help exit 1 ;; esac