#!/bin/bash # Generic script to install systemd timer for any service backup # This script should only be called via ./manage install 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 " exit 1 fi # Configuration should be sourced before running: source backup.env # Check arguments if [ $# -lt 1 ]; then echo "Usage: $0 [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-backup@.service" TIMER_TEMPLATE="service-backup@.timer" SYSTEMD_DIR="/etc/systemd/system" # 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-backup@.service" ]; then echo "ERROR: Service template not found: $BACKUP_DIR/service-backup@.service" exit 1 fi if [ ! -f "$BACKUP_DIR/service-backup@.timer" ]; then echo "ERROR: Timer template not found: $BACKUP_DIR/service-backup@.timer" 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-backup@.service" > "/etc/systemd/system/service-backup@.service" chmod 644 "/etc/systemd/system/service-backup@.service" echo "Generating timer file with current configuration..." sed -e "s|\${DEFAULT_BACKUP_SCHEDULE}|$DEFAULT_BACKUP_SCHEDULE|g" \ "$BACKUP_DIR/service-backup@.timer" > "/etc/systemd/system/service-backup@.timer" chmod 644 "/etc/systemd/system/service-backup@.timer" # 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/service-backup@.timer" > "/etc/systemd/system/$TIMER_INSTANCE" chmod 644 "/etc/systemd/system/$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