92 lines
2.7 KiB
Bash
92 lines
2.7 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 from standard location
|
|
CONFIG_DIR="$HOME/.config/citadel"
|
|
CONFIG_FILE="$CONFIG_DIR/backup.env"
|
|
|
|
if [ -f "$CONFIG_FILE" ]; then
|
|
source "$CONFIG_FILE"
|
|
else
|
|
echo "ERROR: Configuration not found at $CONFIG_FILE" >&2
|
|
echo "Please run './install setup' to initialize configuration" >&2
|
|
exit 1
|
|
fi
|
|
|
|
# Variables universelles pour tout service (auto-dérivées)
|
|
# Use service name if set explicitly, otherwise detect from calling script
|
|
if [ -z "$SERVICE_NAME" ]; then
|
|
# BASH_SOURCE[1] is the script that sourced this template
|
|
SERVICE_NAME="$(basename "$(dirname "${BASH_SOURCE[1]}")")"
|
|
fi
|
|
SERVICE_DIR="$SERVICES_BASE_DIR/$SERVICE_NAME"
|
|
RESTIC_CONFIG_FILE="$CONFIG_DIR/restic.conf"
|
|
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 RESTIC_CONFIG_FILE
|
|
export LOG_FILE
|
|
export DATA_DIR
|
|
export TEMP_BACKUP_DIR |