#!/bin/bash set -e BACKUP_TARGET="${BACKUP_TARGET:-/backups}" BACKUP_PASSWORD="${BACKUP_PASSWORD:-}" # Idempotent restic installation install_restic() { if command -v restic &>/dev/null; then echo "restic already installed: $(restic version)" return 0 fi echo "Installing restic..." # Install from official repository apt-get update apt-get install -y restic # Update to latest version restic self-update 2>/dev/null || true echo "restic installed: $(restic version)" } # Install cron for scheduled backups install_cron() { if command -v cron &>/dev/null; then echo "cron already installed" return 0 fi echo "Installing cron..." apt-get update apt-get install -y cron echo "cron installed" } # Setup directories setup_dirs() { # Create local backup directory if using local target if [[ "$BACKUP_TARGET" == /* ]]; then mkdir -p "$BACKUP_TARGET" echo "Local backup directory: $BACKUP_TARGET" fi # Create status directory mkdir -p /run/vibestack } # Initialize restic repository init_repository() { if [ -z "$BACKUP_PASSWORD" ]; then echo "WARNING: BACKUP_PASSWORD not set - skipping repository init" echo "Set BACKUP_PASSWORD to enable backups" return 0 fi export RESTIC_PASSWORD="$BACKUP_PASSWORD" export RESTIC_REPOSITORY="$BACKUP_TARGET" # Setup cloud credentials if provided if [ -n "$BACKUP_S3_ACCESS_KEY" ]; then export AWS_ACCESS_KEY_ID="$BACKUP_S3_ACCESS_KEY" export AWS_SECRET_ACCESS_KEY="$BACKUP_S3_SECRET_KEY" fi if [ -n "$BACKUP_B2_ACCOUNT_ID" ]; then export B2_ACCOUNT_ID="$BACKUP_B2_ACCOUNT_ID" export B2_ACCOUNT_KEY="$BACKUP_B2_ACCOUNT_KEY" fi # Check if repository exists if restic cat config &>/dev/null; then echo "Backup repository already initialized" return 0 fi echo "Initializing backup repository at $BACKUP_TARGET..." restic init echo "Backup repository initialized" } install_restic install_cron setup_dirs init_repository echo "Backup setup complete"