add backup image

This commit is contained in:
Morten Olsen
2025-09-05 23:07:28 +02:00
parent 0c53bf72e4
commit a27dd320e6
219 changed files with 184 additions and 161 deletions

10
images/backup/Dockerfile Normal file
View File

@@ -0,0 +1,10 @@
FROM alpine/git:latest
# or a more specific image like a Debian slim or Ubuntu base image.
RUN apk add --no-cache restic fuse-overlayfs
WORKDIR /app
COPY backup.sh /app/backup.sh
COPY cleanup.sh /app/cleanup.sh
# Make scripts executable
RUN chmod +x /app/backup.sh /app/cleanup.sh

35
images/backup/backup.sh Normal file
View File

@@ -0,0 +1,35 @@
#!/bin/sh
set -e
if [ -z "$RESTIC_PASSWORD" ]; then
echo "Error: RESTIC_PASSWORD environment variable is not set." >&2
exit 1
fi
RESTIC_REPOSITORY="/mnt/backup"
SOURCE_DIR="/mnt/source"
mkdir -p "$SOURCE_DIR"
mkdir -p "/mnt/backup"
echo "Starting Restic backup from $SOURCE_DIR to $RESTIC_REPOSITORY"
echo "Checking/Initializing Restic repository..."
restic init --repository "$RESTIC_REPOSITORY" || true
echo "Running Restic backup..."
restic backup \
"$SOURCE_DIR" \
--verbose \
--tag "daily" \
--exclude-cache \
if [ $? -eq 0 ]; then
echo "Restic backup completed successfully!"
else
echo "Restic backup failed!"
exit 1
fi
echo "Backup finished."

42
images/backup/cleanup.sh Normal file
View File

@@ -0,0 +1,42 @@
#!/bin/sh
set -e
if [ -z "$RESTIC_PASSWORD" ]; then
echo "Error: RESTIC_PASSWORD environment variable is not set." >&2
exit 1
fi
RESTIC_REPOSITORY="/mnt/backup"
echo "Starting Restic cleanup for repository $RESTIC_REPOSITORY"
echo "Checking Restic repository existence..."
restic snapshots --repository "$RESTIC_REPOSITORY"
# Restic forget and prune strategy
# --keep-daily 7: Keep 7 most recent daily backups
# --keep-weekly 4: Keep 4 most recent weekly backups
# --keep-monthly 6: Keep 6 most recent monthly backups
# --keep-yearly 1: Keep 1 most recent yearly backup
# --prune: Actually delete data that's no longer referenced
# --group-by host,paths: Group snapshots for retention by host and path.
echo "Running Restic forget and prune..."
restic forget \
--group-by host,paths \
--tag "daily" \
--keep-daily 7 \
--keep-weekly 4 \
--keep-monthly 6 \
--keep-yearly 1 \
--prune \
--verbose \
--repository "$RESTIC_REPOSITORY"
if [ $? -eq 0 ]; then
echo "Restic cleanup completed successfully!"
else
echo "Restic cleanup failed!"
exit 1
fi
echo "Cleanup finished."