#!/bin/bash # Global Configuration SCRIPT_PATH="/home/litespeed/mb-backups/backup_all.sh" LOG_FILE="/home/litespeed/mb-backups/logs/cron_check.log" CRON_SCHEDULE="0 0 * * *" # Slack Webhook URL (optional) SLACK_WEBHOOK_URL="https://hooks.slack.com/services/your/webhook/url" # Email Notification (optional) EMAIL="your_email@example.com" # Helper functions log_message() { local level="$1" local message="$2" echo "[$(date)] [$level] $message" | tee -a "$LOG_FILE" } send_notification() { local message="$1" if [ -n "$SLACK_WEBHOOK_URL" ]; then curl -X POST -H 'Content-type: application/json' \ --data "{\"text\":\"$message\"}" "$SLACK_WEBHOOK_URL" log_message "INFO" "Slack notification sent." fi if [ -n "$EMAIL" ]; then echo "$message" | mail -s "Backup Script Notification" "$EMAIL" log_message "INFO" "Email notification sent." fi } check_cron_service() { if ! systemctl is-active --quiet crond; then log_message "ERROR" "Cron service is not running. Attempting to restart..." sudo systemctl start crond && log_message "INFO" "Cron service restarted successfully." || { log_message "ERROR" "Failed to restart cron service." exit 1 } fi } install_cronnext() { log_message "INFO" "Checking for 'cronnext' tool..." if ! command -v cronnext &> /dev/null; then log_message "WARNING" "'cronnext' tool not found. Attempting to install..." if command -v pip &> /dev/null; then pip install cronnext && log_message "INFO" "'cronnext' installed successfully." || { log_message "ERROR" "Failed to install 'cronnext'. Please install it manually." send_notification "Cronnext installation failed. Please check the server configuration." exit 1 } else log_message "ERROR" "'pip' is not installed. Unable to install 'cronnext'." send_notification "Pip is missing; unable to install Cronnext. Please investigate." exit 1 fi else log_message "INFO" "'cronnext' is already installed." fi } check_and_repair_cron() { log_message "INFO" "Checking for existing cron job for $SCRIPT_PATH..." local existing_job existing_job=$(crontab -l 2>/dev/null | grep -F "$SCRIPT_PATH") if [ -z "$existing_job" ]; then log_message "WARNING" "Automated backups are NOT enabled. Attempting to re-add cron job..." (crontab -l 2>/dev/null; echo "$CRON_SCHEDULE SHELL=/bin/bash PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin RESTIC_PASSWORD=\"YourResticPassword\" $SCRIPT_PATH > /home/litespeed/mb-backups/logs/auto/backup.log 2>&1") | crontab - && \ log_message "INFO" "Cron job re-added successfully." || \ log_message "ERROR" "Failed to add cron job." else log_message "INFO" "Automated backups are already enabled with the following schedule:" echo "$existing_job" | awk '{print "Schedule: " $1, $2, $3, $4, $5}' | tee -a "$LOG_FILE" fi } calculate_next_run_fallback() { local cron_schedule="$1" case "$cron_schedule" in "0 * * * *") # Hourly date -d "+1 hour" "+%Y-%m-%d %H:00:00" ;; "0 0 * * *") # Daily at midnight date -d "tomorrow 00:00" "+%Y-%m-%d %H:%M:%S" ;; "0 0 * * 0") # Weekly at midnight on Sunday date -d "next sunday 00:00" "+%Y-%m-%d %H:%M:%S" ;; "*/15 * * * *") # Every 15 minutes date -d "15 minutes" "+%Y-%m-%d %H:%M:%S" ;; *) # Unsupported schedules log_message "ERROR" "Unsupported schedule for fallback: $cron_schedule" echo "Unsupported schedule" ;; esac } display_next_run() { log_message "INFO" "Calculating next run time for the cron job..." install_cronnext local next_run next_run=$(cronnext "$CRON_SCHEDULE" 2>/dev/null) if [ -n "$next_run" ]; then log_message "INFO" "Next scheduled run time: $next_run" else log_message "WARNING" "Failed to calculate next run time using 'cronnext'. Falling back to manual calculation..." next_run=$(calculate_next_run_fallback "$CRON_SCHEDULE") if [ "$next_run" == "Unsupported schedule" ]; then send_notification "Failed to calculate next run time: unsupported schedule $CRON_SCHEDULE." else log_message "INFO" "Next scheduled run time (fallback): $next_run" fi fi } # Main Execution log_message "INFO" "Starting backup schedule verification script..." check_cron_service check_and_repair_cron display_next_run log_message "INFO" "Backup schedule verification completed successfully."