Simplified Daily Backup Enable

main
Anthony 2024-11-13 02:58:17 +08:00
parent df5b7acf3e
commit f830e4de69
2 changed files with 29 additions and 186 deletions

View File

@ -23,46 +23,12 @@ onInstall:
- importScripts - importScripts
settings: settings:
scheduleSettings:
submitUnchanged: true
fields:
- name: frequency
caption: Backup Frequency
type: list
values:
daily: Daily Backup
weekly: Weekly Backup
default: daily
- name: hour
caption: Backup Hour (0-23)
type: string
regex: "^([0-9]|1[0-9]|2[0-3])$"
regexText: Enter a valid hour (0-23)
default: "0"
- name: dayOfWeek
caption: Day of Week (0=Sunday)
type: list
values:
0: Sunday
1: Monday
2: Tuesday
3: Wednesday
4: Thursday
5: Friday
6: Saturday
default: "0"
showIf:
frequency: weekly
backupSettings: backupSettings:
submitUnchanged: true submitUnchanged: true
fields: fields:
- name: blabel - name: blabel
caption: Backup Label caption: Backup Label
type: string type: string
restoreSettings: restoreSettings:
submitUnchanged: true submitUnchanged: true
fields: fields:
@ -83,7 +49,6 @@ menu:
confirmText: Configure automated backup schedule? confirmText: Configure automated backup schedule?
loadingText: Setting up backup schedule... loadingText: Setting up backup schedule...
successText: Backup schedule configured successfully successText: Backup schedule configured successfully
settings: scheduleSettings
title: Configure Automated Backup Schedule title: Configure Automated Backup Schedule
submitButtonText: Save Schedule submitButtonText: Save Schedule
@ -181,23 +146,21 @@ onAfterClone:
actions: actions:
configureAutoBackup: configureAutoBackup:
- cmd[cp]: - cmd[cp]:
user: root user: root
commands: | commands: |
if [ ! -f /etc/restic-password ]; then if [ ! -x "/home/litespeed/mb-backups/manage_backup_schedule.sh" ]; then
echo "Error: Restic password file not found" echo "Error: manage_backup_schedule.sh not found or not executable"
exit 1 exit 1
fi fi
RESTIC_PWD=$(cat /etc/restic-password) bash "/home/litespeed/mb-backups/manage_backup_schedule.sh"
if [ "${settings.frequency}" = "daily" ]; then if [ $? -ne 0 ]; then
cron_schedule="0 ${settings.hour} * * *" echo "Error executing manage_backup_schedule.sh"
else exit 1
cron_schedule="0 ${settings.hour} * * ${settings.dayOfWeek}" fi
fi - return:
bash ${globals.scriptPath}/manage_backup_schedule.sh add "$cron_schedule" "$RESTIC_PWD" type: info
- return: message: "Daily backup schedule configured successfully"
type: info
message: "${response.out}"
removeAutoBackup: removeAutoBackup:
- cmd[cp]: - cmd[cp]:

View File

