manage backup shell upadte
parent
3d8b13e1bb
commit
662621143c
|
@ -1,78 +1,62 @@
|
|||
#!/bin/bash
|
||||
|
||||
# Enable strict error handling
|
||||
set -euo pipefail
|
||||
trap 'handle_error $? $LINENO' ERR
|
||||
|
||||
# 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_"
|
||||
PASSWORD_FILE="/etc/restic-password"
|
||||
LOCK_FILE="/tmp/backup_schedule.lock"
|
||||
SCHEDULE="0 0 * * *" # Default to daily at midnight
|
||||
|
||||
# Error handling function
|
||||
handle_error() {
|
||||
local exit_code=$1
|
||||
local line_no=$2
|
||||
log_action "ERROR: Command failed at line ${line_no} with exit code ${exit_code}"
|
||||
cleanup_and_exit 1
|
||||
}
|
||||
# Ensure the log directory exists
|
||||
mkdir -p "$LOG_DIR"
|
||||
|
||||
# Cleanup function
|
||||
cleanup_and_exit() {
|
||||
local exit_code=$1
|
||||
[ -f "$LOCK_FILE" ] && rm -f "$LOCK_FILE"
|
||||
exit "${exit_code}"
|
||||
}
|
||||
|
||||
# Ensure single instance
|
||||
ensure_single_instance() {
|
||||
if [ -f "$LOCK_FILE" ]; then
|
||||
if kill -0 "$(cat "$LOCK_FILE")" 2>/dev/null; then
|
||||
log_action "ERROR: Another instance is running"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
echo $$ > "$LOCK_FILE"
|
||||
}
|
||||
|
||||
# Logging function
|
||||
log_action() {
|
||||
local timestamp=$(date +'%Y-%m-%d %H:%M:%S')
|
||||
echo "[$timestamp] $1" | tee -a "$ACTION_LOG_FILE"
|
||||
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1" >> "$ACTION_LOG_FILE"
|
||||
}
|
||||
|
||||
# Function to configure the cron job
|
||||
configure_cron_job() {
|
||||
if [ ! -f "$PASSWORD_FILE" ]; then
|
||||
log_action "ERROR: Restic password file not found"
|
||||
return 1
|
||||
# Function to add or update the cron job with dynamic logging
|
||||
add_update_cron_job() {
|
||||
# Verify Restic password is provided
|
||||
if [ -z "$3" ]; then
|
||||
echo "Restic password is required."
|
||||
log_action "Attempted to add/update a schedule without providing a Restic password."
|
||||
exit 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\""
|
||||
# Prepare the cron command to include dynamic date in the backup log filename
|
||||
CMD="RESTIC_PASSWORD=\"$3\" $BACKUP_SCRIPT > \"${BACKUP_LOG_PREFIX}\$(date +\\%Y-\\%m-\\%d_\\%H-\\%M-\\%S).log\" 2>&1"
|
||||
|
||||
log_action "Adding cron job with schedule: $SCHEDULE and command: $cmd"
|
||||
|
||||
# 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"
|
||||
# Add or update the cron job
|
||||
(crontab -l | grep -v "$BACKUP_SCRIPT" 2>/dev/null; echo "$2 $CMD") | crontab -
|
||||
local update_msg="Backup schedule updated to: $2"
|
||||
echo "$update_msg"
|
||||
log_action "$update_msg"
|
||||
}
|
||||
|
||||
# Main execution
|
||||
main() {
|
||||
log_action "Starting manage_backup_schedule.sh"
|
||||
mkdir -p "$LOG_DIR"
|
||||
chmod 750 "$LOG_DIR"
|
||||
ensure_single_instance
|
||||
configure_cron_job
|
||||
cleanup_and_exit 0
|
||||
# Function to remove the cron job
|
||||
remove_cron_job() {
|
||||
crontab -l | grep -v "$BACKUP_SCRIPT" | crontab -
|
||||
local remove_msg="Backup schedule removed."
|
||||
echo "$remove_msg"
|
||||
log_action "$remove_msg"
|
||||
}
|
||||
|
||||
# Execute main function with all arguments
|
||||
main "$@"
|
||||
# Main logic to add, update, or remove the cron job based on user input
|
||||
case $1 in
|
||||
add|update)
|
||||
if [ "$#" -ne 3 ]; then
|
||||
echo "Usage for add/update: $0 {add|update} 'schedule' 'restic_password'"
|
||||
echo "Example: $0 add '0 1 * * *' 'secret_password'"
|
||||
log_action "Incorrect usage for add/update. Correct format not followed."
|
||||
else
|
||||
add_update_cron_job "$@"
|
||||
fi
|
||||
;;
|
||||
remove)
|
||||
remove_cron_job
|
||||
;;
|
||||
*)
|
||||
echo "Invalid action: $1. Use add, update, or remove."
|
||||
log_action "Invalid action attempted: $1"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
|
Loading…
Reference in New Issue