Merge pull request 'backup fixes' (#2650) from borgbackup into main

This commit is contained in:
clan-bot
2024-12-24 06:36:06 +00:00
3 changed files with 52 additions and 33 deletions

View File

@@ -1,7 +1,7 @@
(import ../lib/container-test.nix) (
{ pkgs, ... }:
{
name = "secrets";
name = "deltachat";
nodes.machine =
{ self, ... }:

View File

@@ -138,34 +138,43 @@ in
}) cfg.destinations;
environment.systemPackages = [
(pkgs.writeShellScriptBin "borgbackup-create" ''
set -efu -o pipefail
(pkgs.writeShellApplication {
name = "borgbackup-create";
runtimeInputs = [ config.systemd.package ];
text = ''
${lib.concatMapStringsSep "\n" (dest: ''
systemctl start borgbackup-job-${dest.name}
'') (lib.attrValues cfg.destinations)}
'')
(pkgs.writeShellScriptBin "borgbackup-list" ''
set -efu
'';
})
(pkgs.writeShellApplication {
name = "borgbackup-list";
runtimeInputs = [ pkgs.jq ];
text = ''
(${
lib.concatMapStringsSep "\n" (
dest:
# we need yes here to skip the changed url verification
''yes y | borg-job-${dest.name} list --json | jq '[.archives[] | {"name": ("${dest.name}::${dest.repo}::" + .name)}]' ''
''echo y | /run/current-system/sw/bin/borg-job-${dest.name} list --json | jq '[.archives[] | {"name": ("${dest.name}::${dest.repo}::" + .name)}]' ''
) (lib.attrValues cfg.destinations)
}) | ${pkgs.jq}/bin/jq -s 'add'
'')
(pkgs.writeShellScriptBin "borgbackup-restore" ''
set -efux
}) | jq -s 'add // []'
'';
})
(pkgs.writeShellApplication {
name = "borgbackup-restore";
runtimeInputs = [ pkgs.gawk ];
text = ''
cd /
IFS=':' read -ra FOLDER <<< "$FOLDERS"
job_name=$(echo "$NAME" | ${pkgs.gawk}/bin/awk -F'::' '{print $1}')
IFS=':' read -ra FOLDER <<< "''${FOLDERS-}"
job_name=$(echo "$NAME" | awk -F'::' '{print $1}')
backup_name=''${NAME#"$job_name"::}
if ! command -v borg-job-"$job_name" &> /dev/null; then
if [[ ! -x /run/current-system/sw/bin/borg-job-"$job_name" ]]; then
echo "borg-job-$job_name not found: Backup name is invalid" >&2
exit 1
fi
yes y | borg-job-"$job_name" extract --list "$backup_name" "''${FOLDER[@]}"
'')
echo y | /run/current-system/sw/bin/borg-job-"$job_name" extract "$backup_name" "''${FOLDER[@]}"
'';
})
];
# Facts generation. So the client can authenticate to the server

View File

@@ -21,15 +21,25 @@ class Backup:
def list_provider(machine: Machine, provider: str) -> list[Backup]:
results = []
backup_metadata = json.loads(machine.eval_nix("config.clan.core.backups"))
list_command = backup_metadata["providers"][provider]["list"]
proc = machine.target_host.run(
[backup_metadata["providers"][provider]["list"]],
RunOpts(log=Log.STDERR, check=False),
[list_command],
RunOpts(log=Log.NONE, check=False),
)
if proc.returncode != 0:
# TODO this should be a warning, only raise exception if no providers succeed
msg = f"failed to list backups for provider {provider}: {proc.stdout}"
msg = f"Failed to list backups for provider {provider}:"
msg += f"\n{list_command} exited with {proc.returncode}"
if proc.stderr:
msg += f"\nerror output: {proc.stderr}"
raise ClanError(msg)
try:
parsed_json = json.loads(proc.stdout)
except json.JSONDecodeError as e:
msg = f"Failed to parse json output from provider {provider}:\n{proc.stdout}"
raise ClanError(msg) from e
for archive in parsed_json:
results.append(Backup(name=archive["name"], job_name=archive.get("job_name")))
return results