mb-backup-manager/scripts/imports/manage_backup_schedule.sh

126 lines
3.6 KiB
Bash
Raw Permalink Normal View History

2024-09-18 16:53:20 +00:00
#!/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"
2024-11-12 18:15:11 +00:00
2024-11-12 19:18:30 +00:00
# Ensure the log directory exists
mkdir -p "$LOG_DIR"
2024-09-18 16:53:20 +00:00
# Logging function
2024-09-18 16:53:20 +00:00
log_action() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1" | tee -a "$ACTION_LOG_FILE"
2024-09-18 16:53:20 +00:00
}
# 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
2025-01-06 15:13:20 +00:00
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"
2025-01-06 15:13:20 +00:00
exit 1
fi
}
# Function: Add or update a cron job
2024-11-12 19:18:30 +00:00
add_update_cron_job() {
2025-01-06 15:13:20 +00:00
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."
2025-01-06 15:13:20 +00:00
exit 1
fi
# Prepare the cron job command
local cmd="SHELL=/bin/bash PATH=$CRON_PATH RESTIC_PASSWORD=\"$restic_password\""
cmd+=" $BACKUP_SCRIPT auto"
2025-01-06 15:13:20 +00:00
cmd+=" > \"${BACKUP_LOG_PREFIX}\$(date +\\%Y-\\%m-\\%d_\\%H-\\%M-\\%S).log\" 2>&1"
# Remove existing job and add a new one
2025-01-06 15:13:20 +00:00
(crontab -l 2>/dev/null | grep -v "$BACKUP_SCRIPT"; echo "$schedule $cmd") | crontab -
2024-11-12 18:15:11 +00:00
if crontab -l | grep -q "$BACKUP_SCRIPT auto"; then
2025-01-06 15:13:20 +00:00
log_action "Cron job $action successfully: $schedule"
else
log_action "ERROR: Failed to $action cron job. Please check logs."
exit 1
fi
2024-11-12 18:15:11 +00:00
}
# 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"
2025-01-06 15:13:20 +00:00
exit 1
;;
esac
}
# Execute main function
main "$@"