Add backup service to minecraft-server

This commit is contained in:
Victor Mignot 2024-10-29 13:22:54 +01:00
parent c9b9cf00b1
commit b190b00a39
Signed by: dala
GPG key ID: 5E7F2CE1BEAFED3D

View file

@ -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";
};
};
}