@ -11,8 +11,7 @@ ACTION_LOG_FILE="${LOG_DIR}/schedule_actions.log"
BACKUP_LOG_PREFIX="${LOG_DIR}/backup_" BACKUP_LOG_PREFIX="${LOG_DIR}/backup_"
PASSWORD_FILE="/etc/restic-password" PASSWORD_FILE="/etc/restic-password"
LOCK_FILE="/tmp/backup_schedule.lock" LOCK_FILE="/tmp/backup_schedule.lock"
MAX_RETRIES=3 SCHEDULE="0 0 * * *" # Default to daily at midnight
RETRY_DELAY=5
# Error handling function # Error handling function
handle_error() { handle_error() {
@ -40,153 +39,34 @@ ensure_single_instance() {
echo $$ > "$LOCK_FILE" echo $$ > "$LOCK_FILE"
} }
# Enhanced logging function # Logging function
log_action() { log_action() {
local timestamp=$(date +'%Y-%m-%d %H:%M:%S') local timestamp=$(date +'%Y-%m-%d %H:%M:%S')
local log_msg="[$timestamp] $1" echo "[$timestamp] $1" | tee -a "$ACTION_LOG_FILE"
echo "$log_msg" | tee -a "$ACTION_LOG_FILE"
# Log critical errors to system log
if [[ "$1" == *"ERROR"* ]]; then
logger -t "backup-schedule" "$1"
fi
} }
# Validate system requirements # Function to configure the cron job
check_system_requirements() { configure_cron_job() {
local required_space=1048576 # 1GB in KB if [ ! -f "$PASSWORD_FILE" ]; then
local available_space=$(df -k "$LOG_DIR" | awk 'NR==2 {print $4}') log_action "ERROR: Restic password file not found"
if [ ! -x "$BACKUP_SCRIPT" ]; then
log_action "ERROR: Backup script not executable or not found"
return 1 return 1
fi fi
if [ "$available_space" -lt "$required_space" ]; then local restic_password
log_action "ERROR: Insufficient disk space" restic_password=$(cat "$PASSWORD_FILE")
return 1 local cmd="RESTIC_PASSWORD='$restic_password' LOG_FILE='${BACKUP_LOG_PREFIX}\$(date +\%Y-\%m-\%d_\%H-\%M-\%S).log' $BACKUP_SCRIPT > \"\$LOG_FILE\" 2>&1 || echo \"\$(date) Backup failed\" >> \"$ACTION_LOG_FILE\""
fi
return 0 # Update the cron job
} (crontab -l 2>/dev/null | grep -v "$BACKUP_SCRIPT"; echo "$SCHEDULE $cmd") | crontab -
log_action "Scheduled daily backup at midnight with command: $cmd"
# Validate cron schedule
validate_schedule() {
local schedule="$1"
if ! [[ $schedule =~ ^[0-9,\*/-]+ [0-9,\*/-]+ [0-9,\*/-]+ [0-9,\*/-]+ [0-9,\*/-]+$ ]]; then
log_action "ERROR: Invalid cron schedule format: $schedule"
return 1
fi
return 0
}
# Get Restic password with retry mechanism
get_restic_password() {
local retry_count=0
while [ $retry_count -lt $MAX_RETRIES ]; do
if [ -f "$PASSWORD_FILE" ] && [ -s "$PASSWORD_FILE" ]; then
cat "$PASSWORD_FILE"
return 0
fi
retry_count=$((retry_count + 1))
sleep $RETRY_DELAY
done
log_action "ERROR: Failed to retrieve Restic password after $MAX_RETRIES attempts"
return 1
}
# Enhanced cron job management
add_update_cron_job() {
local action="$1"
local schedule="$2"
local restic_password="$3"
local temp_crontab="/tmp/temp_crontab.$$"
# Validate inputs
if ! validate_schedule "$schedule"; then
return 1
fi
# Prepare cron command with environment variables and error handling
local CMD="RESTIC_PASSWORD='$restic_password' LOG_FILE='${BACKUP_LOG_PREFIX}\$(date +\%Y-\%m-\%d_\%H-\%M-\%S).log' $BACKUP_SCRIPT > \"\$LOG_FILE\" 2>&1 || echo \"\$(date) Backup failed\" >> \"$ACTION_LOG_FILE\""
# Safely update crontab
crontab -l 2>/dev/null | grep -v "$BACKUP_SCRIPT" > "$temp_crontab" || true
echo "$schedule $CMD" >> "$temp_crontab"
if crontab "$temp_crontab"; then
log_action "Successfully ${action}d backup schedule: $schedule"
rm -f "$temp_crontab"
return 0
else
log_action "ERROR: Failed to ${action} backup schedule"
rm -f "$temp_crontab"
return 1
fi
}
# Enhanced cron job removal
remove_cron_job() {
local temp_crontab="/tmp/temp_crontab.$$"
if crontab -l 2>/dev/null | grep -q "$BACKUP_SCRIPT"; then
crontab -l | grep -v "$BACKUP_SCRIPT" > "$temp_crontab"
if crontab "$temp_crontab"; then
log_action "Backup schedule successfully removed"
rm -f "$temp_crontab"
return 0
else
log_action "ERROR: Failed to remove backup schedule"
rm -f "$temp_crontab"
return 1
fi
else
log_action "No backup schedule found to remove"
return 0
fi
} }
# Main execution # Main execution
main() { main() {
# Create log directory with proper permissions
mkdir -p "$LOG_DIR" mkdir -p "$LOG_DIR"
chmod 750 "$LOG_DIR" chmod 750 "$LOG_DIR"
# Ensure single instance
ensure_single_instance ensure_single_instance
configure_cron_job
# Check system requirements
if ! check_system_requirements; then
cleanup_and_exit 1
fi
case $1 in
add|update)
if [ "$#" -ne 3 ]; then
log_action "Usage: $0 {add|update} 'schedule' 'restic_password'"
log_action "Example: $0 add '0 1 * * *' 'secret_password'"
cleanup_and_exit 1
fi
local restic_password="${3:-$(get_restic_password)}"
if [ -z "$restic_password" ]; then
cleanup_and_exit 1
fi
add_update_cron_job "$1" "$2" "$restic_password"
;;
remove)
remove_cron_job
;;
status)
crontab -l | grep "$BACKUP_SCRIPT" || echo "No backup schedule found"
;;
*)
log_action "Invalid action: $1. Use add, update, remove, or status"
cleanup_and_exit 1
;;
esac
cleanup_and_exit 0 cleanup_and_exit 0
} }