Simplified Daily Backup Enable
parent
df5b7acf3e
commit
f830e4de69
51
manifest.jps
51
manifest.jps
|
@ -23,46 +23,12 @@ onInstall:
|
|||
- importScripts
|
||||
|
||||
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:
|
||||
submitUnchanged: true
|
||||
fields:
|
||||
- name: blabel
|
||||
caption: Backup Label
|
||||
type: string
|
||||
|
||||
restoreSettings:
|
||||
submitUnchanged: true
|
||||
fields:
|
||||
|
@ -83,7 +49,6 @@ menu:
|
|||
confirmText: Configure automated backup schedule?
|
||||
loadingText: Setting up backup schedule...
|
||||
successText: Backup schedule configured successfully
|
||||
settings: scheduleSettings
|
||||
title: Configure Automated Backup Schedule
|
||||
submitButtonText: Save Schedule
|
||||
|
||||
|
@ -184,20 +149,18 @@ actions:
|
|||
- cmd[cp]:
|
||||
user: root
|
||||
commands: |
|
||||
if [ ! -f /etc/restic-password ]; then
|
||||
echo "Error: Restic password file not found"
|
||||
if [ ! -x "/home/litespeed/mb-backups/manage_backup_schedule.sh" ]; then
|
||||
echo "Error: manage_backup_schedule.sh not found or not executable"
|
||||
exit 1
|
||||
fi
|
||||
RESTIC_PWD=$(cat /etc/restic-password)
|
||||
if [ "${settings.frequency}" = "daily" ]; then
|
||||
cron_schedule="0 ${settings.hour} * * *"
|
||||
else
|
||||
cron_schedule="0 ${settings.hour} * * ${settings.dayOfWeek}"
|
||||
bash "/home/litespeed/mb-backups/manage_backup_schedule.sh"
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "Error executing manage_backup_schedule.sh"
|
||||
exit 1
|
||||
fi
|
||||
bash ${globals.scriptPath}/manage_backup_schedule.sh add "$cron_schedule" "$RESTIC_PWD"
|
||||
- return:
|
||||
type: info
|
||||
message: "${response.out}"
|
||||
message: "Daily backup schedule configured successfully"
|
||||
|
||||
removeAutoBackup:
|
||||
- cmd[cp]:
|
||||
|
|
|
@ -11,8 +11,7 @@ ACTION_LOG_FILE="${LOG_DIR}/schedule_actions.log"
|
|||
BACKUP_LOG_PREFIX="${LOG_DIR}/backup_"
|
||||
PASSWORD_FILE="/etc/restic-password"
|
||||
LOCK_FILE="/tmp/backup_schedule.lock"
|
||||
MAX_RETRIES=3
|
||||
RETRY_DELAY=5
|
||||
SCHEDULE="0 0 * * *" # Default to daily at midnight
|
||||
|
||||
# Error handling function
|
||||
handle_error() {
|
||||
|
@ -40,153 +39,34 @@ ensure_single_instance() {
|
|||
echo $$ > "$LOCK_FILE"
|
||||
}
|
||||
|
||||
# Enhanced logging function
|
||||
# Logging function
|
||||
log_action() {
|
||||
local timestamp=$(date +'%Y-%m-%d %H:%M:%S')
|
||||
local log_msg="[$timestamp] $1"
|
||||
echo "$log_msg" | tee -a "$ACTION_LOG_FILE"
|
||||
|
||||
# Log critical errors to system log
|
||||
if [[ "$1" == *"ERROR"* ]]; then
|
||||
logger -t "backup-schedule" "$1"
|
||||
fi
|
||||
echo "[$timestamp] $1" | tee -a "$ACTION_LOG_FILE"
|
||||
}
|
||||
|
||||
# Validate system requirements
|
||||
check_system_requirements() {
|
||||
local required_space=1048576 # 1GB in KB
|
||||
local available_space=$(df -k "$LOG_DIR" | awk 'NR==2 {print $4}')
|
||||
|
||||
if [ ! -x "$BACKUP_SCRIPT" ]; then
|
||||
log_action "ERROR: Backup script not executable or not found"
|
||||
# Function to configure the cron job
|
||||
configure_cron_job() {
|
||||
if [ ! -f "$PASSWORD_FILE" ]; then
|
||||
log_action "ERROR: Restic password file not found"
|
||||
return 1
|
||||
fi
|
||||
|
||||
if [ "$available_space" -lt "$required_space" ]; then
|
||||
log_action "ERROR: Insufficient disk space"
|
||||
return 1
|
||||
fi
|
||||
local restic_password
|
||||
restic_password=$(cat "$PASSWORD_FILE")
|
||||
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\""
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# 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
|
||||
# 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"
|
||||
}
|
||||
|
||||
# Main execution
|
||||
main() {
|
||||
# Create log directory with proper permissions
|
||||
mkdir -p "$LOG_DIR"
|
||||
chmod 750 "$LOG_DIR"
|
||||
|
||||
# Ensure single instance
|
||||
ensure_single_instance
|
||||
|
||||
# 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
|
||||
|
||||
configure_cron_job
|
||||
cleanup_and_exit 0
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue