add prune cache support
ci/woodpecker/tag/woodpecker Pipeline was successful

This commit is contained in:
2026-01-22 14:15:56 +01:00
parent cf02cd0829
commit dc23173e2d
6 changed files with 76 additions and 4 deletions
+5 -4
View File
@@ -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"]
+7
View File
@@ -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. ---"
# ----------------------------------------------------
+52
View File
@@ -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
+5
View File
@@ -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"
+6
View File
@@ -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."
+1
View File
@@ -26,6 +26,7 @@ VARS_TO_SANITIZE=(
"BACKUP_PATHS"
"PRUNE_POLICY"
"CHECK_ARGS"
"CACHE_CLEANUP_DAYS"
)
for VAR_NAME in "${VARS_TO_SANITIZE[@]}"; do