Server and backups#
Launch and control a Minecraft server in a Podman container.
This is what mciwb start and mciwb stop use:
MinecraftServer creates, starts, stops and removes the
container, and the constants here are the defaults of those commands.
mciwb talks to the Podman service over its REST API socket, see
container_host() for how the socket is found.
A Minecraft server in a Podman container on this machine. |
|
Backup and restore a Minecraft world folder, as zip files. |
|
Connect to the Podman service and check that it responds. |
|
Find the URL of the Podman service socket. |
The server#
- class mciwb.MinecraftServer(name, rcon, password, server_folder, world_type, keep=True, test=False, version=MINECRAFT_VERSION)[source]#
A Minecraft server in a Podman container on this machine.
Making a
MinecraftServeronly records the settings. Callcreate()to start the container (or to adopt one of the same name that already exists). This is whatmciwb startdoes.The server runs the image
SERVER_IMAGEin creative mode with seed 0 and no spawn protection, and keeps ticking when no players are online. Its files, including the world, are in server_folder and belong to the current user.- Parameters:
name (str) -- The name of the server’s container.
rcon (int) -- The RCON port of the server. The game port, which players connect to, is
rcon + 1.password (str) -- The RCON password of the server.
server_folder (Path) -- The folder to store the server’s files in. It is created if it does not exist.
world_type (str) -- The type of world to create: a world preset such as
"normal"or"flat". Only used when the folder has no world yet.keep (bool) -- Keep the server running: the container has the
alwaysrestart policy, sopodman-restart.servicerestarts it after a reboot, andremove()does nothing unless forced.test (bool) -- Run the server in test mode: a flat world without structures or mobs. server_folder is deleted, and so the world is made afresh, each time
create()makes a new container.version (str) -- The Minecraft Java Edition release to run. Note that an existing world is upgraded if it is opened with a newer release.
Example:
from pathlib import Path from mciwb import MinecraftServer folder = Path.home() / "flat-server" server = MinecraftServer("flat-server", 20200, "secret", folder, "flat") server.create() # players join on port 20201 # later, from any process: stop the server and remove its container MinecraftServer.stop_named("flat-server")
- rcon#
The RCON port. If
createadopts an existing container, this andport,password,versionandserver_folderchange to that container’s settings.
- port#
The game port that players connect to, which is
rcon + 1.
- name#
The name of the server’s container.
- password#
The RCON password.
- server_folder#
The folder that holds the server’s files.
- world#
The world folder, which is
server_folder / "world".
- world_type#
The world preset for a new world.
- keep#
True if the container restarts by itself and
removeneeds force.
- test#
True for a test mode server.
- version#
The Minecraft Java Edition release that the server runs.
- wait_server()[source]#
Wait until the server is ready to accept RCON connections.
RCON is the last part of the server to come up, so the server is then ready for players too. A wrong RCON password raises
rcon.exceptions.WrongPasswordat once. The container must exist (seecreate()).- Raises:
RuntimeError -- If the container stops, or the server does not accept connections within 300 seconds (the class attribute
start_timeout: a new world, or a world upgrade, can take minutes). The error includes the end of the server log.
- stop()[source]#
Stop the Minecraft server, leaving its container in place.
The server gets 60 seconds to save the world and shut down before it is killed. A server that is already stopped is left alone. Start it again with
start(). The container must exist (seecreate()).
- start()[source]#
Start the stopped container of the server and wait until it is ready.
The container must exist: use
create()to make it.- Raises:
RuntimeError -- If the server fails to start, see
wait_server().
- remove(force=False)[source]#
Stop the server and remove its container.
This does nothing for a server made with keep set, unless force is True, and nothing if there is no container. The server folder, with the world, is never deleted.
- Parameters:
force -- Remove the container even if the server was made with keep set.
- create(force=False)[source]#
Spin up a Minecraft server in a container, and wait until it is ready.
A new world is created if the server folder has none. A new server also gets a few settings over RCON: the chunk at the world origin is kept loaded, and the world spawn is set to (632, 73, -1658), a good place to start in the seed 0 world. A test mode server gets different ones: the blocks from -48 to 47 in x and z are kept loaded and mobs do not spawn, and its world spawn is not set.
If a container of this name already exists it is reused (and started if it is stopped) with the settings that it was created with: this object’s port, password, version and folder change to match, with a warning. Use force, or
mciwb stop, to recreate it with new settings.- Parameters:
force (bool) -- Remove a container of this name that already exists, and make a new one. The world in the server folder is kept (except in test mode).
- Raises:
PodmanNotAvailableError -- If no Podman service is reachable.
RuntimeError -- If the server fails to start, see
wait_server().
- classmethod stop_named(name)[source]#
Stop a Minecraft server by name, and remove its container.
This is what
mciwb stopdoes. The container is removed whether or not the server was made with keep set. The server folder, with the world, is not touched, somciwb start(orcreate()) with the same folder brings the same world back. If there is no such container, a warning is logged and nothing else happens.- Parameters:
name (str) -- The name of the server’s container.
- Raises:
PodmanNotAvailableError -- If no Podman service is reachable.
Backups#
Backup and restore the world folder of a Minecraft server as zip files.
- class mciwb.Backup(world_folder=default_server_folder / 'world', backup_folder=backup_folder_default)[source]#
Backup and restore a Minecraft world folder, as zip files.
The world folder must be on this machine. Making a
Backupcreates the backup folder if it does not exist.- Parameters:
Example:
from mciwb import Backup backup = Backup() # with the server running, and connected to it (for example in a # script run by mciwb run) file = backup.backup("before-the-castle") # later, with the server stopped backup.restore("before-the-castle")
- world_folder#
The world folder that is backed up and restored.
- backup_folder#
The folder that holds the backup zip files.
- backup(name=None, running=True)[source]#
Backup the world folder to a zip file in the backup folder.
A backup with the same name is replaced. The world’s
.lockfiles are left out.- Parameters:
name (str | None) -- The name of the backup file.
.zipis added if it is missing, and any character other than a letter, a digit,-,_or.becomes_. Defaults to the current date and time, asyy-mm-dd.HH.MM.SS.zip.running (bool) -- True if the server is running. The server is then told to save everything to disk and to pause saving during the backup, and the players are told about it in chat. This needs an RCON client for the current thread (see
get_client()). Pass False for a stopped server.
- Returns:
The path of the backup file.
- Raises:
ValueError -- If the world folder has no
level.dat, so is not a Minecraft world.- Return type:
- find(name=None)[source]#
Find a backup file in the backup folder.
- Parameters:
name (str | None) -- The name of the backup file, with or without
.zip. Defaults to the most recently modified backup.- Returns:
The path of the backup file.
- Raises:
ValueError -- If there is no backup with that name, or the backup folder has no backups at all.
- Return type:
- restore(name=None, backup=False)[source]#
Restore the world folder from a backup. The server must be stopped.
The current world folder is replaced. It is kept as a sibling folder with the suffix
-oldif backup is True, and deleted otherwise.- Parameters:
- Raises:
ValueError -- If the backup file does not exist. The current world is then left as it is.
Podman#
- mciwb.server.podman_client()[source]#
Connect to the Podman service and check that it responds.
The service is found with
container_host().- Returns:
A connected
podman.PodmanClient. Close it when finished, most easily by using it as a context manager.- Raises:
PodmanNotAvailableError -- If no socket is found, or the service does not respond.
- Return type:
Example:
from mciwb.server import podman_client with podman_client() as client: print([c.name for c in client.containers.list(all=True)])
- mciwb.server.container_host()[source]#
Find the URL of the Podman service socket.
In order of preference:
the
CONTAINER_HOSTenvironment variable, used as it isthe rootless socket
$XDG_RUNTIME_DIR/podman/podman.sockthe rootful socket
/run/podman/podman.sock
- Returns:
The URL, for example
unix:///run/user/1000/podman/podman.sock. Nothing is connected to yet, so the service may still not respond.- Raises:
PodmanNotAvailableError -- If
CONTAINER_HOSTis not set and neither socket exists.- Return type:
- exception mciwb.server.PodmanNotAvailableError[source]#
No Podman service is reachable to run the Minecraft server in.
Raised by
container_host()andpodman_client(), and so bycreate()andstop_named(). The message says where the socket was looked for and how to start the service (systemctl --user enable --now podman.socket).
Defaults#
- mciwb.server.HOST = 'localhost'#
The address of a server that runs on this machine, and the default server of
Iwb.
- mciwb.server.def_port = 20100#
The default RCON port. The game port, which players connect to, is one higher: 20101.
- mciwb.server.def_pass = 'default_pass'#
The default RCON password. Anyone who can reach the RCON port can run commands with it, so choose another for a server that others can reach.
- mciwb.server.PASSWORD_ENV = 'MCIWB_PASSWORD'#
The environment variable that holds the RCON password.
- mciwb.server.def_world_type = 'normal'#
The default world preset, which gives Minecraft’s normal terrain.
- mciwb.server.server_name = 'mciwb-server'#
The default name of the server’s container.
- mciwb.server.MINECRAFT_VERSION = '26.3'#
The Minecraft Java Edition release that mciwb servers run. Keep this in step with
mciwb.item.MINECRAFT_VERSION, the release thatItemis generated from.
- mciwb.server.SERVER_IMAGE = 'docker.io/itzg/minecraft-server:2026.10.0-java25'#
The itzg/minecraft-server image, pinned for reproducibility. Minecraft 26.x needs Java 25, hence the java25 variant.
- mciwb.server.default_server_folder#
The default folder for the server’s files,
~/mciwb-server. The world is in itsworldsubfolder.
- mciwb.server.backup_folder_default#
The default folder for backups of the server’s world,
~/mciwb-backups.