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

117 lines
3.5 KiB
Bash
Raw 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_"
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
log_action() {
2024-11-12 19:18:30 +00:00
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1" >> "$ACTION_LOG_FILE"
2024-09-18 16:53:20 +00:00
}
check_and_install_cron() {
if ! command -v crontab &> /dev/null; then
echo "Cron is not installed. Installing..."
2025-01-06 15:13:20 +00:00
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
}
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
echo "Invalid cron schedule format: $schedule"
log_action "Invalid cron schedule format: $schedule"
exit 1
fi
}
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
2024-11-12 19:18:30 +00:00
echo "Restic password is required."
2025-01-06 15:13:20 +00:00
log_action "Attempted to $action a schedule without providing a Restic password."
2024-11-12 19:18:30 +00:00
exit 1
2024-11-12 18:29:16 +00:00
fi
2024-09-18 16:53:20 +00:00
2025-01-06 15:13:20 +00:00
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"
2024-09-18 16:53:20 +00:00
2025-01-06 15:13:20 +00:00
# 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 -
2024-11-12 18:15:11 +00:00
2025-01-06 15:13:20 +00:00
if crontab -l | grep -q "$BACKUP_SCRIPT"; then
echo "Cron job $action successfully."
log_action "Cron job $action successfully: $schedule"
else
2025-01-06 15:13:20 +00:00
echo "Failed to $action cron job. Please check logs for details."
log_action "Failed to $action cron job: $schedule"
exit 1
fi
2024-11-12 18:15:11 +00:00
}
# Main execution
check_and_install_cron
2024-11-12 19:18:30 +00:00
case $1 in
add|update)
if [ "$#" -ne 3 ]; then
echo "Usage for add/update: $0 {add|update} 'schedule' 'restic_password'"
2025-01-06 15:13:20 +00:00
log_action "Incorrect usage for $1. Format not followed."
exit 1
2024-11-12 19:18:30 +00:00
else
2025-01-06 15:13:20 +00:00
add_update_cron_job "$1" "$2" "$3"
2024-11-12 19:18:30 +00:00
fi
;;
remove)
2025-01-06 15:13:20 +00:00
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."
2024-11-12 19:18:30 +00:00
;;
*)
echo "Invalid action: $1. Use add, update, or remove."
log_action "Invalid action attempted: $1"
exit 1
;;
esac