citadel/backup/service-script-template.sh
Nicolas Duhamel f314fd06d3 Refactor: Rename project from QuantumRick to Citadel
- Update README.md title and project structure references
- Update script headers in install and manage scripts
- Update configuration comments in backup.env.sample
- Update Restic configuration comment in install script
- Maintain consistency across all project documentation

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-06-26 17:04:18 +02:00

102 lines
3.1 KiB
Bash

#!/bin/bash
# Generic Service Script Template for Citadel Backup System
# This template provides standardized configuration for all service scripts
# Usage: Source this template at the beginning of service backup/restore scripts
# Auto-configure via backup.env
# Calculate PROJECT_ROOT based on where template is sourced from
# Template is in backup/, so from services/service_name/ we need to go ../../
if [[ "${BASH_SOURCE[1]}" == */services/* ]]; then
# Sourced from a service script
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[1]}")/../.." && pwd)"
else
# Sourced from backup/ directory or elsewhere
SCRIPT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_ROOT/.." && pwd)"
fi
# Source centralized configuration
if [ -f "$PROJECT_ROOT/backup.env" ]; then
source "$PROJECT_ROOT/backup.env"
else
echo "ERROR: Configuration file backup.env not found in $PROJECT_ROOT" >&2
echo "Please ensure backup.env exists in project root directory" >&2
exit 1
fi
# Variables universelles pour tout service (auto-dérivées)
# Detect service name from the script that sources this template
if [[ "${BASH_SOURCE[1]}" == */services/* ]]; then
SERVICE_NAME="$(basename "$(dirname "${BASH_SOURCE[1]}")")"
else
SERVICE_NAME="$(basename "$(dirname "$0")")"
fi
SERVICE_DIR="$SERVICES_BASE_DIR/$SERVICE_NAME"
CONFIG_FILE="$RESTIC_CONFIG_FILE"
LOG_FILE="$LOG_DIR/$SERVICE_NAME-backup.log"
DATA_DIR="${DATA_DIR:-$(dirname "$SERVICES_BASE_DIR")/data/$SERVICE_NAME}"
TEMP_BACKUP_DIR="$TEMP_DIR/$SERVICE_NAME-backup"
# Logging functions
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1"
}
error() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] ERROR: $1" >&2
}
success() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] SUCCESS: $1"
}
# Function to apply retention policy
apply_retention() {
local service_tag="$1"
log "Applying retention policy for $service_tag"
restic forget --tag "$service_tag" \
--keep-daily "$RETENTION_DAILY" \
--keep-weekly "$RETENTION_WEEKLY" \
--keep-monthly "$RETENTION_MONTHLY" \
--keep-yearly "$RETENTION_YEARLY" \
--prune
if [ $? -eq 0 ]; then
success "Retention policy applied successfully"
else
error "Failed to apply retention policy"
return 1
fi
}
# Function to setup logging
setup_logging() {
# Redirect stdout and stderr to log file while keeping console output
exec 1> >(tee -a "$LOG_FILE")
exec 2> >(tee -a "$LOG_FILE" >&2)
}
# Function to cleanup on exit
cleanup_on_exit() {
log "Cleaning up temporary files..."
[ -d "$TEMP_BACKUP_DIR" ] && rm -rf "$TEMP_BACKUP_DIR"
# Ensure containers are running in case of error
if [ -f "$SERVICE_DIR/docker-compose.yml" ]; then
if docker compose -f "$SERVICE_DIR/docker-compose.yml" ps --services --filter "status=exited" | grep -q .; then
log "Some containers are stopped, restarting..."
cd "$SERVICE_DIR"
docker compose up -d
fi
fi
}
# Export commonly used variables
export SERVICE_NAME
export SERVICE_DIR
export CONFIG_FILE
export LOG_FILE
export DATA_DIR
export TEMP_BACKUP_DIR