factorio: improve backup logic

This commit is contained in:
Leonardo Eugênio 2026-02-22 12:37:21 -03:00
parent 5ccec30fae
commit 33a73074b5

View file

@ -1,10 +1,49 @@
{ {
config, config,
pkgs, 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 = { services.factorio = {
enable = true; enable = true;
package = pkgs.my-factorio-headless; package = pkgs.my-factorio-headless;
@ -20,29 +59,6 @@
wants = [ "network-online.target" ]; 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" = { sops.secrets."factorio/server-config.json" = {
mode = "777"; mode = "777";
}; };