restructure backups

This commit is contained in:
2025-10-18 13:51:23 -05:00
parent f597616843
commit 643ab0958c
22 changed files with 385 additions and 262 deletions

View File

@@ -1,17 +1,32 @@
{ pkgs, config, lib, ... }:
let
{
pkgs,
config,
lib,
...
}: let
service = "minecraft_recpro";
cfg = config.gameservers.${service};
sec = config.sops.secrets;
servers = {
velocity = { data_dir = "/var/lib/gameservers/minecraft_recpro/velocity"; ram = "2G"; };
smp = { data_dir = "/var/lib/gameservers/minecraft_recpro/smp"; ram = "12G"; };
superflat = { data_dir = "/var/lib/gameservers/minecraft_recpro/superflat"; ram = "4G"; };
bento = { data_dir = "/var/lib/gameservers/minecraft_recpro/bento"; ram = "2G"; };
velocity = {
data_dir = "/var/lib/gameservers/minecraft_recpro/velocity";
db_dumb_dir = "/var/backup/mysql/${service}_db.zst";
ram = "2G";
};
smp = {
data_dir = "/var/lib/gameservers/minecraft_recpro/smp";
ram = "12G";
};
superflat = {
data_dir = "/var/lib/gameservers/minecraft_recpro/superflat";
ram = "4G";
};
bento = {
data_dir = "/var/lib/gameservers/minecraft_recpro/bento";
ram = "2G";
};
};
in
{
in {
options.gameservers.${service} = {
enable = lib.mkEnableOption "enables ${service}";
url = lib.mkOption {
@@ -38,12 +53,16 @@ in
type = lib.types.nullOr lib.types.str;
default = "velocity";
};
backup_repo = lib.mkOption {
type = lib.types.path;
default = "/holocron/archives/gameservers/minecraft/recpro_stack";
description = "path to take hourly backups to with borg!";
};
};
config = lib.mkIf cfg.enable {
# declare ${service} group
users.groups.minecraft = { gid = lib.mkForce cfg.ids; };
users.groups.minecraft = {gid = lib.mkForce cfg.ids;};
# declare ${service} user
users.users.minecraft = {
@@ -55,37 +74,41 @@ in
extraGroups = [];
};
systemd.tmpfiles.rules = lib.attrsets.mapAttrsToList (name: cfg:
"d ${cfg.data_dir} 0770 minecraft minecraft -"
) servers;
systemd.tmpfiles.rules =
lib.attrsets.mapAttrsToList (
name: cfg: "d ${cfg.data_dir} 0770 minecraft minecraft -"
)
servers;
# Create a systemd service per server running in tmux
systemd.services = lib.attrsets.mapAttrs (name: srv: {
description = "minecraft_recpro: ${name}";
after = [ "network.target" ];
wants = [ "network.target" ];
serviceConfig = {
User = "minecraft";
Group = "minecraft";
WorkingDirectory = srv.data_dir;
UMask = "0007";
ExecStart = "${pkgs.openjdk21}/bin/java -Xmx${srv.ram} -jar server.jar nogui";
Restart = "on-failure";
KillMode = "process";
};
wantedBy = [ "multi-user.target" ];
}) servers;
systemd.services =
lib.attrsets.mapAttrs (name: srv: {
description = "minecraft_recpro: ${name}";
after = ["network.target"];
wants = ["network.target"];
serviceConfig = {
User = "minecraft";
Group = "minecraft";
WorkingDirectory = srv.data_dir;
UMask = "0007";
ExecStart = "${pkgs.openjdk21}/bin/java -Xmx${srv.ram} -jar server.jar nogui";
Restart = "on-failure";
KillMode = "process";
};
wantedBy = ["multi-user.target"];
})
servers;
environment.systemPackages = with pkgs; [ openjdk21 mcrcon ];
environment.systemPackages = with pkgs; [openjdk21 mcrcon];
services.mysql = {
enable = true;
package = pkgs.mariadb;
ensureDatabases = [ "minecraft_recpro_db" ];
ensureDatabases = ["minecraft_recpro_db"];
ensureUsers = [
{
name = "minecraft";
ensurePermissions = { "minecraft_recpro_db.*" = "ALL PRIVILEGES"; };
ensurePermissions = {"minecraft_recpro_db.*" = "ALL PRIVILEGES";};
}
];
initialScript = pkgs.writeText "minecraft_recpro-init.sql" ''
@@ -94,9 +117,9 @@ in
FLUSH PRIVILEGES;
'';
};
# open firewall
networking.firewall.allowedTCPPorts = [ 25777 25565 25566 25567 ];
networking.firewall.allowedTCPPorts = [25777 25565 25566 25567];
sops.secrets = {
"velocity_forwarding" = {
@@ -109,17 +132,29 @@ in
owner = "mysql";
group = "mysql";
};
};
# add to backups
system.backups.gameserver_baks = lib.listToAttrs (
lib.mapAttrsToList (srv_name: cfg:
{
name = srv_name; # attribute key
value = { paths = [ cfg.data_dir "/var/backup/mysql/${service}_db.zst" ]; }; # attribute value
}
) servers
);
# backups minecraft_recpro with borg!
services.borgbackup.jobs.${service} =
lib.mapAttrs (name: srv: {
archiveBaseName = "${name}";
repo = cfg.backup_repo;
paths = [srv.data_dir] ++ lib.optionals (srv ? db_dump_dir) [srv.db_path];
compression = "auto,zstd";
startAt = "*-*-* *:00:00";
group = "archives";
encryption.mode = "repokey-blake2";
encryption.passCommand = "cat ${config.sops.secrets."borg_passwd".path}";
extraArgs = ["--verbose" "--show-rc" "--umask" "0007"];
extraCreateArgs = ["--list" "--stats" "--filter" "AME"];
prune.keep = {
within = "1d"; # Keep all archives from the last day
hourly = 24;
daily = 7;
weekly = 12;
monthly = -1; # Keep at least one archive for each month
};
})
servers;
};
}