More improvements and fixes

main
Anthony 2025-01-06 23:48:39 +08:00
parent 1291e4a255
commit c6aa1a191a
2 changed files with 31 additions and 30 deletions

View File

@ -148,7 +148,7 @@ actions:
configureAutoBackup:
- cmd[cp]:
user: root
commands: bash "/home/jelastic/mb-backups/manage_backup_schedule.sh" add "0 0 * * *" "$(cat /etc/restic-password)"
commands: bash "/home/jelastic/mb-backups/manage_backup_schedule.sh" add "0 0 * * *" "$(cat /etc/restic-password)" "${globals.envName}"
- return:
type: info
message: "Daily backup schedule configured successfully"
@ -156,7 +156,7 @@ actions:
removeAutoBackup:
- cmd[cp]:
user: root
commands: bash /home/litespeed/mb-backups/manage_backup_schedule.sh remove
commands: bash /home/litespeed/mb-backups/manage_backup_schedule.sh remove "${globals.envName}"
- return:
type: info
message: "${response.out}"
@ -172,7 +172,7 @@ actions:
backupnow:
- cmd[cp]:
user: root
commands: bash /home/jelastic/mb-backups/backup-logic.sh "backup"
commands: bash /home/jelastic/mb-backups/backup-logic.sh "${globals.envName}" "backup"
- return:
type: info
message: "${response.out}"

View File

@ -1,27 +1,30 @@
#!/bin/bash
set -e # Exit on error
trap 'echo "[$(date +'%Y-%m-%d %H:%M:%S')] ERROR: An error occurred. Exiting." | tee -a "$LOG_FILE"' ERR
trap 'log_message "ERROR: An error occurred. Exiting."' ERR
# Global Configurations
LOG_FILE="/var/log/backup_script.log"
BACKUP_REPO_PATH="/mnt/backups/${ENV_NAME}"
PASSWORD_FILE="/etc/restic-password"
RESTIC_CONFIG_FILE="/etc/restic-config"
DEFAULT_BACKUP_COUNT=5
# Validate required variables
validate_inputs() {
: "${ENV_NAME:?ENV_NAME not set or empty. Please provide an environment name.}"
: "${RESTIC_PASSWORD:?RESTIC_PASSWORD not set}"
: "${APP_PATH:?APP_PATH not set}"
# Dynamically set the backup path
BACKUP_REPO_PATH="/mnt/backups/${ENV_NAME}"
[ -d "$BACKUP_REPO_PATH" ] || mkdir -p "$BACKUP_REPO_PATH"
export BACKUP_REPO_PATH
}
# Logging function
log_message() {
local message="$1"
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $message" | tee -a "$LOG_FILE"
}
# Validate required variables
validate_inputs() {
: "${RESTIC_PASSWORD:?RESTIC_PASSWORD not set}"
: "${BACKUP_REPO_PATH:?BACKUP_REPO_PATH not set}"
}
# Ensure Restic is installed
install_restic() {
if ! which restic &>/dev/null; then
@ -40,8 +43,6 @@ install_restic() {
# Initialize or validate repository
check_backup_repo() {
[ -d "$BACKUP_REPO_PATH" ] || mkdir -p "$BACKUP_REPO_PATH"
if ! restic -r "$BACKUP_REPO_PATH" snapshots &>/dev/null; then
log_message "Initializing new Restic repository..."
restic -r "$BACKUP_REPO_PATH" init