- Automated backup system using Restic - Systemd timer integration for scheduled backups - Service management tools (start/stop/restore) - Multi-service support with template-based configuration - Backup and restore functionality with test/production modes 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
139 lines
4.6 KiB
Bash
Executable File
139 lines
4.6 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Generic script to install systemd timer for any service backup
|
|
# Location: /home/citadel/backup/install-service-timer.sh
|
|
# Usage: sudo ./install-service.sh <service_name> [schedule]
|
|
|
|
set -e
|
|
|
|
# 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 /home/citadel/services -name "backup.sh" -printf " %h\n" | sed 's|/home/citadel/services/||' 2>/dev/null || echo " (none found)"
|
|
exit 1
|
|
fi
|
|
|
|
SERVICE_NAME="$1"
|
|
SCHEDULE="${2:-*-*-* 03:00:00}" # Default: daily at 3:00 AM
|
|
|
|
SERVICE_DIR="/home/citadel/services/$SERVICE_NAME"
|
|
BACKUP_SCRIPT="$SERVICE_DIR/backup.sh"
|
|
BACKUP_DIR="/home/citadel/backup"
|
|
|
|
# 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_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
|
|
|
|
# Copy template files if they don't exist in systemd directory
|
|
if [ ! -f "$SYSTEMD_DIR/$SERVICE_TEMPLATE" ]; then
|
|
echo "Installing service template..."
|
|
cp "$BACKUP_DIR/$SERVICE_TEMPLATE" "$SYSTEMD_DIR/"
|
|
chmod 644 "$SYSTEMD_DIR/$SERVICE_TEMPLATE"
|
|
fi
|
|
|
|
if [ ! -f "$SYSTEMD_DIR/$TIMER_TEMPLATE" ]; then
|
|
echo "Installing timer template..."
|
|
cp "$BACKUP_DIR/$TIMER_TEMPLATE" "$SYSTEMD_DIR/"
|
|
chmod 644 "$SYSTEMD_DIR/$TIMER_TEMPLATE"
|
|
fi
|
|
|
|
# Create custom timer with specific schedule if different from default
|
|
if [ "$SCHEDULE" != "*-*-* 03:00:00" ]; 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="/var/log/$SERVICE_NAME-backup.log"
|
|
touch "$LOG_FILE"
|
|
chown citadel:citadel "$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
|