Compare commits

...

2 Commits

Author SHA1 Message Date
Anthony 6a19fda7f5 Update logging 2024-11-13 01:18:35 +08:00
Anthony 9879f18833 Updated restic password 2024-11-13 01:13:53 +08:00
1 changed files with 22 additions and 23 deletions

View File

@ -28,51 +28,50 @@ function update_restic(){
restic self-update 2>&1;
}
function check_backup_repo(){
local backup_repo_path="/mnt/backups/${ENV_NAME}"
function check_backup_repo() {
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 and if it matches the ENV_NAME
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"
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 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="$ENV_NAME"
# 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;