backups for mine
This commit is contained in:
@@ -124,3 +124,52 @@ Archives contain paths relative to that directory. Restore file ownership to the
|
|||||||
game service's user (`minecraft` for Java servers, `horizon` for Horizon),
|
game service's user (`minecraft` for Java servers, `horizon` for Horizon),
|
||||||
reapply the matching Nix configuration and runtime secrets, then start the game
|
reapply the matching Nix configuration and runtime secrets, then start the game
|
||||||
and its backup timer.
|
and its backup timer.
|
||||||
|
|
||||||
|
## Isolated wowMineMap restore test
|
||||||
|
|
||||||
|
Neuro declares `wowMineMapRestore` as a separate, manually started Minecraft
|
||||||
|
instance. It reuses the production NeoForge package, pinned WorldOfSosal
|
||||||
|
modpack import, and shared modpack adjustments. It uses an 8 GB heap and reduced
|
||||||
|
view/simulation distances for the test. Its directory is
|
||||||
|
`/srv/minecraft/wowMineMapRestore`; its game socket is `127.0.0.1:25568`.
|
||||||
|
RCON, public firewall access, and scheduled backups of this copy are disabled.
|
||||||
|
It does not use the production source-map importer or reset service.
|
||||||
|
|
||||||
|
After deploying the neuro NixOS configuration normally, start the copy:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
ssh neuro 'systemctl start minecraft-server-wowMineMapRestore.service'
|
||||||
|
ssh neuro 'journalctl -u minecraft-modpack-import-wowMineMapRestore -u minecraft-backup-restore-wowMineMapRestore -u minecraft-server-wowMineMapRestore -n 100 --no-pager'
|
||||||
|
ssh neuro 'tail -n 80 /srv/minecraft/wowMineMapRestore/logs/latest.log'
|
||||||
|
```
|
||||||
|
|
||||||
|
The first start installs the matching modpack, then downloads this exact object:
|
||||||
|
|
||||||
|
```text
|
||||||
|
s3://backup-hectic-lab/minecraft/neuro/wowMineMap/minecraft-wowMineMap-20261117T172531Z.tar.zst
|
||||||
|
```
|
||||||
|
|
||||||
|
The restore uses the existing root-only S3 credentials file. The key needs
|
||||||
|
`GetObject` access in addition to the backup permissions above. It checks the
|
||||||
|
compressed size (9,206,330,929 bytes), zstd integrity, archive paths, and the
|
||||||
|
configured world's `level.dat`. Backup data, including player and mod state,
|
||||||
|
overrides bootstrap configuration; regenerated mods remain installed.
|
||||||
|
It refuses to replace an existing world. A completion marker binds the
|
||||||
|
restored directory to this S3 object, and subsequent starts skip both bootstrap
|
||||||
|
and restoration so that restored configuration is preserved.
|
||||||
|
|
||||||
|
Connect using the matching WorldOfSosal client through an SSH tunnel:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
ssh -N -L 25568:127.0.0.1:25568 neuro
|
||||||
|
```
|
||||||
|
|
||||||
|
Use `localhost:25568` in Minecraft. Confirm startup reaches `Done`, enter the
|
||||||
|
restored Azeroth dimension, and inspect known builds, player inventory, and mod
|
||||||
|
objects. A running systemd unit alone does not prove that the saved world
|
||||||
|
loaded correctly. These real-world checks remain to be performed after deploy.
|
||||||
|
Stop the test when finished:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
ssh neuro 'systemctl stop minecraft-server-wowMineMapRestore.service'
|
||||||
|
```
|
||||||
|
|||||||
@@ -0,0 +1,203 @@
|
|||||||
|
{ config, lib, pkgs, ... }:
|
||||||
|
let
|
||||||
|
minecraft = config.services.minecraft-servers;
|
||||||
|
restores = lib.filterAttrs (_: restore: restore.enable) config.services.minecraft-backup-restores;
|
||||||
|
marker = ".minecraft-backup-restore-complete";
|
||||||
|
# Python's data filter resolves symlink chains against the extraction tree and
|
||||||
|
# rejects escaping links and special files. Also reject absolute/traversal
|
||||||
|
# names outright (data_filter alone strips leading slashes).
|
||||||
|
extract = pkgs.writeText "minecraft-restore-extract.py" ''
|
||||||
|
import pathlib
|
||||||
|
import sys
|
||||||
|
import tarfile
|
||||||
|
|
||||||
|
def safe_member(member: tarfile.TarInfo, destination: str) -> tarfile.TarInfo | None:
|
||||||
|
path = pathlib.PurePosixPath(member.name)
|
||||||
|
if path.is_absolute() or ".." in path.parts or path.parts == ("${marker}",):
|
||||||
|
raise tarfile.FilterError(f"Unsafe archive entry: {member.name}")
|
||||||
|
# nix-minecraft regenerates this as an absolute store symlink. Keep
|
||||||
|
# bootstrap's EULA rather than restoring a possibly obsolete store path.
|
||||||
|
if path.parts == ("eula.txt",):
|
||||||
|
return None
|
||||||
|
return tarfile.data_filter(member, destination)
|
||||||
|
|
||||||
|
with tarfile.open(sys.argv[1], mode="r:") as archive:
|
||||||
|
archive.extractall(sys.argv[2], filter=safe_member)
|
||||||
|
'';
|
||||||
|
fresh = pkgs.writeText "minecraft-restore-fresh.py" ''
|
||||||
|
import os
|
||||||
|
import pathlib
|
||||||
|
import sys
|
||||||
|
|
||||||
|
target = pathlib.Path(sys.argv[1])
|
||||||
|
level_name = sys.argv[2]
|
||||||
|
if os.path.lexists(target / level_name):
|
||||||
|
sys.exit(f"Refusing existing Minecraft level directory: {target / level_name}")
|
||||||
|
# Refuse even incomplete saves, including custom level-name directories
|
||||||
|
# and save signatures nested beneath mod-specific dimension paths.
|
||||||
|
for directory, directories, files in os.walk(target, followlinks=False):
|
||||||
|
for name in directories + files:
|
||||||
|
path = pathlib.Path(directory) / name
|
||||||
|
if (
|
||||||
|
name in {"level.dat", "level.dat_old", "session.lock", "region",
|
||||||
|
"entities", "poi", "playerdata", "advancements", "stats",
|
||||||
|
"dimensions", "DIM-1", "DIM1"}
|
||||||
|
or name.endswith((".mca", ".mcr"))
|
||||||
|
or (path.parent == target and (
|
||||||
|
name.startswith("world") or name == level_name
|
||||||
|
))
|
||||||
|
):
|
||||||
|
sys.exit(f"Refusing existing Minecraft world state: {path}")
|
||||||
|
'';
|
||||||
|
script = name: restore:
|
||||||
|
let
|
||||||
|
target = "${minecraft.dataDir}/${restore.serverName}";
|
||||||
|
levelName = toString (minecraft.servers.${restore.serverName}.serverProperties.level-name or "world");
|
||||||
|
identity = builtins.toJSON { inherit name; inherit (restore) bucket key endpoint; };
|
||||||
|
in pkgs.writeShellScript "minecraft-backup-restore-${name}" ''
|
||||||
|
set -euo pipefail
|
||||||
|
umask 077
|
||||||
|
target=${lib.escapeShellArg target}
|
||||||
|
${pkgs.coreutils}/bin/mkdir -p ${lib.escapeShellArg minecraft.dataDir}
|
||||||
|
exec 9>${lib.escapeShellArg "${minecraft.dataDir}/.minecraft-restore-${restore.serverName}.lock"}
|
||||||
|
${pkgs.util-linux}/bin/flock 9
|
||||||
|
if [ -L "$target" ]; then
|
||||||
|
echo "Refusing symlink restore target: $target" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
if [ -e "$target/${marker}" ] || [ -L "$target/${marker}" ]; then
|
||||||
|
if [ -f "$target/${marker}" ] && [ ! -L "$target/${marker}" ] &&
|
||||||
|
[ "$(${pkgs.coreutils}/bin/cat "$target/${marker}")" = ${lib.escapeShellArg identity} ]; then
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
echo "Restore marker does not match the configured S3 backup: $target/${marker}" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
${pkgs.python3}/bin/python ${fresh} "$target" ${lib.escapeShellArg levelName}
|
||||||
|
work="$(${pkgs.coreutils}/bin/mktemp -d ${lib.escapeShellArg "${minecraft.dataDir}/.minecraft-restore-${restore.serverName}.XXXXXX"})"
|
||||||
|
trap '${pkgs.coreutils}/bin/rm -rf -- "$work"' EXIT
|
||||||
|
trap 'exit 1' HUP INT TERM
|
||||||
|
${pkgs.coreutils}/bin/mkdir "$work/server"
|
||||||
|
if [ -e "$target" ]; then
|
||||||
|
test -d "$target"
|
||||||
|
${pkgs.coreutils}/bin/cp -a -- "$target/." "$work/server/"
|
||||||
|
fi
|
||||||
|
: "''${AWS_ACCESS_KEY_ID:?AWS_ACCESS_KEY_ID is required}"
|
||||||
|
: "''${AWS_SECRET_ACCESS_KEY:?AWS_SECRET_ACCESS_KEY is required}"
|
||||||
|
${pkgs.awscli2}/bin/aws s3 cp \
|
||||||
|
${lib.escapeShellArg "s3://${restore.bucket}/${restore.key}"} "$work/backup.tar.zst" \
|
||||||
|
--endpoint-url ${lib.escapeShellArg restore.endpoint} \
|
||||||
|
--region ${lib.escapeShellArg restore.region} \
|
||||||
|
--cli-connect-timeout 30 --cli-read-timeout 300 --only-show-errors
|
||||||
|
${lib.optionalString (restore.expectedSize != null) ''
|
||||||
|
test "$(${pkgs.coreutils}/bin/stat -c %s "$work/backup.tar.zst")" = ${toString restore.expectedSize}
|
||||||
|
''}
|
||||||
|
${pkgs.zstd}/bin/zstd --test --quiet "$work/backup.tar.zst"
|
||||||
|
${pkgs.zstd}/bin/zstd --decompress --stdout "$work/backup.tar.zst" > "$work/backup.tar"
|
||||||
|
${pkgs.python3}/bin/python ${extract} "$work/backup.tar" "$work/server"
|
||||||
|
# Require the configured save, not an unrelated nested level.dat that
|
||||||
|
# would let Minecraft silently generate a fresh world after this restore.
|
||||||
|
test -f "$work/server"/${lib.escapeShellArg "${levelName}/level.dat"}
|
||||||
|
printf '%s\n' ${lib.escapeShellArg identity} > "$work/server/${marker}"
|
||||||
|
${pkgs.coreutils}/bin/chown -hR minecraft:minecraft "$work/server"
|
||||||
|
# The marker moves with the tree in the same atomic operation. When a
|
||||||
|
# bootstrap exists, exchange leaves it in work/server for trap cleanup;
|
||||||
|
# an exchange failure leaves the original directory untouched.
|
||||||
|
if [ -e "$target" ]; then
|
||||||
|
${pkgs.coreutils}/bin/mv --exchange --no-copy -T "$work/server" "$target"
|
||||||
|
else
|
||||||
|
${pkgs.coreutils}/bin/mv --no-copy -T "$work/server" "$target"
|
||||||
|
fi
|
||||||
|
'';
|
||||||
|
in {
|
||||||
|
options.services.minecraft-backup-restores = lib.mkOption {
|
||||||
|
default = {};
|
||||||
|
description = "One-time full S3 backup restores, optionally after modpack bootstrap. The regenerated root eula.txt is retained from bootstrap.";
|
||||||
|
type = lib.types.attrsOf (lib.types.submodule ({ name, ... }: {
|
||||||
|
options = {
|
||||||
|
enable = lib.mkEnableOption "one-time Minecraft backup restore";
|
||||||
|
serverName = lib.mkOption { type = lib.types.str; default = name; };
|
||||||
|
completionMarker = lib.mkOption {
|
||||||
|
type = lib.types.str;
|
||||||
|
readOnly = true;
|
||||||
|
default = "${minecraft.dataDir}/${config.services.minecraft-backup-restores.${name}.serverName}/${marker}";
|
||||||
|
description = "Persistent marker path; bootstrap units should use ConditionPathExists=!<this path> to preserve restored configuration on subsequent boots.";
|
||||||
|
};
|
||||||
|
bucket = lib.mkOption { type = lib.types.str; };
|
||||||
|
key = lib.mkOption {
|
||||||
|
type = lib.types.str;
|
||||||
|
description = "Exact S3 object key ending in .tar.zst.";
|
||||||
|
};
|
||||||
|
endpoint = lib.mkOption { type = lib.types.str; };
|
||||||
|
region = lib.mkOption { type = lib.types.str; };
|
||||||
|
credentialsFile = lib.mkOption {
|
||||||
|
type = lib.types.str;
|
||||||
|
description = "Runtime EnvironmentFile outside /nix/store containing AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY.";
|
||||||
|
};
|
||||||
|
expectedSize = lib.mkOption {
|
||||||
|
type = lib.types.nullOr lib.types.ints.positive;
|
||||||
|
default = null;
|
||||||
|
description = "Expected compressed object size in bytes.";
|
||||||
|
};
|
||||||
|
timeout = lib.mkOption { type = lib.types.str; default = "2h"; };
|
||||||
|
afterUnits = lib.mkOption {
|
||||||
|
type = lib.types.listOf lib.types.str;
|
||||||
|
default = [];
|
||||||
|
description = "Required units that finish before restore, e.g. modpack bootstrap. Backup files override bootstrap files; bootstrap-only mods remain.";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
|
||||||
|
config = lib.mkIf (restores != {}) {
|
||||||
|
assertions = [
|
||||||
|
{
|
||||||
|
assertion = lib.hasPrefix "/" minecraft.dataDir && !lib.hasPrefix "/nix/store/" minecraft.dataDir;
|
||||||
|
message = "Minecraft restores require an absolute runtime dataDir.";
|
||||||
|
}
|
||||||
|
{
|
||||||
|
assertion = lib.length (lib.unique (map (restore: restore.serverName) (lib.attrValues restores))) == lib.length (lib.attrNames restores);
|
||||||
|
message = "Minecraft restores must have unique server targets.";
|
||||||
|
}
|
||||||
|
] ++ lib.concatLists (lib.mapAttrsToList (name: restore: [
|
||||||
|
{
|
||||||
|
assertion = builtins.match "[a-zA-Z0-9_-]+" name != null
|
||||||
|
&& builtins.match "[a-zA-Z0-9_-]+" restore.serverName != null
|
||||||
|
&& minecraft.enable
|
||||||
|
&& (minecraft.servers.${restore.serverName}.enable or false);
|
||||||
|
message = "Minecraft restore ${name} must target an enabled Minecraft server and use safe names.";
|
||||||
|
}
|
||||||
|
{
|
||||||
|
assertion = lib.hasPrefix "/" restore.credentialsFile
|
||||||
|
&& !lib.hasPrefix "/nix/store/" restore.credentialsFile;
|
||||||
|
message = "Minecraft restore ${name} credentials must be a runtime file outside /nix/store.";
|
||||||
|
}
|
||||||
|
{
|
||||||
|
assertion = lib.hasPrefix "https://" restore.endpoint && lib.hasSuffix ".tar.zst" restore.key;
|
||||||
|
message = "Minecraft restore ${name} requires HTTPS and an exact .tar.zst object key.";
|
||||||
|
}
|
||||||
|
]) restores);
|
||||||
|
|
||||||
|
systemd.services = lib.mkMerge (lib.mapAttrsToList (name: restore: {
|
||||||
|
"minecraft-backup-restore-${name}" = {
|
||||||
|
description = "One-time full backup restore of Minecraft ${restore.serverName}";
|
||||||
|
wants = [ "network-online.target" ];
|
||||||
|
after = [ "network-online.target" ] ++ restore.afterUnits;
|
||||||
|
requires = restore.afterUnits;
|
||||||
|
before = [ "minecraft-server-${restore.serverName}.service" ];
|
||||||
|
serviceConfig = {
|
||||||
|
Type = "oneshot";
|
||||||
|
User = "root";
|
||||||
|
UMask = "0077";
|
||||||
|
EnvironmentFile = restore.credentialsFile;
|
||||||
|
ExecStart = script name restore;
|
||||||
|
TimeoutStartSec = restore.timeout;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
"minecraft-server-${restore.serverName}" = {
|
||||||
|
requires = [ "minecraft-backup-restore-${name}.service" ];
|
||||||
|
after = [ "minecraft-backup-restore-${name}.service" ];
|
||||||
|
};
|
||||||
|
}) restores);
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
{ config, pkgs, ... }:
|
||||||
|
let
|
||||||
|
name = "wowMineMapRestore";
|
||||||
|
production = config.services.minecraft-servers.servers.wowMineMap;
|
||||||
|
pack = config.services.minecraft-modpack-imports.worldOfSosal;
|
||||||
|
restore = config.services.minecraft-backup-restores.${name};
|
||||||
|
serverDir = "${config.services.minecraft-servers.dataDir}/${name}";
|
||||||
|
in {
|
||||||
|
imports = [ ../../../module/generic/minecraft-restore.nix ];
|
||||||
|
|
||||||
|
sops.secrets."minecraft/restore-storage-box-pack-key" = {
|
||||||
|
sopsFile = config.sops.secrets."minecraft/storage-box-pack-key".sopsFile;
|
||||||
|
key = "minecraft/storage-box-pack-key";
|
||||||
|
owner = "mc-pack-${name}";
|
||||||
|
group = "mc-pack-${name}";
|
||||||
|
mode = "0400";
|
||||||
|
};
|
||||||
|
|
||||||
|
services.minecraft-servers.servers.${name} = {
|
||||||
|
enable = true;
|
||||||
|
autoStart = false;
|
||||||
|
openFirewall = false;
|
||||||
|
package = production.package;
|
||||||
|
jvmOpts = "-Xmx8G -Xms2G";
|
||||||
|
serverProperties = production.serverProperties // {
|
||||||
|
server-ip = "127.0.0.1";
|
||||||
|
server-port = 25568;
|
||||||
|
enable-rcon = false;
|
||||||
|
enable-query = false;
|
||||||
|
view-distance = 8;
|
||||||
|
simulation-distance = 4;
|
||||||
|
motd = "WorldOfSosal backup restore test";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
services.minecraft-modpack-imports.${name} = {
|
||||||
|
inherit (pack) remoteHost remoteUser remotePath archiveName archiveSha256
|
||||||
|
expectedDependencies hostPublicKey retries timeout;
|
||||||
|
enable = true;
|
||||||
|
serverName = name;
|
||||||
|
sshKeyFile = config.sops.secrets."minecraft/restore-storage-box-pack-key".path;
|
||||||
|
};
|
||||||
|
|
||||||
|
systemd.services."minecraft-modpack-import-${name}" = {
|
||||||
|
unitConfig.ConditionPathExists = "!${restore.completionMarker}";
|
||||||
|
postStart = import ./world-of-sosal-settings.nix { inherit pkgs serverDir; };
|
||||||
|
};
|
||||||
|
|
||||||
|
services.minecraft-backup-restores.${name} = {
|
||||||
|
enable = true;
|
||||||
|
serverName = name;
|
||||||
|
inherit (config.services.minecraft-backups.s3)
|
||||||
|
bucket endpoint region credentialsFile;
|
||||||
|
key = "minecraft/neuro/wowMineMap/minecraft-wowMineMap-20261117T172531Z.tar.zst";
|
||||||
|
expectedSize = 9206330929;
|
||||||
|
afterUnits = [ "minecraft-modpack-import-${name}.service" ];
|
||||||
|
};
|
||||||
|
|
||||||
|
services.minecraft-backups.servers.${name}.enable = false;
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@
|
|||||||
./create-aeronautics.nix
|
./create-aeronautics.nix
|
||||||
./wow-mine-map.nix
|
./wow-mine-map.nix
|
||||||
./world-of-sosal.nix
|
./world-of-sosal.nix
|
||||||
|
./backup-restore-test.nix
|
||||||
./public-tunnel.nix
|
./public-tunnel.nix
|
||||||
./world-import.nix
|
./world-import.nix
|
||||||
./modpack-import.nix
|
./modpack-import.nix
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
{ pkgs, serverDir }:
|
||||||
|
''
|
||||||
|
rm -f -- \
|
||||||
|
"${serverDir}/mods/dynamicheight-0.2.0+neoforge-1.21-1.21.1.jar" \
|
||||||
|
"${serverDir}/mods/sable_dynamicheight_compat-1.0.0.jar"
|
||||||
|
${pkgs.gnused}/bin/sed -i \
|
||||||
|
-e '/^[[:space:]]*levelKeyPrefix[[:space:]]*=/d' \
|
||||||
|
-e '/^\[server\]$/a\levelKeyPrefix = "world-of-sosal-20261114T223938Z-winter-biomes-"' \
|
||||||
|
"${serverDir}/config/DistantHorizons.toml"
|
||||||
|
${pkgs.gnused}/bin/sed -i \
|
||||||
|
-e 's/^\([[:space:]]*enableCheatModeForCreative[[:space:]]*=\).*/\1 false/' \
|
||||||
|
"${serverDir}/config/jei-server.toml"
|
||||||
|
''
|
||||||
@@ -36,17 +36,9 @@ in
|
|||||||
after = [ "minecraft-world-import-wowMineMap.service" ];
|
after = [ "minecraft-world-import-wowMineMap.service" ];
|
||||||
requires = [ "minecraft-world-import-wowMineMap.service" ];
|
requires = [ "minecraft-world-import-wowMineMap.service" ];
|
||||||
# Test the map's native dimension height without runtime height overrides.
|
# Test the map's native dimension height without runtime height overrides.
|
||||||
postStart = ''
|
postStart = import ./world-of-sosal-settings.nix {
|
||||||
rm -f -- \
|
inherit pkgs;
|
||||||
"${config.services.minecraft-servers.dataDir}/wowMineMap/mods/dynamicheight-0.2.0+neoforge-1.21-1.21.1.jar" \
|
serverDir = "${config.services.minecraft-servers.dataDir}/wowMineMap";
|
||||||
"${config.services.minecraft-servers.dataDir}/wowMineMap/mods/sable_dynamicheight_compat-1.0.0.jar"
|
};
|
||||||
${pkgs.gnused}/bin/sed -i \
|
|
||||||
-e '/^[[:space:]]*levelKeyPrefix[[:space:]]*=/d' \
|
|
||||||
-e '/^\[server\]$/a\levelKeyPrefix = "world-of-sosal-20261114T223938Z-winter-biomes-"' \
|
|
||||||
"${config.services.minecraft-servers.dataDir}/wowMineMap/config/DistantHorizons.toml"
|
|
||||||
${pkgs.gnused}/bin/sed -i \
|
|
||||||
-e 's/^\([[:space:]]*enableCheatModeForCreative[[:space:]]*=\).*/\1 false/' \
|
|
||||||
"${config.services.minecraft-servers.dataDir}/wowMineMap/config/jei-server.toml"
|
|
||||||
'';
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,7 +38,8 @@ in (import ./affine.nix { inherit system inputs self pkgs; }) //
|
|||||||
(import ./affine-frontend.nix { inherit system inputs pkgs; }) //
|
(import ./affine-frontend.nix { inherit system inputs pkgs; }) //
|
||||||
(import ./element.nix { inherit system inputs pkgs; }) //
|
(import ./element.nix { inherit system inputs pkgs; }) //
|
||||||
(import ./horizon.nix { inherit system inputs pkgs; }) //
|
(import ./horizon.nix { inherit system inputs pkgs; }) //
|
||||||
(import ./minecraft-backups.nix { inherit system inputs pkgs; }) // {
|
(import ./minecraft-backups.nix { inherit system inputs pkgs; }) //
|
||||||
|
(import ./minecraft-restore.nix { inherit system inputs pkgs; }) // {
|
||||||
p4d-module-basic =
|
p4d-module-basic =
|
||||||
assert enabledCfg.users.users.p4d.isSystemUser;
|
assert enabledCfg.users.users.p4d.isSystemUser;
|
||||||
assert enabledCfg.users.users.p4d.group == "p4d";
|
assert enabledCfg.users.users.p4d.group == "p4d";
|
||||||
|
|||||||
@@ -0,0 +1,224 @@
|
|||||||
|
{ system, inputs, pkgs, ... }:
|
||||||
|
let
|
||||||
|
lib = inputs.nixpkgs.lib;
|
||||||
|
aws = pkgs.writeShellScriptBin "aws" ''
|
||||||
|
set -eu
|
||||||
|
test "$1 $2" = "s3 cp"
|
||||||
|
test "$3" = s3://test/backups/full.tar.zst
|
||||||
|
test "$AWS_ACCESS_KEY_ID" = test
|
||||||
|
test "$AWS_SECRET_ACCESS_KEY" = test
|
||||||
|
if [ "''${FAIL_DOWNLOAD:-0}" = 1 ]; then
|
||||||
|
printf partial > "$4"
|
||||||
|
exit 32
|
||||||
|
fi
|
||||||
|
cp /build/backup.tar.zst "$4"
|
||||||
|
'';
|
||||||
|
# Build sandboxes have no minecraft account and cannot chown to its UID.
|
||||||
|
# Keep the real filesystem operations, mapping only the production owner.
|
||||||
|
chown = pkgs.writeShellScriptBin "chown" ''
|
||||||
|
set -eu
|
||||||
|
test "$1" = -hR
|
||||||
|
test "$2" = minecraft:minecraft
|
||||||
|
exec ${pkgs.coreutils}/bin/chown -hR "$(id -u):$(id -g)" "$3"
|
||||||
|
'';
|
||||||
|
mv = pkgs.writeShellScriptBin "mv" ''
|
||||||
|
set -eu
|
||||||
|
if [ "$1" = --exchange ] && [ "''${FAIL_EXCHANGE:-0}" = 1 ]; then
|
||||||
|
exit 44
|
||||||
|
fi
|
||||||
|
exec ${pkgs.coreutils}/bin/mv "$@"
|
||||||
|
'';
|
||||||
|
coreutils = pkgs.symlinkJoin {
|
||||||
|
name = "minecraft-restore-test-coreutils";
|
||||||
|
paths = [ pkgs.coreutils ];
|
||||||
|
postBuild = ''
|
||||||
|
rm "$out/bin/chown" "$out/bin/mv"
|
||||||
|
ln -s ${chown}/bin/chown "$out/bin/chown"
|
||||||
|
ln -s ${mv}/bin/mv "$out/bin/mv"
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
mkConfig = settings: (lib.nixosSystem {
|
||||||
|
inherit system pkgs;
|
||||||
|
modules = [
|
||||||
|
inputs.nix-minecraft.nixosModules.minecraft-servers
|
||||||
|
({ config, lib, ... }: import ../../nixos/module/generic/minecraft-restore.nix {
|
||||||
|
inherit config lib;
|
||||||
|
pkgs = pkgs // { inherit coreutils; awscli2 = aws; };
|
||||||
|
})
|
||||||
|
{
|
||||||
|
system.stateVersion = "25.05";
|
||||||
|
fileSystems."/" = { device = "/dev/root"; fsType = "ext4"; };
|
||||||
|
boot.loader.grub.enable = false;
|
||||||
|
services.minecraft-servers = {
|
||||||
|
enable = true;
|
||||||
|
eula = true;
|
||||||
|
dataDir = "/build/servers";
|
||||||
|
servers.test = { enable = true; package = pkgs.hello; };
|
||||||
|
};
|
||||||
|
services.minecraft-backup-restores.test = {
|
||||||
|
enable = true;
|
||||||
|
bucket = "test";
|
||||||
|
key = "backups/full.tar.zst";
|
||||||
|
endpoint = "https://s3.invalid";
|
||||||
|
region = "test";
|
||||||
|
credentialsFile = "/run/test-credentials";
|
||||||
|
afterUnits = [ "minecraft-modpack-import-test.service" ];
|
||||||
|
} // settings;
|
||||||
|
systemd.services.minecraft-modpack-import-test = {
|
||||||
|
serviceConfig.Type = "oneshot";
|
||||||
|
script = "true";
|
||||||
|
};
|
||||||
|
}
|
||||||
|
];
|
||||||
|
}).config;
|
||||||
|
cfg = mkConfig {};
|
||||||
|
service = cfg.systemd.services.minecraft-backup-restore-test;
|
||||||
|
restore = service.serviceConfig.ExecStart;
|
||||||
|
sizeRestore = (mkConfig { expectedSize = 1; }).systemd.services.minecraft-backup-restore-test.serviceConfig.ExecStart;
|
||||||
|
differentRestore = (mkConfig { key = "backups/different.tar.zst"; }).systemd.services.minecraft-backup-restore-test.serviceConfig.ExecStart;
|
||||||
|
marker = cfg.services.minecraft-backup-restores.test.completionMarker;
|
||||||
|
badArchive = pkgs.writeText "minecraft-restore-unsafe-fixture.py" ''
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
import tarfile
|
||||||
|
|
||||||
|
with tarfile.open("/build/unsafe.tar", "w") as archive:
|
||||||
|
level = tarfile.TarInfo("./world/level.dat")
|
||||||
|
level.size = 4
|
||||||
|
archive.addfile(level, io.BytesIO(b"save"))
|
||||||
|
entry = tarfile.TarInfo(sys.argv[1])
|
||||||
|
entry.type = sys.argv[2].encode()
|
||||||
|
entry.linkname = sys.argv[3]
|
||||||
|
archive.addfile(entry)
|
||||||
|
'';
|
||||||
|
runCase = name: test: pkgs.runCommand "minecraft-restore-${name}" {
|
||||||
|
nativeBuildInputs = [ pkgs.gnutar pkgs.zstd pkgs.diffutils pkgs.python3 ];
|
||||||
|
} ''
|
||||||
|
set -eu
|
||||||
|
mkdir -p /build/source/world/playerdata /build/source/config /build/source/mods
|
||||||
|
printf saved-level > /build/source/world/level.dat
|
||||||
|
printf saved-player > /build/source/world/playerdata/player.dat
|
||||||
|
printf saved-config > /build/source/config/settings.toml
|
||||||
|
printf saved-mod > /build/source/mods/backup.jar
|
||||||
|
printf saved-properties > /build/source/server.properties
|
||||||
|
printf saved-mod-data > /build/source/mod-data.dat
|
||||||
|
ln -s world/level.dat /build/source/level-link
|
||||||
|
ln /build/source/world/level.dat /build/source/level-hardlink
|
||||||
|
ln -s /nix/store/obsolete-eula.txt /build/source/eula.txt
|
||||||
|
tar -C /build/source -cf - . | zstd -q -o /build/backup.tar.zst
|
||||||
|
mkdir -p /build/servers/test/mods /build/servers/test/config
|
||||||
|
printf bootstrap-mod > /build/servers/test/mods/bootstrap.jar
|
||||||
|
printf bootstrap-config > /build/servers/test/config/settings.toml
|
||||||
|
ln -s /nix/store/current-eula.txt /build/servers/test/eula.txt
|
||||||
|
cp -a /build/servers/test /build/bootstrap
|
||||||
|
export AWS_ACCESS_KEY_ID=test AWS_SECRET_ACCESS_KEY=test
|
||||||
|
assert_unchanged() {
|
||||||
|
test ! -e ${lib.escapeShellArg marker}
|
||||||
|
diff --no-dereference -r /build/bootstrap /build/servers/test
|
||||||
|
test -z "$(find /build/servers -maxdepth 1 -type d -name '.minecraft-restore-*' -print)"
|
||||||
|
}
|
||||||
|
${test}
|
||||||
|
touch "$out"
|
||||||
|
'';
|
||||||
|
in {
|
||||||
|
minecraft-restore-unit-contract =
|
||||||
|
assert lib.assertMsg (lib.all (assertion: assertion.assertion) cfg.assertions)
|
||||||
|
(lib.concatMapStringsSep "\n" (assertion: assertion.message)
|
||||||
|
(builtins.filter (assertion: !assertion.assertion) cfg.assertions));
|
||||||
|
assert marker == "/build/servers/test/.minecraft-backup-restore-complete";
|
||||||
|
assert service.serviceConfig.User == "root";
|
||||||
|
assert service.serviceConfig.Type == "oneshot";
|
||||||
|
assert service.serviceConfig.EnvironmentFile == "/run/test-credentials";
|
||||||
|
assert service.serviceConfig.TimeoutStartSec == "2h";
|
||||||
|
assert lib.elem "minecraft-modpack-import-test.service" service.after;
|
||||||
|
assert lib.elem "minecraft-modpack-import-test.service" service.requires;
|
||||||
|
assert lib.elem "minecraft-server-test.service" service.before;
|
||||||
|
assert lib.elem "minecraft-backup-restore-test.service" cfg.systemd.services.minecraft-server-test.requires;
|
||||||
|
assert lib.elem "minecraft-backup-restore-test.service" cfg.systemd.services.minecraft-server-test.after;
|
||||||
|
pkgs.runCommand "minecraft-restore-unit-contract" {} ''touch "$out"'';
|
||||||
|
|
||||||
|
minecraft-restore-full-bootstrap-and-idempotence = runCase "full-bootstrap-and-idempotence" ''
|
||||||
|
${restore}
|
||||||
|
cmp /build/source/world/level.dat /build/servers/test/world/level.dat
|
||||||
|
cmp /build/source/world/playerdata/player.dat /build/servers/test/world/playerdata/player.dat
|
||||||
|
cmp /build/source/config/settings.toml /build/servers/test/config/settings.toml
|
||||||
|
cmp /build/source/server.properties /build/servers/test/server.properties
|
||||||
|
cmp /build/source/mod-data.dat /build/servers/test/mod-data.dat
|
||||||
|
cmp /build/source/mods/backup.jar /build/servers/test/mods/backup.jar
|
||||||
|
cmp /build/bootstrap/mods/bootstrap.jar /build/servers/test/mods/bootstrap.jar
|
||||||
|
test "$(readlink /build/servers/test/eula.txt)" = /nix/store/current-eula.txt
|
||||||
|
test "$(readlink /build/servers/test/level-link)" = world/level.dat
|
||||||
|
test "$(stat -c %i /build/servers/test/level-hardlink)" = "$(stat -c %i /build/servers/test/world/level.dat)"
|
||||||
|
test -f ${lib.escapeShellArg marker}
|
||||||
|
printf changed-after-restore > /build/servers/test/world/level.dat
|
||||||
|
printf changed-after-restore > /build/servers/test/config/settings.toml
|
||||||
|
cp -a /build/servers/test /build/completed
|
||||||
|
FAIL_DOWNLOAD=1 ${restore}
|
||||||
|
diff --no-dereference -r /build/completed /build/servers/test
|
||||||
|
if ${differentRestore}; then echo "accepted a different backup marker" >&2; exit 1; fi
|
||||||
|
diff --no-dereference -r /build/completed /build/servers/test
|
||||||
|
'';
|
||||||
|
|
||||||
|
minecraft-restore-fresh-target = runCase "fresh-target" ''
|
||||||
|
rm -rf /build/servers/test
|
||||||
|
${restore}
|
||||||
|
cmp /build/source/world/level.dat /build/servers/test/world/level.dat
|
||||||
|
test -f ${lib.escapeShellArg marker}
|
||||||
|
test ! -L /build/servers/test/eula.txt
|
||||||
|
'';
|
||||||
|
|
||||||
|
minecraft-restore-failures-preserve-bootstrap = runCase "failures-preserve-bootstrap" ''
|
||||||
|
cp /build/backup.tar.zst /build/good.tar.zst
|
||||||
|
if FAIL_DOWNLOAD=1 ${restore}; then exit 1; fi
|
||||||
|
assert_unchanged
|
||||||
|
printf corrupt > /build/backup.tar.zst
|
||||||
|
if ${restore}; then exit 1; fi
|
||||||
|
assert_unchanged
|
||||||
|
printf not-a-tar | zstd -q -f -o /build/backup.tar.zst
|
||||||
|
if ${restore}; then exit 1; fi
|
||||||
|
assert_unchanged
|
||||||
|
mkdir /build/wrong-world
|
||||||
|
cp -a /build/source/world /build/wrong-world/unrelated-save
|
||||||
|
tar -C /build/wrong-world -cf - . | zstd -q -f -o /build/backup.tar.zst
|
||||||
|
if ${restore}; then exit 1; fi
|
||||||
|
assert_unchanged
|
||||||
|
cp /build/good.tar.zst /build/backup.tar.zst
|
||||||
|
if ${sizeRestore}; then exit 1; fi
|
||||||
|
assert_unchanged
|
||||||
|
if FAIL_EXCHANGE=1 ${restore}; then exit 1; fi
|
||||||
|
assert_unchanged
|
||||||
|
'';
|
||||||
|
|
||||||
|
minecraft-restore-existing-world-refused = runCase "existing-world-refused" ''
|
||||||
|
for path in world custom/region dimensions/custom/region custom/playerdata custom/level.dat custom/chunk.mca; do
|
||||||
|
if [ "$path" = world ]; then
|
||||||
|
mkdir /build/servers/test/world
|
||||||
|
else
|
||||||
|
mkdir -p "/build/servers/test/''${path%/*}"
|
||||||
|
touch "/build/servers/test/$path"
|
||||||
|
fi
|
||||||
|
cp -a /build/servers/test /build/existing
|
||||||
|
if ${restore}; then echo "accepted existing world $path" >&2; exit 1; fi
|
||||||
|
test ! -e ${lib.escapeShellArg marker}
|
||||||
|
diff --no-dereference -r /build/existing /build/servers/test
|
||||||
|
rm -rf /build/servers/test /build/existing
|
||||||
|
cp -a /build/bootstrap /build/servers/test
|
||||||
|
done
|
||||||
|
'';
|
||||||
|
|
||||||
|
minecraft-restore-unsafe-archives-refused = runCase "unsafe-archives-refused" ''
|
||||||
|
unsafe() {
|
||||||
|
python ${badArchive} "$1" "$2" "$3"
|
||||||
|
zstd -q -f /build/unsafe.tar -o /build/backup.tar.zst
|
||||||
|
if ${restore}; then echo "accepted unsafe archive $1" >&2; exit 1; fi
|
||||||
|
assert_unchanged
|
||||||
|
}
|
||||||
|
unsafe ../outside 0 ""
|
||||||
|
unsafe /build/outside 0 ""
|
||||||
|
unsafe ./escape 2 ../../outside
|
||||||
|
unsafe ./absolute-link 2 /build/outside
|
||||||
|
unsafe ./hard-link 1 ../outside
|
||||||
|
unsafe ./.minecraft-backup-restore-complete 0 ""
|
||||||
|
test ! -e /build/outside
|
||||||
|
'';
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user