Skip to content

About

A grid backup system for Magnetar

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

GridBackups

Magnetar server plugin for Space Engineers dedicated servers.

GridBackups exports ship/station grid blueprints on configurable backup triggers. Standalone servers keep backups in the Quasar data folder by default.

In a cluster, the plugin keeps its backups in the cluster's shared storage regardless of UseQuasarConfigFolder. On one Host that is the plugin's folder in the shared directory (CLUSTER_SHARED_ROOT), where per-grid file locks coordinate the nodes, as in earlier versions. A cluster on several Host machines runs the cluster's storage service (CLUSTER_STORAGE) instead, and the nodes of every Host reach the same backups over the network through the Plugin SDK's shared store. The files are the same either way, so backups taken before are kept and nothing is converted.

With the storage service a backup is written once under a unique name and never changed, so nodes need no locks. A node first writes a backup to an upload queue on its own disk (GridBackupsUploadQueue in the node's data folder) and sends it to the store in the background, in order. A backup is therefore taken while the storage can't be reached, for example while a region Host's link to the main Host is down; it survives a node restart and arrives when the storage is back. The queue holds at most UploadQueueMegabytes (2048). A node that stops first waits up to UploadQueueStopSeconds (30) for its queue to reach the storage, because only that node can read its queue. What is still queued then is logged as an error with the folder, and is sent when the node starts again with the same data folder. A node that is removed for good after such a stop keeps those backups on its disk. When it is full a backup is refused and taken at a later scan. Listings, the owner index and the hourly deletion of old backups are answered from the storage service's index. No storage call runs on the game thread.

The storage service needs Magnetar 2.4.3.13 or later (Plugin SDK contract 1.2). On an older Magnetar the plugin works as before on a plain server and on a one-Host cluster, and fails to load in a cluster that announces a storage service. Update all nodes of a cluster together.

On a cluster with a Gateway, a node backs up only the grids it owns and simulates (PluginClusterClient.LocateEntity says Local). It writes nothing while it starts, stops or is fenced, or for ClusterSettleSeconds (120) after it starts serving. A grid frozen for a handover, or still being loaded, is skipped. So is a grid in the first ClusterSettleSeconds after its partition came to the node (a load or a handover) or after it arrived there. The plugin logs Backups on hold and Backups resumed with the reason, and each backup's side file names the node that took it. Nodes that only share a storage root, without a Gateway, back up every grid they see. Quasar sets CLUSTER_SHARED_ROOT automatically for a single Host. A cluster on several Hosts needs the cluster's storage service. With neither, the plugin fails to load instead of writing separate per-Host backups.

Configuration

The server plugin uses Magnetar PluginSdk configuration. Quasar can render the config UI from the schema generated by GridBackupsConfig. A GridBackups.cfg that fails to load is renamed to GridBackups.cfg.corrupted.<time>.txt and replaced by the defaults.

Important options:

  • Standalone storage root defaults to the Quasar data folder (QUASAR_DATA_DIR, derived from QUASAR_DS_CONFIG_PATH, or the platform default Quasar config directory). With UseQuasarConfigFolder off it is the server's data folder instead. Cluster storage uses the SDK shared root.
  • Minimum and maximum minutes since last backup.
  • In a cluster, the seconds to hold backups after a node start, a partition load or handover, or a grid arrival (ClusterSettleSeconds).
  • In a cluster, the megabytes of backups a node may hold on its own disk while the shared storage can't be reached (UploadQueueMegabytes).
  • In a cluster, the seconds a stopping node waits for its queued backups to reach the shared storage (UploadQueueStopSeconds).
  • Backup before concealment via public plugin entrypoint.
  • Backup when steady after a configured unchanged block-count duration.
  • Backup after a configured maximum block-count change.
  • Connected-grid grouping, owner folder naming, NPC/nobody grid inclusion, and retention limits.

Commands

  • !gridbackup list <PlayerName|SteamID|IdentityID> lists backed up grids for a player.
  • !gridbackup list <PlayerName|SteamID|IdentityID> <GridName|GridID|ListNumber> lists backup files for one grid.
  • !gridbackup list faction <FactionTag> lists backed up grids for a faction.
  • !gridbackup find <GridName|GridID> searches matching backed up grids across known owners.
  • !gridbackup restore <PlayerName|SteamID|IdentityID> <GridName|GridID|ListNumber> [backupNumber] [keepOriginalPosition] [force] restores a backup.
  • !gridbackup restore faction <FactionTag> <GridName|GridID|ListNumber> [backupNumber] [keepOriginalPosition] [force] restores from a faction member backup.
  • !gridbackup run starts a full backup scan.
  • !gridbackup status shows current backup service state.

Restore defaults to the latest backup, spawning near the caller. Console restores must use keepOriginalPosition=true. Use force=true to skip the occupation check.

External API

Other plugins can call:

bool WouldBackupBeforeConcealment(List<MyCubeGrid> grids);
bool BackupBeforeConcealment(List<MyObjectBuilder_CubeGrid> grids, long ownerIdentity);
bool BackupGridsManuallyWithBuilders(List<MyObjectBuilder_CubeGrid> grids, long ownerIdentity);

BackupBeforeConcealment applies the same filters as the scans: the BackupBeforeConcealment option, BackupNobodyGrids and BackupNpcGrids for ownerIdentity, MinBlocksForBackup for the total block count of the grids, and MinMinutesSinceLastSave. WouldBackupBeforeConcealment answers whether it would back up a grid group now, without building object builders, so a concealment plugin can skip building them for a backup that won't be written. Pass it the same grids, and call it on the game's update thread.

Companion operations and the loss log

Quasar's assistant (QSR-4) calls Grid Backups through the companion channel: list, describe, losses, restore and remove-restored, for a player named by Steam id. A restore spawns the grids with the entity ids of the backup, so the same lost grid is the same grid after it comes back. The plugin also keeps a loss log: each player-owned grid it backs up that leaves the world, with the cause (destroyed, deleted, cleanup, ground, merged, stored in Hangar or unknown), in the backup folder. Every big owner of a backup is recorded, so a grid is listed for each of them, by the companion operations and by !gridbackup list. See docs/CompanionOperations.md for the JSON contract and how the causes are told apart.

Build

Requirements:

  • Space Engineers Dedicated Server
  • Magnetar
  • .NET 10 SDK
  • .NET Framework 4.8.1 Developer Pack on Windows
  • Python 3.12 or newer, for setup.py

Build:

dotnet build GridBackups.sln --configuration Debug

Directory.Build.props auto-detects the Dedicated Server (Dedicated64) and the Magnetar installation (Magnetar). To override them, set them in Directory.Build.props.user, which is not committed. setup.py writes that file with the auto-detected Dedicated Server folder. The plugin version lives in Version.Build.props.

Development and deployment

Load the working copy through a Magnetar development folder: start Magnetar with -sources and add the repository with the Sources button. Magnetar then compiles the plugin from source.

Builds deploy nothing by default. To copy the build into <MagnetarData>/Local, set MagnetarData (the Magnetar config folder) in Directory.Build.props.user or pass -p:MagnetarData=... to the build.

Tests

tests/ holds the plugin's test suite. It runs on Linux only.

tests/unit has unit tests of the logic that needs no game: the storage root (Quasar data folder, the server's data folder, cluster storage), a broken config file, the backup triggers, retention, grid name matching and folder names, the backup index, and the restore decisions (big owners, backup choice, loss causes, the placement search). They build against the Dedicated Server and Magnetar like the plugin does:

dotnet test tests/unit

The rest is an in-game suite. It starts a Magnetar server with this working copy of the plugin, joins it with two headless game clients and drives them through the Remote plugin: player A is an administrator, player B has no rights. The tests run the chat commands and check the backup files, the plugin's log and the grids on the server. What it covers:

  • Backup triggers: the first scan, the minimum and maximum time since the last backup, a steady block count, a large block count change, and the entry points other plugins call, BackupBeforeConcealment among them (test_triggers.py)
  • What a backup holds and where it goes: grids joined by a rotor, owner folder names, NPC and nobody grids, the original owners, retention of normal, daily and old backups (test_contents.py)
  • The !gridbackup commands, their permissions and bad input (test_commands.py)
  • Restores: a restored grid matching the backup, near the caller or in place, backup numbers, faction restores, and the occupied-area check (test_restore.py)
  • The Quasar data folder from QUASAR_DATA_DIR, QUASAR_DS_CONFIG_PATH or the home folder, and a broken config file, each on a server of its own (test_storage.py)
  • Cluster storage: two servers started as cluster nodes that share one storage root (nodes.py), coordinating their backups through it (test_cluster.py)
  • Backups, config and listing across a server restart (test_restart.py)
  • The companion operations: grid losses and their causes, restores that keep the entity ids, the checks that refuse one, its undo, placement away from other players' grids, co-owners, and the plugin found and called by its companion members the way Quasar.Agent does it (test_companion.py)

The test world is the game's Empty World with the test players, a faction and a row of test grids, written fresh for every run. tests/probe is a test-only server plugin the servers load next to Grid Backups. It puts a new config into the running plugin, moves a grid's last backup back in time instead of waiting for minutes to pass, reports the state of grids, calls the public entry points, and runs chat commands as the console from a file, which is how the tests drive the servers that have no clients.

The ownership and the holds on a real cluster (Gateway, World Authority, partitions, handover, drain) are tested by the cluster repository's scenario bench: Test/grid_backups_scenario.py in CometWorks/cluster (P2-GRID-BACKUPS) loads a checkout of this repository on a local cluster and checks the start, owner, handover and drain rules from each backup's side file.

Running

You need the Space Engineers client and Dedicated Server from Steam, Magnetar and Pulsar installed, uv, and working copies of Remote and DirectTransport. The rig takes the ports, client ids and folders from a test slot of the se workspace (testing/parallel-test-work), and waits for free memory before each process it starts.

uv run pytest

Where GitHub's API rate limits Magnetar, export PULSAR_GITHUB_TOKEN for it to download its hub plugins with. A full run takes about 40 minutes, most of it starting servers; dotnet test tests/unit takes seconds. tests/rig.py lists the environment variables that point the rig at other installations and working copies.

To keep the server and clients running between runs while you work on a test:

uv run python tests/session.py start
GB_ATTACH=1 GB_KEEP=1 uv run pytest tests/test_commands.py
uv run python tests/session.py stop

Functionality is inspired by and reimplements the original Torch plugin GridBackup by LordTylus (license: Apache-2.0): https://github.com/LordTylus/SE-Torch-Plugin-ALE-GridBackup

About

A grid backup system for Magnetar

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages