#!/bin/bash # Configuration BACKUP_SCRIPT="/home/litespeed/mb-backups/backup_all.sh" LOG_DIR="/home/litespeed/mb-backups/logs/auto" ACTION_LOG_FILE="${LOG_DIR}/schedule_actions.log" BACKUP_LOG_PREFIX="${LOG_DIR}/backup_" # Ensure the log directory exists mkdir -p "$LOG_DIR" log_action() { echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1" >> "$ACTION_LOG_FILE" } check_and_install_cron() { if ! command -v crontab &> /dev/null; then echo "Cron is not installed. Installing..." sudo dnf install -y cronie || { echo "Failed to install cron."; exit 1; } fi if ! systemctl is-active --quiet crond; then echo "Starting cron service..." sudo systemctl start crond sudo systemctl enable crond fi if systemctl is-active --quiet crond; then echo "Cron service is running." else echo "Failed to start cron service." log_action "Failed to start cron service." exit 1 fi } validate_cron_syntax() { local schedule="$1" if ! echo "$schedule" | grep -Eq '^(\*|[0-9]|[0-5][0-9]) (\*|[0-9]|1[0-9]|2[0-3]) (\*|[0-9]|3[0-1]) (\*|[1-9]|1[0-2]) (\*|[0-6])$'; then echo "Invalid cron schedule format: $schedule" log_action "Invalid cron schedule format: $schedule" exit 1 fi } add_update_cron_job() { local action="$1" local schedule="$2" local restic_password="$3" validate_cron_syntax "$schedule" if [ -z "$restic_password" ]; then echo "Restic password is required." log_action "Attempted to $action a schedule without providing a Restic password." exit 1 fi local restic_path="/usr/local/bin/restic" if [ ! -x "$restic_path" ]; then echo "Error: restic not found at $restic_path. Please verify installation." log_action "restic not found at $restic_path" exit 1 fi # Prepare the cron job command local cmd="SHELL=/bin/bash" cmd+=" PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" cmd+=" RESTIC_PASSWORD=\"$restic_password\"" cmd+=" $BACKUP_SCRIPT" cmd+=" > \"${BACKUP_LOG_PREFIX}\$(date +\\%Y-\\%m-\\%d_\\%H-\\%M-\\%S).log\" 2>&1" # Debug: Output the command to verify echo "Adding the following cron job:" echo "$schedule $cmd" # Remove any existing lines containing $BACKUP_SCRIPT, then add the new one (crontab -l 2>/dev/null | grep -v "$BACKUP_SCRIPT"; echo "$schedule $cmd") | crontab - if crontab -l | grep -q "$BACKUP_SCRIPT"; then echo "Cron job $action successfully." log_action "Cron job $action successfully: $schedule" else echo "Failed to $action cron job. Please check logs for details." log_action "Failed to $action cron job: $schedule" exit 1 fi } # Main execution check_and_install_cron case $1 in add|update) if [ "$#" -ne 3 ]; then echo "Usage for add/update: $0 {add|update} 'schedule' 'restic_password'" log_action "Incorrect usage for $1. Format not followed." exit 1 else add_update_cron_job "$1" "$2" "$3" fi ;; remove) tmp_cron=$(mktemp) crontab -l 2>/dev/null | grep -v "$BACKUP_SCRIPT" > "$tmp_cron" crontab "$tmp_cron" rm -f "$tmp_cron" echo "All backup schedules removed." log_action "All backup schedules removed." ;; *) echo "Invalid action: $1. Use add, update, or remove." log_action "Invalid action attempted: $1" exit 1 ;; esac