Updated restic password

main
Anthony 2024-11-13 01:13:53 +08:00
parent 3b5ab6e98b
commit 9879f18833
1 changed files with 14 additions and 10 deletions

View File

@ -28,28 +28,31 @@ 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)
@ -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;