{ 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; }; }; }