backups: skip preRestore and postRestore if not specified by a service

This commit is contained in:
Jörg Thalheim 2024-03-20 07:49:27 +01:00
parent aa659bcc17
commit 539df08706
2 changed files with 24 additions and 22 deletions

View File

@ -17,18 +17,18 @@
Folder where state resides in Folder where state resides in
''; '';
}; };
preRestoreScript = lib.mkOption { preRestoreCommand = lib.mkOption {
type = lib.types.str; type = lib.types.nullOr lib.types.str;
default = ":"; default = null;
description = '' description = ''
script to run before restoring the state dir from a backup script to run before restoring the state dir from a backup
Utilize this to stop services which currently access these folders Utilize this to stop services which currently access these folders
''; '';
}; };
postRestoreScript = lib.mkOption { postRestoreCommand = lib.mkOption {
type = lib.types.str; type = lib.types.nullOr lib.types.str;
default = ":"; default = null;
description = '' description = ''
script to restore the service after the state dir was restored from a backup script to restore the service after the state dir was restored from a backup

View File

@ -14,15 +14,16 @@ def restore_service(machine: Machine, name: str, provider: str, service: str) ->
env["NAME"] = name env["NAME"] = name
env["FOLDERS"] = ":".join(folders) env["FOLDERS"] = ":".join(folders)
proc = machine.target_host.run( if pre_restore := backup_folders[service]["preRestoreCommand"]:
[backup_folders[service]["preRestoreScript"]], proc = machine.target_host.run(
stdout=subprocess.PIPE, [pre_restore],
extra_env=env, stdout=subprocess.PIPE,
) extra_env=env,
if proc.returncode != 0:
raise ClanError(
f"failed to run preRestoreScript: {backup_folders[service]['preRestoreScript']}, error was: {proc.stdout}"
) )
if proc.returncode != 0:
raise ClanError(
f"failed to run preRestoreCommand: {pre_restore}, error was: {proc.stdout}"
)
proc = machine.target_host.run( proc = machine.target_host.run(
[backup_metadata["providers"][provider]["restore"]], [backup_metadata["providers"][provider]["restore"]],
@ -34,15 +35,16 @@ def restore_service(machine: Machine, name: str, provider: str, service: str) ->
f"failed to restore backup: {backup_metadata['providers'][provider]['restore']}" f"failed to restore backup: {backup_metadata['providers'][provider]['restore']}"
) )
proc = machine.target_host.run( if post_restore := backup_folders[service]["postRestoreCommand"]:
[backup_folders[service]["postRestoreScript"]], proc = machine.target_host.run(
stdout=subprocess.PIPE, [post_restore],
extra_env=env, stdout=subprocess.PIPE,
) extra_env=env,
if proc.returncode != 0:
raise ClanError(
f"failed to run postRestoreScript: {backup_folders[service]['postRestoreScript']}, error was: {proc.stdout}"
) )
if proc.returncode != 0:
raise ClanError(
f"failed to run postRestoreCommand: {post_restore}, error was: {proc.stdout}"
)
def restore_backup( def restore_backup(