diff --git a/scripts/backup-logic.sh b/scripts/backup-logic.sh index 2170c3b..e1d5e5c 100644 --- a/scripts/backup-logic.sh +++ b/scripts/backup-logic.sh @@ -28,31 +28,34 @@ function update_restic(){ restic self-update 2>&1; } -function check_backup_repo(){ +function check_backup_repo() { local backup_repo_path="/mnt/backups/${ENV_NAME}" local password_file="/etc/restic-password" # Create the backup repository path if it does not exist [ -d "${backup_repo_path}" ] || mkdir -p "${backup_repo_path}" - # Check if the password file exists and if it matches the ENV_NAME + # Check if the password file exists, otherwise create a new random password if [ -f "$password_file" ]; then stored_password=$(cat "$password_file") - if [ "$stored_password" != "$ENV_NAME" ]; then - echo "$(date) Password mismatch detected. Updating password to ${ENV_NAME}" | tee -a "${BACKUP_LOG_FILE}" - echo "$ENV_NAME" > "$password_file" + if [ -z "$stored_password" ] || [[ ${#stored_password} -lt 10 ]]; then + # Generate a new password if the existing one is less than 10 characters + echo "$(date) Password is invalid or too short. Generating a new password." | tee -a "${BACKUP_LOG_FILE}" + stored_password=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 10) + echo "$stored_password" > "$password_file" fi else - # Create the password file if it doesn't exist - echo "$ENV_NAME" > "$password_file" - echo "$(date) Password file created with password: ${ENV_NAME}" | tee -a "${BACKUP_LOG_FILE}" + # Generate and save a random 10-character password + stored_password=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 10) + echo "$stored_password" > "$password_file" + echo "$(date) Password file created with password: ${stored_password}" | tee -a "${BACKUP_LOG_FILE}" fi # Export the repository password - export RESTIC_PASSWORD="$ENV_NAME" + export RESTIC_PASSWORD="$stored_password" export RESTIC_REPOSITORY="$backup_repo_path" export FILES_COUNT=$(find "${backup_repo_path}" -mindepth 1 | wc -l) - + # Check repository integrity if it has files, otherwise initialize it if [ "${FILES_COUNT}" -gt 0 ]; then echo "$(date) ${ENV_NAME} Checking the backup repository integrity and consistency" | tee -a "${BACKUP_LOG_FILE}" @@ -73,6 +76,7 @@ function check_backup_repo(){ fi } + function sendEmailNotification() { if [ -e "/usr/lib/jelastic/modules/api.module" ]; then [ -e "/var/run/jem.pid" ] && return 0;