From b190b00a39393f4cd5d95c24ba56289a9246cfb0 Mon Sep 17 00:00:00 2001 From: Victor Mignot Date: Tue, 29 Oct 2024 13:22:54 +0100 Subject: [PATCH] Add backup service to minecraft-server --- modules/server/minecraft.nix | 39 ++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/modules/server/minecraft.nix b/modules/server/minecraft.nix index 6df2f86..601b34b 100644 --- a/modules/server/minecraft.nix +++ b/modules/server/minecraft.nix @@ -5,6 +5,9 @@ ... }: with lib; +let + serverStdinPath = config.systemd.sockets.minecraft-server.socketConfig.ListenFIFO; +in { options.my.server.papermc.enable = mkOption { type = types.bool; @@ -17,6 +20,42 @@ with lib; package = pkgs.papermc; dataDir = "/srv/minecraft"; openFirewall = true; + eula = true; + }; + + systemd.services."minecraft-server-backup" = { + path = with pkgs; [ + gnutar + gzip + ]; + + script = '' + echo 'say Une backup du serveur va commencer dans 1 minute.' > ${serverStdinPath} + sleep 1m + + cat << EOF > ${serverStdinPath} + say Sauvegarde en cours... + save-off + save-all + EOF + + tar -cpzf /srv/minecraft-backups/backup-$(date -u +%Y%m%d_%H%M%S).tar.gz ${config.services.minecraft-server.dataDir} + + cat << EOF > ${serverStdinPath} + save-on + say Sauvegarde terminée ! + EOF + + # Deleting saves older than 2 weeks + find /srv/minecraft-backups/ -type f -mtime +14 -name 'backup-*.tar.gz' -delete + ''; + + serviceConfig = { + Type = "oneshot"; + User = "root"; + }; + + startAt = "*-*-* 00:00,12:00"; }; }; }