Compare commits

..

No commits in common. "6a19fda7f5e380e3bbae74147fb63fd59726a391" and "3b5ab6e98bafcb050553701a6dceddadab0b4b1a" have entirely different histories.

1 changed files with 23 additions and 22 deletions

View File

@ -28,50 +28,51 @@ function update_restic(){
restic self-update 2>&1; restic self-update 2>&1;
} }
function check_backup_repo() { function check_backup_repo(){
local backup_repo_path="/mnt/backups/" local backup_repo_path="/mnt/backups/${ENV_NAME}"
local password_file="/etc/restic-password" local password_file="/etc/restic-password"
local backup_log_file="/var/log/backup_addon.log" # Set the log file path
# Ensure the backup repository directory exists # Create the backup repository path if it does not exist
[ -d "${backup_repo_path}" ] || mkdir -p "${backup_repo_path}" [ -d "${backup_repo_path}" ] || mkdir -p "${backup_repo_path}"
# Generate a new 10-character random password if password file does not exist or is empty # Check if the password file exists and if it matches the ENV_NAME
if [ ! -f "$password_file" ] || [ -z "$(cat "$password_file")" ]; then if [ -f "$password_file" ]; then
local new_password=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 10) stored_password=$(cat "$password_file")
echo "$new_password" > "$password_file" if [ "$stored_password" != "$ENV_NAME" ]; then
echo "$(date) Password file created with a new random password." | tee -a "${backup_log_file}" echo "$(date) Password mismatch detected. Updating password to ${ENV_NAME}" | tee -a "${BACKUP_LOG_FILE}"
echo "$ENV_NAME" > "$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}"
fi fi
# Load the password from the file for use with Restic # Export the repository password
export RESTIC_PASSWORD=$(cat "$password_file") export RESTIC_PASSWORD="$ENV_NAME"
export RESTIC_REPOSITORY="$backup_repo_path" export RESTIC_REPOSITORY="$backup_repo_path"
export FILES_COUNT=$(find "${backup_repo_path}" -mindepth 1 | wc -l) export FILES_COUNT=$(find "${backup_repo_path}" -mindepth 1 | wc -l)
# Check repository integrity if files exist; initialize otherwise # Check repository integrity if it has files, otherwise initialize it
if [ "${FILES_COUNT}" -gt 0 ]; then if [ "${FILES_COUNT}" -gt 0 ]; then
echo "$(date) ${ENV_NAME} Checking the backup repository integrity and consistency" | tee -a "${backup_log_file}" echo "$(date) ${ENV_NAME} Checking the backup repository integrity and consistency" | tee -a "${BACKUP_LOG_FILE}"
# Remove stale lock if exists
if [[ $(ls -A "${backup_repo_path}/locks") ]]; then if [[ $(ls -A "${backup_repo_path}/locks") ]]; then
echo "$(date) ${ENV_NAME} Backup repository has a stale lock, removing" | tee -a "${backup_log_file}" echo "$(date) ${ENV_NAME} Backup repository has a stale lock, removing" | tee -a "${BACKUP_LOG_FILE}"
GOGC=20 restic -r "${backup_repo_path}" unlock GOGC=20 restic -r "${backup_repo_path}" unlock
sendEmailNotification sendEmailNotification
fi fi
# Run the repository check command
if ! GOGC=20 restic -q -r "${backup_repo_path}" check --read-data-subset=5%; then if ! GOGC=20 restic -q -r "${backup_repo_path}" check --read-data-subset=5%; then
echo "Backup repository integrity check failed." | tee -a "${backup_log_file}" echo "Backup repository integrity check failed." | tee -a "${BACKUP_LOG_FILE}"
exit 1 exit 1
fi fi
else else
echo "$(date) ${ENV_NAME} Initializing new backup repository" | tee -a "${backup_log_file}" echo "$(date) ${ENV_NAME} Initializing new backup repository" | tee -a "${BACKUP_LOG_FILE}"
GOGC=20 restic init -r "${backup_repo_path}" GOGC=20 restic init -r "${backup_repo_path}"
fi fi
} }
function sendEmailNotification() { function sendEmailNotification() {
if [ -e "/usr/lib/jelastic/modules/api.module" ]; then if [ -e "/usr/lib/jelastic/modules/api.module" ]; then
[ -e "/var/run/jem.pid" ] && return 0; [ -e "/var/run/jem.pid" ] && return 0;