From 33a73074b5975db4da31e43d20cb2e6394d04813 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonardo=20Eug=C3=AAnio?= Date: Sun, 22 Feb 2026 12:37:21 -0300 Subject: [PATCH] factorio: improve backup logic --- hosts/monolith/factorio-server.nix | 64 +++++++++++++++++++----------- 1 file changed, 40 insertions(+), 24 deletions(-) diff --git a/hosts/monolith/factorio-server.nix b/hosts/monolith/factorio-server.nix index 0db1e3a..d7e84f1 100644 --- a/hosts/monolith/factorio-server.nix +++ b/hosts/monolith/factorio-server.nix @@ -1,10 +1,49 @@ { config, pkgs, - lib, ... }: +let + mkBackup = time: { + systemd.services."factorio-backup-save-${time}" = { + description = "Backup factorio saves"; + script = '' + set -exuo pipefail + + FILENAME="space-age-$(date --iso=seconds | tr ':' '_').zip" + DEST_DIR=~lelgenio/Documentos/GameSaves/factorio_saves/space-age-1/${time} + + mkdir -p "$DEST_DIR" + cp /var/lib/factorio/saves/default.zip "$DEST_DIR"/$FILENAME + chown lelgenio "$DEST_DIR" "$DEST_DIR"/$FILENAME + + # list all files, from oldest to newest + # remove the last 10 from the list + # delete the rest + cd "$DEST_DIR" + ls | head -n-10 | xargs -r rm -v + ''; + serviceConfig.Type = "oneshot"; + }; + + systemd.timers."factorio-backup-save-${time}" = { + timerConfig = { + # Systemd accepts descriptive names such as "daily" + # The times are at midnight, Persistent makes sure that the backups get executed + OnCalendar = time; + Persistent = true; + Unit = "factorio-backup-save-${time}.service"; + }; + wantedBy = [ "timers.target" ]; + }; + }; +in { + imports = [ + (mkBackup "daily") + (mkBackup "monthly") + ]; + services.factorio = { enable = true; package = pkgs.my-factorio-headless; @@ -20,29 +59,6 @@ wants = [ "network-online.target" ]; }; - systemd.services.factorio-backup-save = { - description = "Backup factorio saves"; - script = '' - FILENAME="space-age-$(date --iso=seconds | tr ':' '_').zip" - ${lib.getExe pkgs.rsync} \ - -av \ - --chown=lelgenio \ - /var/lib/factorio/saves/default.zip \ - ~lelgenio/Documentos/GameSaves/factorio_saves/$FILENAME - ''; - serviceConfig.Type = "oneshot"; - wantedBy = [ "multi-user.target" ]; - }; - - systemd.timers.factorio-backup-save = { - timerConfig = { - OnCalendar = "*-*-* 18:00:00"; - Persistent = true; - Unit = "factorio-backup-save.service"; - }; - wantedBy = [ "timers.target" ]; - }; - sops.secrets."factorio/server-config.json" = { mode = "777"; };