#!/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_" CRON_FILE="/var/spool/cron/crontabs/$(whoami)" CRON_PATH="/usr/bin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" # Ensure the log directory exists mkdir -p "$LOG_DIR" # Logging function log_action() { echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1" | tee -a "$ACTION_LOG_FILE" } # Function: Validate dependencies validate_dependencies() { for cmd in dnf crontab systemctl restic; do if ! command -v "$cmd" &>/dev/null; then log_action "ERROR: Required command '$cmd' not found." exit 1 fi done } # Function: Install and start cron service if needed check_and_install_cron() { if ! command -v crontab &>/dev/null; then log_action "Cron is not installed. Installing..." sudo dnf install -y cronie || { log_action "Failed to install cron."; exit 1; } fi if ! systemctl is-active --quiet crond; then log_action "Starting cron service..." sudo systemctl start crond sudo systemctl enable crond || { log_action "Failed to enable cron service."; exit 1; } fi log_action "Cron service is running and ready." } # Function: Validate cron schedule format 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 log_action "ERROR: Invalid cron schedule format: $schedule" exit 1 fi } # Function: Add or update a cron job add_update_cron_job() { local action="$1" local schedule="$2" local restic_password="$3" validate_cron_syntax "$schedule" if [ -z "$restic_password" ]; then log_action "ERROR: Restic password is required." exit 1 fi # Prepare the cron job command local cmd="SHELL=/bin/bash PATH=$CRON_PATH RESTIC_PASSWORD=\"$restic_password\"" cmd+=" $BACKUP_SCRIPT auto" cmd+=" > \"${BACKUP_LOG_PREFIX}\$(date +\\%Y-\\%m-\\%d_\\%H-\\%M-\\%S).log\" 2>&1" # Remove existing job and add a new one (crontab -l 2>/dev/null | grep -v "$BACKUP_SCRIPT"; echo "$schedule $cmd") | crontab - if crontab -l | grep -q "$BACKUP_SCRIPT auto"; then log_action "Cron job $action successfully: $schedule" else log_action "ERROR: Failed to $action cron job. Please check logs." exit 1 fi } # Function: Remove cron jobs remove_cron_jobs() { crontab -l 2>/dev/null | grep -v "$BACKUP_SCRIPT auto" | crontab - log_action "All backup schedules removed." } # Function: List cron jobs list_cron_jobs() { log_action "Listing all current cron jobs:" crontab -l 2>/dev/null | tee -a "$ACTION_LOG_FILE" } # Main execution main() { validate_dependencies 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) remove_cron_jobs ;; list) list_cron_jobs ;; *) echo "Usage: $0 {add|update|remove|list} [schedule] [restic_password]" log_action "Invalid action attempted: $1" exit 1 ;; esac } # Execute main function main "$@"