borgbackup: use writeShellApplication

This commit is contained in:
Jörg Thalheim
2024-12-24 07:21:05 +01:00
parent 50c52dd099
commit 077f5415b0

View File

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