- Move backup/manage to root with auto-sourcing configuration - Create consolidated ./install script (replaces gen-conf.sh + init-restic.sh) - Add protection against direct execution of backup/ scripts - Update documentation (README.md, CLAUDE.md) for new architecture - Remove obsolete gen-conf.sh and init-restic.sh 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
146 lines
4.9 KiB
Bash
Executable File
146 lines
4.9 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Generic script to install systemd timer for any service backup
|
|
# This script should only be called via ./manage install <service>
|
|
|
|
set -e
|
|
|
|
# Check if called via manage script
|
|
if [ "${CALLED_FROM_MANAGE:-}" != "true" ]; then
|
|
echo "ERROR: This script should not be called directly."
|
|
echo "Use: ./manage install <service_name>"
|
|
exit 1
|
|
fi
|
|
|
|
# Configuration should be sourced before running: source backup.env
|
|
|
|
# Check arguments
|
|
if [ $# -lt 1 ]; then
|
|
echo "Usage: $0 <service_name> [schedule]"
|
|
echo ""
|
|
echo "Examples:"
|
|
echo " $0 paperless # Daily at 3:00 AM (default)"
|
|
echo " $0 nextcloud \"*-*-* 02:30:00\" # Daily at 2:30 AM"
|
|
echo " $0 gitlab \"Mon *-*-* 04:00:00\" # Weekly on Monday at 4:00 AM"
|
|
echo ""
|
|
echo "Available services with backup.sh:"
|
|
find "$SERVICES_BASE_DIR" -name "backup.sh" -printf " %h\n" | sed "s|$SERVICES_BASE_DIR/||" 2>/dev/null || echo " (none found)"
|
|
exit 1
|
|
fi
|
|
|
|
SERVICE_NAME="$1"
|
|
SCHEDULE="${2:-$DEFAULT_BACKUP_SCHEDULE}" # Use default from config
|
|
|
|
SERVICE_DIR="$SERVICES_BASE_DIR/$SERVICE_NAME"
|
|
BACKUP_SCRIPT="$SERVICE_DIR/backup.sh"
|
|
BACKUP_DIR="$BACKUP_BASE_DIR"
|
|
|
|
# Systemd template files
|
|
SERVICE_TEMPLATE="$SERVICE_TEMPLATE"
|
|
TIMER_TEMPLATE="$TIMER_TEMPLATE"
|
|
SYSTEMD_DIR="$SYSTEMD_DIR"
|
|
|
|
# Instance names
|
|
SERVICE_INSTANCE="service-backup@$SERVICE_NAME.service"
|
|
TIMER_INSTANCE="service-backup@$SERVICE_NAME.timer"
|
|
|
|
echo "=== Installing Systemd Timer for $SERVICE_NAME Backup ==="
|
|
|
|
# Check if running as root
|
|
if [ "$EUID" -ne 0 ]; then
|
|
echo "This script needs to be run with sudo for systemd operations"
|
|
echo "Usage: sudo $0 $SERVICE_NAME"
|
|
exit 1
|
|
fi
|
|
|
|
# Check if service directory exists
|
|
if [ ! -d "$SERVICE_DIR" ]; then
|
|
echo "ERROR: Service directory not found: $SERVICE_DIR"
|
|
exit 1
|
|
fi
|
|
|
|
# Check if backup script exists
|
|
if [ ! -f "$BACKUP_SCRIPT" ]; then
|
|
echo "ERROR: Backup script not found: $BACKUP_SCRIPT"
|
|
echo "Please create a backup.sh script in the service directory"
|
|
exit 1
|
|
fi
|
|
|
|
# Make backup script executable
|
|
chmod +x "$BACKUP_SCRIPT"
|
|
|
|
# Check if template files exist
|
|
if [ ! -f "$BACKUP_DIR/$SERVICE_TEMPLATE" ]; then
|
|
echo "ERROR: Service template not found: $BACKUP_DIR/$SERVICE_TEMPLATE"
|
|
exit 1
|
|
fi
|
|
|
|
if [ ! -f "$BACKUP_DIR/$TIMER_TEMPLATE" ]; then
|
|
echo "ERROR: Timer template not found: $BACKUP_DIR/$TIMER_TEMPLATE"
|
|
exit 1
|
|
fi
|
|
|
|
# Stop and disable existing timer if it exists
|
|
if systemctl is-active --quiet "$TIMER_INSTANCE" 2>/dev/null; then
|
|
echo "Stopping existing timer..."
|
|
systemctl stop "$TIMER_INSTANCE"
|
|
fi
|
|
|
|
if systemctl is-enabled --quiet "$TIMER_INSTANCE" 2>/dev/null; then
|
|
echo "Disabling existing timer..."
|
|
systemctl disable "$TIMER_INSTANCE"
|
|
fi
|
|
|
|
# Generate service and timer files with substituted variables
|
|
echo "Generating service file with current configuration..."
|
|
sed -e "s|\${BACKUP_USER}|$BACKUP_USER|g" \
|
|
-e "s|\${SERVICES_BASE_DIR}|$SERVICES_BASE_DIR|g" \
|
|
"$BACKUP_DIR/$SERVICE_TEMPLATE" > "$SYSTEMD_DIR/$SERVICE_TEMPLATE"
|
|
chmod 644 "$SYSTEMD_DIR/$SERVICE_TEMPLATE"
|
|
|
|
echo "Generating timer file with current configuration..."
|
|
sed -e "s|\${DEFAULT_BACKUP_SCHEDULE}|$DEFAULT_BACKUP_SCHEDULE|g" \
|
|
"$BACKUP_DIR/$TIMER_TEMPLATE" > "$SYSTEMD_DIR/$TIMER_TEMPLATE"
|
|
chmod 644 "$SYSTEMD_DIR/$TIMER_TEMPLATE"
|
|
|
|
# Create custom timer with specific schedule if different from default
|
|
if [ "$SCHEDULE" != "$DEFAULT_BACKUP_SCHEDULE" ]; then
|
|
echo "Creating custom timer with schedule: $SCHEDULE"
|
|
sed "s|OnCalendar=\*-\*-\* 03:00:00|OnCalendar=$SCHEDULE|" \
|
|
"$BACKUP_DIR/$TIMER_TEMPLATE" > "$SYSTEMD_DIR/$TIMER_INSTANCE"
|
|
chmod 644 "$SYSTEMD_DIR/$TIMER_INSTANCE"
|
|
fi
|
|
|
|
# Reload systemd
|
|
echo "Reloading systemd daemon..."
|
|
systemctl daemon-reload
|
|
|
|
# Enable and start timer
|
|
echo "Enabling and starting timer for $SERVICE_NAME..."
|
|
systemctl enable "service-backup@$SERVICE_NAME.timer"
|
|
systemctl start "service-backup@$SERVICE_NAME.timer"
|
|
|
|
# Create log file with proper permissions
|
|
LOG_FILE="$LOG_DIR/$SERVICE_NAME-backup.log"
|
|
touch "$LOG_FILE"
|
|
chown "$BACKUP_USER:$BACKUP_USER" "$LOG_FILE"
|
|
|
|
echo ""
|
|
echo "✅ Systemd timer installed successfully for $SERVICE_NAME!"
|
|
echo ""
|
|
echo "📋 Useful commands:"
|
|
echo " Check timer status: systemctl status service-backup@$SERVICE_NAME.timer"
|
|
echo " View timer schedule: systemctl list-timers service-backup@$SERVICE_NAME.timer"
|
|
echo " Run backup manually: systemctl start service-backup@$SERVICE_NAME.service"
|
|
echo " View backup logs: journalctl -u service-backup@$SERVICE_NAME.service"
|
|
echo " View recent logs: journalctl -u service-backup@$SERVICE_NAME.service -f"
|
|
echo " Stop timer: systemctl stop service-backup@$SERVICE_NAME.timer"
|
|
echo " Disable timer: systemctl disable service-backup@$SERVICE_NAME.timer"
|
|
echo ""
|
|
echo "📅 Schedule: $SCHEDULE (with up to 5min random delay)"
|
|
echo "📝 Logs: Available via journalctl and $LOG_FILE"
|
|
|
|
echo ""
|
|
echo "🔍 Current timer status:"
|
|
systemctl status "service-backup@$SERVICE_NAME.timer" --no-pager -l || true
|