From dc23173e2d9078c08175e7fb55b4716ae93b9b7e Mon Sep 17 00:00:00 2001 From: Victor Date: Thu, 22 Jan 2026 14:15:56 +0100 Subject: [PATCH] add prune cache support --- Dockerfile | 9 ++++--- backup.sh | 7 +++++ cache-cleanup.sh | 52 ++++++++++++++++++++++++++++++++++++++ docker-compose.example.yml | 5 ++++ prune.sh | 6 +++++ start.sh | 1 + 6 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 cache-cleanup.sh diff --git a/Dockerfile b/Dockerfile index 5cc556d..78f3f47 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,7 +2,7 @@ FROM debian:bookworm-slim # Define the version of Rustic to install as a build argument -ARG RUSTIC_VERSION=v0.10.2 +ARG RUSTIC_VERSION=v0.10.3 # Set DEBIAN_FRONTEND to noninteractive to suppress warnings ENV DEBIAN_FRONTEND=noninteractive @@ -11,8 +11,8 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ cron bash curl tzdata ca-certificates && \ rm -rf /var/lib/apt/lists/* -# RUN curl -L https://github.com/rustic-rs/rustic/releases/download/${RUSTIC_VERSION}/rustic-${RUSTIC_VERSION}-x86_64-unknown-linux-musl.tar.gz \ -RUN curl -L https://github.com/rustic-rs/rustic/releases/download/v0.10.2/rustic-v0.10.2-1-g189b17c-x86_64-unknown-linux-musl.tar.gz \ +RUN curl -L https://github.com/rustic-rs/rustic/releases/download/${RUSTIC_VERSION}/rustic-${RUSTIC_VERSION}-aarch64-unknown-linux-musl.tar.gz \ +# RUN curl -L https://github.com/rustic-rs/rustic/releases/download/v0.10.2/rustic-v0.10.2-1-g189b17c-x86_64-unknown-linux-musl.tar.gz \ | tar -xvzf - -C /usr/local/bin rustic && \ \ curl -o /usr/local/bin/mc https://dl.min.io/client/mc/release/linux-amd64/mc && \ @@ -28,6 +28,7 @@ COPY backup.sh /usr/local/bin/backup.sh COPY restore.sh /usr/local/bin/restore.sh COPY start.sh /start.sh COPY prune.sh /usr/local/bin/prune.sh -RUN chmod +x /usr/local/bin/backup.sh /usr/local/bin/restore.sh /start.sh /usr/local/bin/prune.sh +COPY cache-cleanup.sh /usr/local/bin/cache-cleanup.sh +RUN chmod +x /usr/local/bin/backup.sh /usr/local/bin/restore.sh /start.sh /usr/local/bin/prune.sh /usr/local/bin/cache-cleanup.sh CMD ["/start.sh"] \ No newline at end of file diff --git a/backup.sh b/backup.sh index 62501ac..2c7419c 100644 --- a/backup.sh +++ b/backup.sh @@ -78,6 +78,13 @@ if [ $STATUS -ne 0 ]; then exit 1 fi echo "$(date): Backup successfully completed." + +# --- Optional Cache Cleanup --- +if [ -n "$CACHE_CLEANUP_DAYS" ] && [ "$CACHE_CLEANUP_DAYS" -gt 0 ]; then + echo "$(date): --> Running cache cleanup..." + /usr/local/bin/cache-cleanup.sh +fi + echo "$(date): --- ALL BACKUP TASKS COMPLETED SUCCESSFULLY. ---" # ---------------------------------------------------- diff --git a/cache-cleanup.sh b/cache-cleanup.sh new file mode 100644 index 0000000..68e0231 --- /dev/null +++ b/cache-cleanup.sh @@ -0,0 +1,52 @@ +#!/bin/bash +# cache-cleanup.sh: Clean up old cache files to free disk space. + +set -e + +send_notification() { + local status=$1 + local message=$2 + + if [ -n "$N8N_WEBHOOK_URL" ]; then + curl -X POST -H "Content-Type: application/json" \ + -d "{\"hostname\": \"${HOSTNAME}\", \"status\": \"${status}\", \"message\": \"${message}\"}" \ + "${N8N_WEBHOOK_URL}" --silent --output /dev/null || echo "$(date): WARNING: Failed to send notification to n8n." + fi +} + +trap 'send_notification "failure" "Cache cleanup script failed at line $LINENO. Check container logs for details."' ERR + +# --- Configuration --- +CACHE_DIR="/root/.cache/rustic" +CLEANUP_DAYS=${CACHE_CLEANUP_DAYS:-7} + +echo "$(date): --> Starting cache cleanup process..." +echo "$(date): Cache directory: ${CACHE_DIR}" +echo "$(date): Removing files not accessed for more than ${CLEANUP_DAYS} day(s)..." + +# Check if cache directory exists +if [ ! -d "${CACHE_DIR}" ]; then + echo "$(date): Cache directory does not exist. Skipping cleanup." + exit 0 +fi + +# Get initial size +INITIAL_SIZE=$(du -sh "${CACHE_DIR}" 2>/dev/null | cut -f1) +echo "$(date): Initial cache size: ${INITIAL_SIZE}" + +# Find and delete files not accessed for more than CLEANUP_DAYS +# Using -atime (access time) based on the user's successful command +DELETED_COUNT=$(find "${CACHE_DIR}" -type f -atime +${CLEANUP_DAYS} -delete -print | wc -l) + +# Get final size +FINAL_SIZE=$(du -sh "${CACHE_DIR}" 2>/dev/null | cut -f1 || echo "0K") +echo "$(date): Final cache size: ${FINAL_SIZE}" +echo "$(date): Deleted ${DELETED_COUNT} old cache file(s)." + +if [ ${DELETED_COUNT} -gt 0 ]; then + CLEANUP_MESSAGE="Cache cleanup completed. Removed ${DELETED_COUNT} file(s). Final cache size: ${FINAL_SIZE}" + echo "$(date): ${CLEANUP_MESSAGE}" + send_notification "success" "${CLEANUP_MESSAGE}" +else + echo "$(date): No old cache files found to delete." +fi diff --git a/docker-compose.example.yml b/docker-compose.example.yml index 24a58ab..449ccf8 100644 --- a/docker-compose.example.yml +++ b/docker-compose.example.yml @@ -35,6 +35,11 @@ services: # Optional: Max random delay in seconds to wait before starting the backup. - RANDOM_DELAY_SECONDS=3600 + # Optional: Cache cleanup configuration (days to keep cache files). + # Files not accessed for longer than this period will be deleted. + # Set to 0 or leave empty to disable cache cleanup. + - CACHE_CLEANUP_DAYS=7 + # Optional: Webhook URL for n8n to send notifications (for success and failure). - N8N_WEBHOOK_URL="http://your-n8n-instance/webhook/your-id" diff --git a/prune.sh b/prune.sh index d98e0e3..c2be176 100644 --- a/prune.sh +++ b/prune.sh @@ -42,5 +42,11 @@ rustic prune echo "$(date): --> Running integrity check (check)..." rustic check ${CHECK_ARGS} +# --- Optional Cache Cleanup --- +if [ -n "$CACHE_CLEANUP_DAYS" ] && [ "$CACHE_CLEANUP_DAYS" -gt 0 ]; then + echo "$(date): --> Running cache cleanup..." + /usr/local/bin/cache-cleanup.sh +fi + echo "$(date): --- MAINTENANCE COMPLETED SUCCESSFULLY. ---" send_notification "success" "Rustic maintenance (forget/prune/check) completed successfully." \ No newline at end of file diff --git a/start.sh b/start.sh index 0f129aa..2064ead 100644 --- a/start.sh +++ b/start.sh @@ -26,6 +26,7 @@ VARS_TO_SANITIZE=( "BACKUP_PATHS" "PRUNE_POLICY" "CHECK_ARGS" + "CACHE_CLEANUP_DAYS" ) for VAR_NAME in "${VARS_TO_SANITIZE[@]}"; do