From 4efcaba7c5aa4969b71d7ca6cf77c072d0587329 Mon Sep 17 00:00:00 2001 From: ngosang Date: Sat, 20 Jan 2024 16:00:11 +0100 Subject: [PATCH] Replaced RESTIC_REPO_URL, RESTIC_REPO_PASSWORD and RESTIC_REPO_PASSWORD_FILE environment variables --- README.md | 28 ++++++++++++++-------------- docker-compose.yml | 6 +++--- entrypoint.sh | 19 ++++++++++++++----- restic-exporter.py | 28 ++++++++++++++++++++-------- 4 files changed, 51 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 107f522..5399837 100644 --- a/README.md +++ b/README.md @@ -19,8 +19,8 @@ Requirements: ```bash pip install -r /requirements.txt -export RESTIC_REPO_URL=/data -export RESTIC_REPO_PASSWORD_FILE=/restic_password_file +export RESTIC_REPOSITORY=/data +export RESTIC_PASSWORD_FILE=/restic_password_file python restic-exporter.py ``` @@ -59,9 +59,9 @@ services: container_name: restic-exporter environment: - TZ=Europe/Madrid - - RESTIC_REPO_URL=/data - - RESTIC_REPO_PASSWORD= - # - RESTIC_REPO_PASSWORD_FILE= + - RESTIC_REPOSITORY=/data + - RESTIC_PASSWORD= + # - RESTIC_PASSWORD_FILE= - REFRESH_INTERVAL=1800 # 30 min volumes: - /host_path/restic/data:/data @@ -76,8 +76,8 @@ services: docker run -d \ --name=restic-exporter \ -e TZ=Europe/Madrid \ - -e RESTIC_REPO_URL=/data \ - -e RESTIC_REPO_PASSWORD= \ + -e RESTIC_REPOSITORY=/data \ + -e RESTIC_PASSWORD= \ -e REFRESH_INTERVAL=1800 \ -p 8001:8001 \ --restart unless-stopped \ @@ -91,17 +91,17 @@ Some of them need additional environment variables for the secrets. All configuration is done with environment variables: -- `RESTIC_REPO_URL`: Restic repository URL. All backends are supported. Examples: +- `RESTIC_REPOSITORY`: Restic repository URL. All backends are supported. Examples: * Local repository: `/data` * REST Server: `rest:http://user:password@127.0.0.1:8000/` * Amazon S3: `s3:s3.amazonaws.com/bucket_name` * Backblaze B2: `b2:bucketname:path/to/repo` * Rclone (see notes below): `rclone:gd-backup:/restic` -- `RESTIC_REPO_PASSWORD`: Restic repository password in plain text. This is only -required if `RESTIC_REPO_PASSWORD_FILE` is not defined. -- `RESTIC_REPO_PASSWORD_FILE`: File with the Restic repository password in plain -text. This is only required if `RESTIC_REPO_PASSWORD` is not defined. Remember +- `RESTIC_PASSWORD`: Restic repository password in plain text. This is only +required if `RESTIC_PASSWORD_FILE` is not defined. +- `RESTIC_PASSWORD_FILE`: File with the Restic repository password in plain +text. This is only required if `RESTIC_PASSWORD` is not defined. Remember to mount the Docker volume with the file. - `AWS_ACCESS_KEY_ID`: (Optional) Required for Amazon S3, Minio and Wasabi backends. @@ -138,8 +138,8 @@ services: container_name: restic-exporter environment: - TZ=Europe/Madrid - - RESTIC_REPO_URL=rclone:gd-backup:/restic - - RESTIC_REPO_PASSWORD= + - RESTIC_REPOSITORY=rclone:gd-backup:/restic + - RESTIC_PASSWORD= - REFRESH_INTERVAL=1800 # 30 min volumes: - /host_path/restic/data:/data diff --git a/docker-compose.yml b/docker-compose.yml index 82a8d44..0120506 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,9 +6,9 @@ services: container_name: restic-exporter environment: - TZ=Europe/Madrid - - RESTIC_REPO_URL=/data - - RESTIC_REPO_PASSWORD=password_here - # - RESTIC_REPO_PASSWORD_FILE=/file_with_password_here + - RESTIC_REPOSITORY=/data + - RESTIC_PASSWORD=password_here + # - RESTIC_PASSWORD_FILE=/file_with_password_here - REFRESH_INTERVAL=1800 # 30 min volumes: - /host_path/restic/data:/data diff --git a/entrypoint.sh b/entrypoint.sh index 9e17911..d8942d7 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -3,14 +3,23 @@ # Exit on error. For debug use set -x set -e -if [ -z "${RESTIC_REPO_PASSWORD}" ]; then - if [ -z "${RESTIC_REPO_PASSWORD_FILE}" ]; then - echo "You have to define one of these environment variables: RESTIC_REPO_PASSWORD or RESTIC_REPO_PASSWORD_FILE" +if [ -n "${RESTIC_REPO_PASSWORD}" ]; then + echo "The environment variable RESTIC_REPO_PASSWORD is deprecated, please use RESTIC_PASSWORD instead." + export RESTIC_PASSWORD="${RESTIC_REPO_PASSWORD}" +fi +if [ -n "${RESTIC_REPO_PASSWORD_FILE}" ]; then + echo "The environment variable RESTIC_REPO_PASSWORD_FILE is deprecated, please use RESTIC_PASSWORD_FILE instead." + export RESTIC_PASSWORD_FILE="${RESTIC_REPO_PASSWORD_FILE}" +fi + +if [ -z "${RESTIC_PASSWORD}" ]; then + if [ -z "${RESTIC_PASSWORD_FILE}" ]; then + echo "You have to define one of these environment variables: RESTIC_PASSWORD or RESTIC_PASSWORD_FILE" exit 1 fi else - export RESTIC_REPO_PASSWORD_FILE="/tmp/restic_passwd" - echo "${RESTIC_REPO_PASSWORD}" > "${RESTIC_REPO_PASSWORD_FILE}" + export RESTIC_PASSWORD_FILE="/tmp/restic_passwd" + echo "${RESTIC_PASSWORD}" > "${RESTIC_PASSWORD_FILE}" fi /usr/local/bin/python -u /restic-exporter.py diff --git a/restic-exporter.py b/restic-exporter.py index 27f9fab..53cac95 100644 --- a/restic-exporter.py +++ b/restic-exporter.py @@ -347,16 +347,28 @@ if __name__ == "__main__": logging.info("Starting Restic Prometheus Exporter") logging.info("It could take a while if the repository is remote") - try: - restic_repo_url = os.environ["RESTIC_REPO_URL"] - except Exception: - logging.error("The environment variable RESTIC_REPO_URL is mandatory") + restic_repo_url = os.environ.get("RESTIC_REPOSITORY") + if restic_repo_url is None: + restic_repo_url = os.environ.get("RESTIC_REPO_URL") + if restic_repo_url is not None: + logging.warning( + "The environment variable RESTIC_REPO_URL is deprecated, " + "please use RESTIC_REPOSITORY instead." + ) + if restic_repo_url is None: + logging.error("The environment variable RESTIC_REPOSITORY is mandatory") sys.exit(1) - try: - restic_repo_password_file = os.environ["RESTIC_REPO_PASSWORD_FILE"] - except Exception: - logging.error("The environment variable RESTIC_REPO_PASSWORD_FILE is mandatory") + restic_repo_password_file = os.environ.get("RESTIC_PASSWORD_FILE") + if restic_repo_password_file is None: + restic_repo_password_file = os.environ.get("RESTIC_REPO_PASSWORD_FILE") + if restic_repo_password_file is not None: + logging.warning( + "The environment variable RESTIC_REPO_PASSWORD_FILE is deprecated, " + "please use RESTIC_PASSWORD_FILE instead." + ) + if restic_repo_password_file is None: + logging.error("The environment variable RESTIC_PASSWORD_FILE is mandatory") sys.exit(1) exporter_address = os.environ.get("LISTEN_ADDRESS", "0.0.0.0")