localbackup: add pre/post exec hooks

This commit is contained in:
Jörg Thalheim
2024-03-20 10:13:30 +01:00
parent 58ae9d9cd0
commit 916e37eb26

View File

@@ -8,7 +8,7 @@ let
cfg = config.clan.localbackup; cfg = config.clan.localbackup;
rsnapshotConfig = target: states: '' rsnapshotConfig = target: states: ''
config_version 1.2 config_version 1.2
snapshot_root ${target} snapshot_root ${target.directory}
sync_first 1 sync_first 1
cmd_cp ${pkgs.coreutils}/bin/cp cmd_cp ${pkgs.coreutils}/bin/cp
cmd_rm ${pkgs.coreutils}/bin/rm cmd_rm ${pkgs.coreutils}/bin/rm
@@ -17,6 +17,13 @@ let
cmd_logger ${pkgs.inetutils}/bin/logger cmd_logger ${pkgs.inetutils}/bin/logger
cmd_du ${pkgs.coreutils}/bin/du cmd_du ${pkgs.coreutils}/bin/du
cmd_rsnapshot_diff ${pkgs.rsnapshot}/bin/rsnapshot-diff cmd_rsnapshot_diff ${pkgs.rsnapshot}/bin/rsnapshot-diff
${lib.optionalString (target.preExec != null) ''
cmd_preexec ${pkgs.writeShellScript "preexec.sh" target.preExec}
''}
${lib.optionalString (target.postExec != null) ''
cmd_postexec ${pkgs.writeShellScript "postexec.sh" target.postExec}
''}
retain snapshot ${builtins.toString config.clan.localbackup.snapshots} retain snapshot ${builtins.toString config.clan.localbackup.snapshots}
${lib.concatMapStringsSep "\n" (state: '' ${lib.concatMapStringsSep "\n" (state: ''
${lib.concatMapStringsSep "\n" (folder: '' ${lib.concatMapStringsSep "\n" (folder: ''
@@ -47,6 +54,16 @@ in
default = null; default = null;
description = "mountpoint of the directory to backup. If set, the directory will be mounted before the backup and unmounted afterwards"; description = "mountpoint of the directory to backup. If set, the directory will be mounted before the backup and unmounted afterwards";
}; };
preExec = lib.mkOption {
type = lib.types.nullOr lib.types.lines;
default = null;
description = "Shell commands to run before the backup";
};
postExec = lib.mkOption {
type = lib.types.nullOr lib.types.lines;
default = null;
description = "Shell commands to run after the backup";
};
}; };
} }
) )
@@ -90,8 +107,8 @@ in
( (
echo "Creating backup '${target.name}'" echo "Creating backup '${target.name}'"
${setupMount target.mountpoint} ${setupMount target.mountpoint}
rsnapshot -c "${pkgs.writeText "rsnapshot.conf" (rsnapshotConfig target.directory (lib.attrValues config.clanCore.state))}" sync rsnapshot -c "${pkgs.writeText "rsnapshot.conf" (rsnapshotConfig target (lib.attrValues config.clanCore.state))}" sync
rsnapshot -c "${pkgs.writeText "rsnapshot.conf" (rsnapshotConfig target.directory (lib.attrValues config.clanCore.state))}" snapshot rsnapshot -c "${pkgs.writeText "rsnapshot.conf" (rsnapshotConfig target (lib.attrValues config.clanCore.state))}" snapshot
) )
'') (builtins.attrValues cfg.targets)} '') (builtins.attrValues cfg.targets)}
'') '')
@@ -110,7 +127,7 @@ in
( (
${setupMount target.mountpoint} ${setupMount target.mountpoint}
find ${lib.escapeShellArg target.directory} -mindepth 1 -maxdepth 1 -name "snapshot.*" -print0 -type d \ find ${lib.escapeShellArg target.directory} -mindepth 1 -maxdepth 1 -name "snapshot.*" -print0 -type d \
| jq -Rs 'split("\u0000") | .[] | select(. != "") | { "name": ("${target.mountpoint}::" + .)}' | jq -Rs 'split("\u0000") | .[] | select(. != "") | { "name": ("${target.mountpoint or ""}::" + .)}'
) )
'') (builtins.attrValues cfg.targets) '') (builtins.attrValues cfg.targets)
}) | jq -s . }) | jq -s .
@@ -128,12 +145,14 @@ in
mountpoint=$(awk -F'::' '{print $1}' <<< $NAME) mountpoint=$(awk -F'::' '{print $1}' <<< $NAME)
backupname=''${NAME#$mountpoint::} backupname=''${NAME#$mountpoint::}
if [[ ! -z $backupname ]]; then
mkdir -p "$mountpoint" mkdir -p "$mountpoint"
if mountpoint -q "$mountpoint"; then if mountpoint -q "$mountpoint"; then
umount "$mountpoint" umount "$mountpoint"
fi fi
mount "$mountpoint" mount "$mountpoint"
trap "umount $mountpoint" EXIT trap "umount $mountpoint" EXIT
fi
IFS=';' read -ra FOLDER <<< "$FOLDERS" IFS=';' read -ra FOLDER <<< "$FOLDERS"
for folder in "''${FOLDER[@]}"; do for folder in "''${FOLDER[@]}"; do