Update logging

main
Anthony 2024-11-13 01:18:35 +08:00
parent 9879f18833
commit 6a19fda7f5
1 changed files with 19 additions and 24 deletions

View File

@ -29,54 +29,49 @@ function update_restic(){
}
function check_backup_repo() {
local backup_repo_path="/mnt/backups/${ENV_NAME}"
local backup_repo_path="/mnt/backups/"
local password_file="/etc/restic-password"
local backup_log_file="/var/log/backup_addon.log" # Set the log file path
# Create the backup repository path if it does not exist
# Ensure the backup repository directory exists
[ -d "${backup_repo_path}" ] || mkdir -p "${backup_repo_path}"
# Check if the password file exists, otherwise create a new random password
if [ -f "$password_file" ]; then
stored_password=$(cat "$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
# 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}"
# Generate a new 10-character random password if password file does not exist or is empty
if [ ! -f "$password_file" ] || [ -z "$(cat "$password_file")" ]; then
local new_password=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 10)
echo "$new_password" > "$password_file"
echo "$(date) Password file created with a new random password." | tee -a "${backup_log_file}"
fi
# Export the repository password
export RESTIC_PASSWORD="$stored_password"
# Load the password from the file for use with Restic
export RESTIC_PASSWORD=$(cat "$password_file")
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
# Check repository integrity if files exist; initialize otherwise
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
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
sendEmailNotification
fi
# Run the repository check command
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
fi
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}"
fi
}
function sendEmailNotification() {
if [ -e "/usr/lib/jelastic/modules/api.module" ]; then
[ -e "/var/run/jem.pid" ] && return 0;