From 5229f32497a4516713e4847aa7fda7e67eda1ed8 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Thu, 24 Oct 2024 10:38:59 +0200 Subject: [PATCH] Add NTFY support --- Dockerfile | 4 +++- backup.sh | 47 +++++++++++++++++++++++++++++++++++---- entrypoint.sh | 16 ++++++------- gdrive.docker-compose.yml | 46 ++++++++++++++++++++------------------ restore.sh | 8 +++---- 5 files changed, 82 insertions(+), 39 deletions(-) diff --git a/Dockerfile b/Dockerfile index 5626aae..fda1a0b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,7 +5,7 @@ FROM alpine:latest AS final # RUN pacman --noconfirm -Syu duplicity python-pip python-pydrive2 ENV CARGO_NET_GIT_FETCH_WITH_CLI=true RUN \ - apk add py3-pip python3-dev gcc libffi-dev musl-dev openssl-dev pkgconfig duplicity rust cargo git && \ + apk add py3-pip python3-dev gcc libffi-dev musl-dev openssl-dev pkgconfig duplicity rust cargo git curl && \ pip install --upgrade pip && \ pip install pydrive2 && \ apk del rust musl-dev libffi-dev gcc python3-dev cargo git pkgconfig openssl-dev @@ -32,3 +32,5 @@ LABEL org.opencontainers.image.authors="Stefano Pigozzi " # Configure duplicity ENV DUPLICITY_FULL_IF_OLDER_THAN=1M +ENV NTFY="" +ENV NTFY_TAGS="" diff --git a/backup.sh b/backup.sh index b00702e..0c30b1e 100755 --- a/backup.sh +++ b/backup.sh @@ -8,8 +8,47 @@ set -e export PASSPHRASE=$(cat "${DUPLICITY_PASSPHRASE_FILE}") echo "Launched in backup mode, performing backup..." >> /dev/stderr + +if [ -n "${NTFY}" ]; then + echo "Sending ntfy backup start notification..." >> /dev/stderr + curl "${NTFY}" \ + --silent \ + --header "X-Title: Backup started" \ + --data "Duplicity is attempting to perform a backup to **${DUPLICITY_TARGET_URL}**..." \ + --header "X-Priority: min" \ + --header "X-Tags: arrow_heading_up,${NTFY_TAGS}" \ + --header "Content-Type: text/markdown" +fi + duplicity \ - --allow-source-mismatch \ - --full-if-older-than "${DUPLICITY_FULL_IF_OLDER_THAN}" \ - /mnt \ - "${DUPLICITY_TARGET_URL}" + --allow-source-mismatch \ + --full-if-older-than "${DUPLICITY_FULL_IF_OLDER_THAN}" \ + /mnt \ + "${DUPLICITY_TARGET_URL}" + +backup_result=$? + +if [ -n "${NTFY}" ]; then + case "$backup_result" in + 0) + echo "Sending ntfy backup complete notification..." >> /dev/stderr + curl "${NTFY}" \ + --silent \ + --header "X-Title: Backup complete" \ + --data "Duplicity has successfully performed a backup to **${DUPLICITY_TARGET_URL}**!" \ + --header "X-Priority: low" \ + --header "X-Tags: white_check_mark,${NTFY_TAGS}" \ + --header "Content-Type: text/markdown" + ;; + *) + echo "Sending ntfy backup failed notification..." >> /dev/stderr + curl "${NTFY}" \ + --silent \ + --header "X-Title: Backup failed" \ + --data "Duplicity failed to perform a backup to **${DUPLICITY_TARGET_URL}**, and exited with status code **${backup_result}**." \ + --header "X-Priority: max" \ + --header "X-Tags: sos,${NTFY_TAGS}" \ + --header "Content-Type: text/markdown" + ;; + esac +fi diff --git a/entrypoint.sh b/entrypoint.sh index 0eb1a0c..cbeb96f 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -2,16 +2,16 @@ case "$MODE" in backup) - echo "Running first backup..." - /etc/periodic/daily/backup.sh - echo "Running cron for daily backups..." - crond -f -l 0 - echo "Cron has exited." + echo "Running first backup..." + /etc/periodic/daily/backup.sh + echo "Running cron for daily backups..." + crond -f -l 0 + echo "Cron has exited." ;; restore) - echo "Restoring from latest backup..." - /usr/lib/backup-duplicity/restore.sh - echo "Done." + echo "Restoring from latest backup..." + /usr/lib/backup-duplicity/restore.sh + echo "Done." ;; *) echo "No such mode." >> /dev/stderr diff --git a/gdrive.docker-compose.yml b/gdrive.docker-compose.yml index c57fcf6..84a2196 100644 --- a/gdrive.docker-compose.yml +++ b/gdrive.docker-compose.yml @@ -1,27 +1,29 @@ secrets: - google_client_config: - file: "./google_client_config.yml" - duplicity_passphrase: - file: "./duplicity_passphrase.txt" + google_client_config: + file: "./google_client_config.yml" + duplicity_passphrase: + file: "./duplicity_passphrase.txt" volumes: - duplicity_credentials: - external: true + duplicity_credentials: + external: true services: - duplicity: - image: "ghcr.io/steffo99/backup-duplicity:latest" - entrypoint: "/bin/sh" - command: "/etc/periodic/daily/backup.sh" - restart: unless-stopped - volumes: - - "./exampledata:/mnt/example" - - "duplicity_credentials:/var/lib/duplicity" - environment: - MODE: "backup" - DUPLICITY_PASSPHRASE_FILE: "/run/secrets/duplicity_passphrase" - DUPLICITY_TARGET_URL: "pydrive://641079776729-90s4tnli0ao913ajrpv8cp3c4kkk77j5.apps.googleusercontent.com/Duplicity/this" - GOOGLE_DRIVE_SETTINGS: "/run/secrets/google_client_config" - secrets: - - google_client_config - - duplicity_passphrase + duplicity: + image: "ghcr.io/steffo99/backup-duplicity:latest" + entrypoint: "/bin/sh" + command: "/etc/periodic/daily/backup.sh" + restart: unless-stopped + volumes: + - "./exampledata:/mnt/example" + - "duplicity_credentials:/var/lib/duplicity" + environment: + MODE: "backup" + DUPLICITY_PASSPHRASE_FILE: "/run/secrets/duplicity_passphrase" + DUPLICITY_TARGET_URL: "pydrive://641079776729-90s4tnli0ao913ajrpv8cp3c4kkk77j5.apps.googleusercontent.com/Duplicity/this" + GOOGLE_DRIVE_SETTINGS: "/run/secrets/google_client_config" + NTFY: "https://ntfy.sh/garasauto" + NTFY_TAGS: "garasauto" + secrets: + - google_client_config + - duplicity_passphrase diff --git a/restore.sh b/restore.sh index 4e2c24c..9259cdd 100755 --- a/restore.sh +++ b/restore.sh @@ -9,7 +9,7 @@ export PASSPHRASE=$(cat "${DUPLICITY_PASSPHRASE_FILE}") echo "Launched in restore mode, restoring backup..." >> /dev/stderr duplicity \ - --force \ - --allow-source-mismatch \ - "${DUPLICITY_TARGET_URL}" \ - /mnt + --force \ + --allow-source-mismatch \ + "${DUPLICITY_TARGET_URL}" \ + /mnt