back to scripts

docker-backup.sh

bash 45 lines secrets redacted

Archives every stack's compose + .env files to the NAS with retention — the "rebuild from scratch" safety net.

Note Live script from my home-lab server. Tokens, IDs, phone numbers and other secrets have been replaced with placeholders like <WHATSAPP_GROUP_ID> — everything else is the real, running code.
#!/bin/bash
# Docker config backup — archives only compose files and .env to NAS
set -euo pipefail

BACKUP_SRC="/home/lanky/docker"
NAS_DEST="/mnt/nas/Backups/lankyserver"
TIMESTAMP=$(date +%Y%m%d-%H%M%S)
ARCHIVE_NAME="docker-config-${TIMESTAMP}.tar.gz"
ARCHIVE_PATH="${NAS_DEST}/${ARCHIVE_NAME}"
LATEST_COPY="${NAS_DEST}/docker-config-latest.tar.gz"
LOG="/home/lanky/reports/backup-script.log"
KEEP_DAYS=30
TMPLIST="/tmp/docker-backup-files-$$.txt"

echo "[${TIMESTAMP}] Starting Docker config backup" | tee -a "$LOG"

# Verify NAS is mounted
if ! mountpoint -q /mnt/nas; then
    echo "[${TIMESTAMP}] ERROR: /mnt/nas is not mounted" | tee -a "$LOG"
    exit 1
fi

mkdir -p "$NAS_DEST"

# Build file list: only compose and .env files
find "$BACKUP_SRC" \( -name 'docker-compose.yml' -o -name 'docker-compose.yaml' -o -name '.env' \) > "$TMPLIST"
FILE_COUNT=$(wc -l < "$TMPLIST")
echo "[${TIMESTAMP}] Found ${FILE_COUNT} config files to archive" | tee -a "$LOG"

# Create archive from explicit file list
tar -czf "$ARCHIVE_PATH" -T "$TMPLIST" 2>> "$LOG"
rm -f "$TMPLIST"

SIZE=$(du -sh "$ARCHIVE_PATH" | cut -f1)
echo "[${TIMESTAMP}] Archive created: $ARCHIVE_NAME ($SIZE)" | tee -a "$LOG"

# Copy as 'latest' (NAS SMB shares don't support symlinks)
cp -f "$ARCHIVE_PATH" "$LATEST_COPY"
echo "[${TIMESTAMP}] Updated docker-config-latest.tar.gz" | tee -a "$LOG"

# Prune old backups beyond KEEP_DAYS (keep 'latest' always)
find "$NAS_DEST" -name 'docker-config-2*.tar.gz' -mtime +${KEEP_DAYS} -delete
echo "[${TIMESTAMP}] Pruned archives older than ${KEEP_DAYS} days" | tee -a "$LOG"

echo "[${TIMESTAMP}] Backup complete: $ARCHIVE_NAME ($SIZE)" | tee -a "$LOG"

back to scripts