Backup & Restore API Documentation
Provides API functions for server backup, restore, and pruning operations.
This module offers a high-level interface for managing the backup and restoration
of Bedrock server data. It orchestrates calls to methods of the
BedrockServer
class,
primarily those provided by the
ServerBackupMixin
.
- Key functionalities include:
Listing available backup files (
list_backup_files()
).Backing up individual components like the server world (
backup_world()
) or specific configuration files (backup_config_file()
).Performing a comprehensive backup of all standard server data (
backup_all()
).Restoring all server data from the latest available backups (
restore_all()
).Restoring the server world from a specific
.mcworld
file (restore_world()
).Restoring a specific configuration file from its backup (
restore_config_file()
).Pruning old backups based on retention policies (
prune_old_backups()
).
Operations involving file modifications are thread-safe using a unified lock
(_backup_restore_lock
). For actions requiring the server to be offline,
this module utilizes the
server_lifecycle_manager()
to safely stop and restart the server. All functions are exposed to the plugin system.
- bedrock_server_manager.api.backup_restore.list_backup_files(server_name: str, backup_type: str, app_context: AppContext | None = None) Dict[str, Any]
Lists available backup files for a given server and type.
This is a read-only operation and does not require a lock. It calls
list_backups()
.- Parameters:
server_name (str) – The name of the server.
backup_type (str) – The type of backups to list. Valid options are “world”, “properties”, “allowlist”, “permissions”, or “all” (case-insensitive).
- Returns:
A dictionary with the operation result. On success:
{"status": "success", "backups": BackupData}
. If backup_type is specific (e.g., “world”), BackupData isList[str]
of backup file paths. If backup_type is “all”, BackupData isDict[str, List[str]]
categorizing backups (e.g.,{"world_backups": [...], "properties_backups": [...]}
). An empty list/dict is returned if no backups are found or backup dir is missing. On error:{"status": "error", "message": "<error_message>"}
.- Return type:
Dict[str, Any]
- Raises:
InvalidServerNameError – If the server name is empty.
MissingArgumentError – If backup_type is empty.
UserInputError – If backup_type is invalid.
ConfigurationError – If the server’s backup directory is not configured.
FileOperationError – For OS errors during file listing.
- bedrock_server_manager.api.backup_restore.backup_world(server_name: str, stop_start_server: bool = True, app_context: AppContext | None = None) Dict[str, str]
Creates a backup of the server’s world directory.
This operation is thread-safe and guarded by a lock. It calls the internal
_backup_world_data_internal
method of theBedrockServer
instance, which handles determining the active world, exporting it to a.mcworld
file, and pruning old world backups. If stop_start_server isTrue
, theserver_lifecycle_manager()
is used to manage the server’s state. Triggersbefore_backup
andafter_backup
plugin events (with type “world”).- Parameters:
server_name (str) – The name of the server whose world is to be backed up.
stop_start_server (bool, optional) – If
True
, the server will be stopped before the backup and restarted afterwards. Defaults toTrue
.
- Returns:
A dictionary with the operation result. Possible statuses: “success”, “error”, or “skipped” (if lock not acquired). On success:
{"status": "success", "message": "World backup '<filename>' created..."}
On error:{"status": "error", "message": "<error_message>"}
.- Return type:
Dict[str, str]
- Raises:
MissingArgumentError – If server_name is empty.
BSMError – Propagates errors from underlying operations, including:
ConfigurationError
(backup path not set),AppFileNotFoundError
(world dir missing),BackupRestoreError
(export/pruning issues), or errors from server stop/start.
- bedrock_server_manager.api.backup_restore.backup_config_file(server_name: str, file_to_backup: str, stop_start_server: bool = True, app_context: AppContext | None = None) Dict[str, str]
Creates a backup of a specific server configuration file.
This operation is thread-safe and guarded by a lock. It calls the internal
_backup_config_file_internal
method of theBedrockServer
instance. This core method copies the specified file (e.g.,server.properties
) from the server’s installation directory to a timestamped backup in the server’s backup directory, then prunes older backups of that file type. Theserver_lifecycle_manager()
is used if stop_start_server isTrue
, though typically not strictly necessary for config file backups unless there’s a concern about live writes. Triggersbefore_backup
andafter_backup
plugin events (with type “config_file”).- Parameters:
server_name (str) – The name of the server.
file_to_backup (str) – The name of the configuration file to back up (e.g., “server.properties”, “allowlist.json”). This file is expected to be in the root of the server’s installation directory.
stop_start_server (bool, optional) – If
True
, the server lifecycle will be managed (stopped before, restarted after if it was running). While often not strictly needed for config file backups, it can ensure consistency if the server might be writing to the file. Defaults toTrue
.
- Returns:
A dictionary with the operation result. Possible statuses: “success”, “error”, or “skipped” (if lock not acquired). On success:
{"status": "success", "message": "Config file '<name>' backed up as '<backup_name>'..."}
If original file not found:{"status": "error", "message": "Config file backup failed: File ... not found."}
(or similar from BSMError) On other error:{"status": "error", "message": "<error_message>"}
.- Return type:
Dict[str, str]
- Raises:
MissingArgumentError – If server_name or file_to_backup is empty.
BSMError – Propagates errors from underlying operations, including
ConfigurationError
(backup path not set),FileOperationError
(file copy/pruning issues), or errors from server stop/start if stop_start_server is true.
- bedrock_server_manager.api.backup_restore.backup_all(server_name: str, stop_start_server: bool = True, app_context: AppContext | None = None) Dict[str, Any]
Performs a full backup of the server’s world and configuration files.
This operation is thread-safe and guarded by a lock. It calls
backup_all_data()
. If stop_start_server isTrue
, theserver_lifecycle_manager()
is used to stop the server before the backup. Note: The server is not automatically restarted by this specific API function after the backup, even if stop_start_server is true; only the stop phase of the lifecycle manager is effectively used here for backup_all. Triggersbefore_backup
andafter_backup
plugin events (with type “all”).- Parameters:
server_name (str) – The name of the server to back up.
stop_start_server (bool, optional) – If
True
, the server will be stopped before the backup operation begins. Defaults toTrue
.
- Returns:
A dictionary with the operation result. Possible statuses: “success”, “error”, or “skipped” (if lock not acquired). On success:
{"status": "success", "message": "Full backup completed...", "details": BackupResultsDict}
whereBackupResultsDict
maps component names (e.g., “world”, “allowlist.json”) to the path of their backup file, orNone
if a component’s backup failed. On error (e.g., critical world backup failure):{"status": "error", "message": "<error_message>"}
.- Return type:
Dict[str, Any]
- Raises:
MissingArgumentError – If server_name is empty.
BSMError – Propagates errors from underlying operations, including:
ConfigurationError
(backup path not set),BackupRestoreError
(if critical world backup fails), or errors from server stop if stop_start_server is true.
- bedrock_server_manager.api.backup_restore.restore_all(server_name: str, stop_start_server: bool = True, app_context: AppContext | None = None) Dict[str, Any]
Restores the server from the latest available backups.
This operation is thread-safe and guarded by a lock. It calls
restore_all_data_from_latest()
. If stop_start_server isTrue
, theserver_lifecycle_manager()
is used to manage the server’s state, restarting it only if the restore operation (all components) is successful.Warning
This operation OVERWRITES current world data and configuration files in the server’s installation directory with content from the latest backups.
Triggers
before_restore
andafter_restore
plugin events (with type “all”).- Parameters:
server_name (str) – The name of the server to restore.
stop_start_server (bool, optional) – If
True
, the server will be stopped before restoring and restarted afterwards only if the entire restore operation succeeds. Defaults toTrue
.
- Returns:
A dictionary with the operation result. Possible statuses: “success”, “error”, or “skipped” (if lock not acquired). On success:
{"status": "success", "message": "Restore_all completed...", "details": RestoreResultsDict}
whereRestoreResultsDict
maps component names to their restored paths orNone
on failure/skip. If no backups found:{"status": "success", "message": "No backups found..."}
On error:{"status": "error", "message": "<error_message>"}
(e.g., if a component failed to restore).- Return type:
Dict[str, Any]
- Raises:
MissingArgumentError – If server_name is empty.
BSMError – Propagates errors from underlying operations, including:
ConfigurationError
(backup path not set),BackupRestoreError
(if any component fails to restore), or errors from server stop/start.
- bedrock_server_manager.api.backup_restore.restore_world(server_name: str, backup_file_path: str, stop_start_server: bool = True, app_context: AppContext | None = None) Dict[str, str]
Restores a server’s world from a specific backup file.
This operation is thread-safe and guarded by a lock. If stop_start_server is
True
, it uses theserver_lifecycle_manager()
to manage the server’s state, restarting it only if the restore is successful. The core world import is performed byimport_active_world_from_mcworld()
.Warning
This is a DESTRUCTIVE operation. The existing active world directory will be deleted before the new world is imported from the backup.
Triggers
before_restore
andafter_restore
plugin events (with type “world”).- Parameters:
server_name (str) – The name of the server.
backup_file_path (str) – The absolute path to the
.mcworld
backup file to be restored.stop_start_server (bool, optional) – If
True
, the server will be stopped before restoring and restarted afterwards only if the restore is successful. Defaults toTrue
.
- Returns:
A dictionary with the operation result. Possible statuses: “success”, “error”, or “skipped” (if lock not acquired). On success:
{"status": "success", "message": "World restore from '<filename>' completed..."}
On error:{"status": "error", "message": "<error_message>"}
.- Return type:
Dict[str, str]
- Raises:
MissingArgumentError – If server_name or backup_file_path is empty.
AppFileNotFoundError – If backup_file_path does not exist.
BSMError – Propagates errors from underlying operations like
BackupRestoreError
,ExtractError
, or errors from server stop/start.
- bedrock_server_manager.api.backup_restore.restore_config_file(server_name: str, backup_file_path: str, stop_start_server: bool = True, app_context: AppContext | None = None) Dict[str, str]
Restores a specific config file from a backup.
This operation is thread-safe and guarded by a lock. If stop_start_server is
True
, it uses theserver_lifecycle_manager()
to manage the server’s state, restarting it only if the restore is successful. The core config file restoration is performed by the internal_restore_config_file_internal
method of theBedrockServer
instance.Warning
This operation OVERWRITES the current version of the configuration file in the server’s installation directory with the content from the backup.
Triggers
before_restore
andafter_restore
plugin events (with type “config_file”).- Parameters:
server_name (str) – The name of the server.
backup_file_path (str) – The absolute path to the configuration backup file (e.g.,
.../server_backup_YYYYMMDD_HHMMSS.properties
).stop_start_server (bool, optional) – If
True
, the server will be stopped before restoring and restarted afterwards only if the restore is successful. Defaults toTrue
.
- Returns:
A dictionary with the operation result. Possible statuses: “success”, “error”, or “skipped” (if lock not acquired). On success:
{"status": "success", "message": "Config file '<original_name>' restored from '<backup_name>'..."}
On error:{"status": "error", "message": "<error_message>"}
.- Return type:
Dict[str, str]
- Raises:
MissingArgumentError – If server_name or backup_file_path is empty.
AppFileNotFoundError – If backup_file_path does not exist.
UserInputError – If the backup filename format is unrecognized.
BSMError – Propagates errors from underlying operations like
FileOperationError
or errors from server stop/start.
- bedrock_server_manager.api.backup_restore.prune_old_backups(server_name: str, app_context: AppContext | None = None) Dict[str, str]
Prunes old backups for a server based on retention settings.
This operation is thread-safe and guarded by a lock. It iteratively calls
prune_server_backups()
for the server’s world (.mcworld
files) and standard configuration files (server.properties
,allowlist.json
,permissions.json
). The number of backups to keep is determined by theretention.backups
application setting. Triggersbefore_prune_backups
andafter_prune_backups
plugin events.- Parameters:
server_name (str) – The name of the server whose backups are to be pruned.
- Returns:
A dictionary with the operation result. Possible statuses: “success”, “error”, or “skipped” (if lock not acquired). On full success:
{"status": "success", "message": "Backup pruning completed..."}
If some components fail pruning:{"status": "error", "message": "Pruning completed with errors: <details>"}
If backup directory not found:{"status": "success", "message": "No backup directory found..."}
On other setup error:{"status": "error", "message": "<error_message>"}
.- Return type:
Dict[str, str]
- Raises:
MissingArgumentError – If server_name is empty.
BSMError – Propagates errors from underlying operations, particularly
ConfigurationError
if backup path is not set, orUserInputError
if retention settings are invalid. IndividualFileOperationError
for components are typically aggregated into the error message.