#!/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 from standard location CONFIG_DIR="$HOME/.config/citadel" CONFIG_FILE="$CONFIG_DIR/backup.env" if [ -f "$CONFIG_FILE" ]; then source "$CONFIG_FILE" else echo "Configuration not found at $CONFIG_FILE" echo "Will create configuration during setup..." 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 RESTIC_CONFIG_FILE="$CONFIG_DIR/restic.conf" local BACKUP_ENV_FILE="$CONFIG_DIR/backup.env" echo "=== Generating Citadel Configuration ===" # Create config directory mkdir -p "$CONFIG_DIR" # Copy backup.env.sample if backup.env doesn't exist if [ ! -f "$BACKUP_ENV_FILE" ]; then if [ -f "$SCRIPT_DIR/backup.env.sample" ]; then echo "Creating configuration from sample..." cp "$SCRIPT_DIR/backup.env.sample" "$BACKUP_ENV_FILE" echo "✅ Configuration created at $BACKUP_ENV_FILE" echo "Please edit this file to match your environment" else echo "ERROR: backup.env.sample not found in $SCRIPT_DIR" return 1 fi fi # Source the configuration to get variables source "$BACKUP_ENV_FILE" local REPO_PATH="$BACKUP_REPOSITORY" # Check if restic config already exists if [ -f "$RESTIC_CONFIG_FILE" ]; then echo "Restic configuration already exists at $RESTIC_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 # Generate secure password (32 characters) echo "Generating secure password..." local RESTIC_PASSWORD=$(openssl rand -base64 32) # Create restic configuration file echo "Creating restic configuration file..." cat > "$RESTIC_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 "$RESTIC_CONFIG_FILE" echo "✅ Restic configuration file created at $RESTIC_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: $RESTIC_CONFIG_FILE" echo " Use 'cat $RESTIC_CONFIG_FILE' to view the password if needed" echo "" return 0 } initialize_repository() { local RESTIC_CONFIG_FILE="$CONFIG_DIR/restic.conf" # Source backup.env to get repository path if [ -f "$CONFIG_DIR/backup.env" ]; then source "$CONFIG_DIR/backup.env" else echo "Error: backup.env not found at $CONFIG_DIR/backup.env" echo "Please run '$0 config' first to create the configuration." return 1 fi local REPO_PATH="$BACKUP_REPOSITORY" 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 restic config file exists if [ ! -f "$RESTIC_CONFIG_FILE" ]; then echo "Error: Restic configuration file $RESTIC_CONFIG_FILE not found!" echo "Please run '$0 config' first to create the configuration." return 1 fi # Source the restic configuration source "$RESTIC_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: $RESTIC_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