hetzner-cloud-1/modules/backup.nix
Markus Zoppelt 864c396d68 fix: stop backups and archive cache from filling the disk
The root disk hit 100%, so Forgejo could no longer write to its DB and
murmur failed its -supw pre-start step after the last deploy.

Nightly full-copy backups (30-day retention) included data/repo-archive,
a download cache inflated to 7G by crawlers.

- backup: exclude repo-archive; hardlink unchanged files against the
  previous snapshot via --link-dest; write SQLite DBs fresh with
  sqlite3 .backup instead of into hardlinked files
- forgejo: clean the archive cache hourly and on start
- nginx: serve robots.txt disallowing archive/commit/blame/compare paths
- journald: cap at SystemMaxUse=1G

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 00:33:19 +01:00

78 lines
2.2 KiB
Nix

{
config,
pkgs,
lib,
...
}:
let
backupTime = "03:00";
retentionDays = 30;
in
{
systemd.services.forgejo-backup = {
description = "Create daily Forgejo backup snapshot";
serviceConfig = {
Type = "oneshot";
User = "root";
};
path = [
pkgs.sqlite
pkgs.rsync
];
script = ''
set -euo pipefail
TIMESTAMP=$(date -u +%Y-%m-%dT%H-%M-%S)
DEST="/root/backups/$TIMESTAMP"
PREV=$(find /root/backups -mindepth 1 -maxdepth 1 -type d | sort | tail -n 1)
mkdir -p "$DEST"
# Unchanged files are hardlinked against the previous snapshot, so each
# day only costs the delta. SQLite DBs are excluded from rsync and written
# fresh by sqlite3 below: writing into a hardlinked file would also
# modify the older snapshot that shares the inode.
# repo-archive is a regenerable download cache and must stay out.
rsync -a \
''${PREV:+--link-dest="$PREV"} \
--exclude='data/forgejo.db' \
--exclude='data/forgejo.db-shm' \
--exclude='data/forgejo.db-wal' \
--exclude='data/repo-archive' \
--exclude='data/tmp' \
--exclude='data/queues' \
--exclude='log' \
--exclude='dump' \
/var/lib/forgejo/ "$DEST/"
sqlite3 /var/lib/forgejo/data/forgejo.db \
".backup '$DEST/data/forgejo.db'"
if [ -d /var/lib/murmur ]; then
mkdir -p "$DEST/murmur"
rsync -a \
''${PREV:+--link-dest="$PREV/murmur"} \
--exclude='murmur.sqlite' \
/var/lib/murmur/ "$DEST/murmur/"
if [ -f /var/lib/murmur/murmur.sqlite ]; then
sqlite3 /var/lib/murmur/murmur.sqlite \
".backup '$DEST/murmur/murmur.sqlite'"
fi
fi
echo "Snapshot created: $DEST"
# Remove snapshots older than retentionDays
find /root/backups -mindepth 1 -maxdepth 1 -type d -mtime +${toString retentionDays} -exec rm -rf {} +
'';
};
systemd.timers.forgejo-backup = {
description = "Daily Forgejo backup snapshot timer";
wantedBy = [ "timers.target" ];
timerConfig = {
OnCalendar = backupTime;
Persistent = true;
};
};
}