2015-12-28 18:43:31 +00:00
|
|
|
|
{ config, lib, pkgs, ... }:
|
2016-10-20 18:54:38 +00:00
|
|
|
|
with import <stockholm/lib>;
|
2015-12-28 18:43:31 +00:00
|
|
|
|
let
|
|
|
|
|
out = {
|
|
|
|
|
options.krebs.backup = api;
|
2016-02-14 15:43:44 +00:00
|
|
|
|
config = lib.mkIf cfg.enable imp;
|
2015-12-28 18:43:31 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
cfg = config.krebs.backup;
|
|
|
|
|
|
|
|
|
|
api = {
|
|
|
|
|
enable = mkEnableOption "krebs.backup" // { default = true; };
|
|
|
|
|
plans = mkOption {
|
|
|
|
|
default = {};
|
2016-02-06 17:54:01 +00:00
|
|
|
|
type = types.attrsOf (types.submodule ({ config, ... }: {
|
2015-12-28 18:43:31 +00:00
|
|
|
|
options = {
|
2021-11-08 03:47:47 +00:00
|
|
|
|
enable = mkEnableOption "krebs.backup.${config._module.args.name}" // {
|
2016-02-07 22:48:34 +00:00
|
|
|
|
default = true;
|
|
|
|
|
};
|
2015-12-28 18:43:31 +00:00
|
|
|
|
method = mkOption {
|
|
|
|
|
type = types.enum ["pull" "push"];
|
|
|
|
|
};
|
|
|
|
|
name = mkOption {
|
|
|
|
|
type = types.str;
|
2016-02-06 17:54:01 +00:00
|
|
|
|
default = config._module.args.name;
|
2021-11-08 03:47:47 +00:00
|
|
|
|
defaultText = "‹name›";
|
2015-12-28 18:43:31 +00:00
|
|
|
|
};
|
|
|
|
|
src = mkOption {
|
|
|
|
|
type = types.krebs.file-location;
|
|
|
|
|
};
|
|
|
|
|
dst = mkOption {
|
|
|
|
|
type = types.krebs.file-location;
|
|
|
|
|
};
|
|
|
|
|
startAt = mkOption {
|
2016-01-05 20:47:55 +00:00
|
|
|
|
default = "hourly";
|
2016-02-07 15:08:07 +00:00
|
|
|
|
type = with types; nullOr str; # TODO systemd.time(7)'s calendar event
|
2015-12-28 18:43:31 +00:00
|
|
|
|
};
|
|
|
|
|
snapshots = mkOption {
|
2016-01-05 20:47:55 +00:00
|
|
|
|
default = {
|
|
|
|
|
hourly = { format = "%Y-%m-%dT%H"; retain = 4; };
|
|
|
|
|
daily = { format = "%Y-%m-%d"; retain = 7; };
|
|
|
|
|
weekly = { format = "%YW%W"; retain = 4; };
|
|
|
|
|
monthly = { format = "%Y-%m"; retain = 12; };
|
|
|
|
|
yearly = { format = "%Y"; };
|
|
|
|
|
};
|
2015-12-28 18:43:31 +00:00
|
|
|
|
type = types.attrsOf (types.submodule {
|
|
|
|
|
options = {
|
|
|
|
|
format = mkOption {
|
|
|
|
|
type = types.str; # TODO date's +FORMAT
|
|
|
|
|
};
|
|
|
|
|
retain = mkOption {
|
|
|
|
|
type = types.nullOr types.int;
|
|
|
|
|
default = null; # null = retain all snapshots
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
};
|
2017-07-22 21:01:18 +00:00
|
|
|
|
timerConfig = mkOption {
|
|
|
|
|
type = with types; attrsOf str;
|
|
|
|
|
default = optionalAttrs (config.startAt != null) {
|
|
|
|
|
OnCalendar = config.startAt;
|
|
|
|
|
};
|
|
|
|
|
};
|
2015-12-28 18:43:31 +00:00
|
|
|
|
};
|
|
|
|
|
}));
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
imp = {
|
2016-03-15 14:59:05 +00:00
|
|
|
|
krebs.on-failure.plans =
|
|
|
|
|
listToAttrs (map (plan: nameValuePair "backup.${plan.name}" {
|
|
|
|
|
}) (filter (plan: build-host-is "pull" "dst" plan ||
|
|
|
|
|
build-host-is "push" "src" plan)
|
|
|
|
|
enabled-plans));
|
|
|
|
|
|
2015-12-28 18:43:31 +00:00
|
|
|
|
systemd.services =
|
2016-02-07 15:21:58 +00:00
|
|
|
|
listToAttrs (map (plan: nameValuePair "backup.${plan.name}" {
|
|
|
|
|
# TODO if there is plan.user, then use its privkey
|
|
|
|
|
# TODO push destination users need a similar path
|
|
|
|
|
path = with pkgs; [
|
|
|
|
|
coreutils
|
|
|
|
|
gnused
|
|
|
|
|
openssh
|
|
|
|
|
rsync
|
|
|
|
|
utillinux
|
|
|
|
|
];
|
2017-12-12 17:50:36 +00:00
|
|
|
|
restartIfChanged = false;
|
2016-02-07 15:21:58 +00:00
|
|
|
|
serviceConfig = rec {
|
|
|
|
|
ExecStart = start plan;
|
|
|
|
|
SyslogIdentifier = ExecStart.name;
|
|
|
|
|
Type = "oneshot";
|
|
|
|
|
};
|
|
|
|
|
}) (filter (plan: build-host-is "pull" "dst" plan ||
|
|
|
|
|
build-host-is "push" "src" plan)
|
2016-02-07 22:48:34 +00:00
|
|
|
|
enabled-plans));
|
2015-12-28 18:43:31 +00:00
|
|
|
|
|
2017-07-22 21:01:18 +00:00
|
|
|
|
systemd.timers =
|
|
|
|
|
listToAttrs (map (plan: nameValuePair "backup.${plan.name}" {
|
|
|
|
|
wantedBy = [ "timers.target" ];
|
|
|
|
|
timerConfig = plan.timerConfig;
|
|
|
|
|
}) (filter (plan: plan.timerConfig != {} && (
|
|
|
|
|
build-host-is "pull" "dst" plan ||
|
|
|
|
|
build-host-is "push" "src" plan))
|
|
|
|
|
enabled-plans));
|
|
|
|
|
|
2016-02-07 15:21:58 +00:00
|
|
|
|
users.groups.backup.gid = genid "backup";
|
|
|
|
|
users.users.root.openssh.authorizedKeys.keys =
|
|
|
|
|
map (plan: getAttr plan.method {
|
|
|
|
|
push = plan.src.host.ssh.pubkey;
|
|
|
|
|
pull = plan.dst.host.ssh.pubkey;
|
|
|
|
|
}) (filter (plan: build-host-is "pull" "src" plan ||
|
|
|
|
|
build-host-is "push" "dst" plan)
|
2016-02-07 22:48:34 +00:00
|
|
|
|
enabled-plans);
|
2015-12-28 18:43:31 +00:00
|
|
|
|
};
|
|
|
|
|
|
2016-02-07 22:48:34 +00:00
|
|
|
|
enabled-plans = filter (getAttr "enable") (attrValues cfg.plans);
|
|
|
|
|
|
2016-02-07 15:21:58 +00:00
|
|
|
|
build-host-is = method: side: plan:
|
|
|
|
|
plan.method == method &&
|
|
|
|
|
config.krebs.build.host.name == plan.${side}.host.name;
|
2015-12-28 18:43:31 +00:00
|
|
|
|
|
2016-04-17 08:25:59 +00:00
|
|
|
|
start = plan: let
|
|
|
|
|
login-name = "root";
|
|
|
|
|
identity = local.host.ssh.privkey.path;
|
|
|
|
|
ssh = "ssh -i ${shell.escape identity}";
|
|
|
|
|
local = getAttr plan.method {
|
|
|
|
|
push = plan.src // { rsync = src-rsync; };
|
|
|
|
|
pull = plan.dst // { rsync = dst-rsync; };
|
|
|
|
|
};
|
|
|
|
|
remote = getAttr plan.method {
|
|
|
|
|
push = plan.dst // { rsync = dst-rsync; };
|
|
|
|
|
pull = plan.src // { rsync = src-rsync; };
|
|
|
|
|
};
|
|
|
|
|
src-rsync = "rsync";
|
|
|
|
|
dst-rsync = concatStringsSep " && " [
|
|
|
|
|
"stat ${shell.escape plan.dst.path} >/dev/null"
|
|
|
|
|
"mkdir -m 0700 -p ${shell.escape plan.dst.path}/current"
|
|
|
|
|
"flock -n ${shell.escape plan.dst.path} rsync"
|
|
|
|
|
];
|
2016-06-13 00:04:22 +00:00
|
|
|
|
in pkgs.writeBash "backup.${plan.name}" ''
|
2016-02-07 15:21:58 +00:00
|
|
|
|
set -efu
|
2016-04-17 08:25:59 +00:00
|
|
|
|
start_date=$(date +%s)
|
|
|
|
|
ssh_target=${shell.escape login-name}@$(${fastest-address remote.host})
|
2016-02-07 15:21:58 +00:00
|
|
|
|
${getAttr plan.method {
|
|
|
|
|
push = ''
|
2016-04-17 08:25:59 +00:00
|
|
|
|
rsync_src=${shell.escape plan.src.path}
|
|
|
|
|
rsync_dst=$ssh_target:${shell.escape plan.dst.path}
|
|
|
|
|
echo >&2 "update snapshot current; $rsync_src -> $rsync_dst"
|
2016-02-07 15:21:58 +00:00
|
|
|
|
'';
|
|
|
|
|
pull = ''
|
2016-04-17 08:25:59 +00:00
|
|
|
|
rsync_src=$ssh_target:${shell.escape plan.src.path}
|
|
|
|
|
rsync_dst=${shell.escape plan.dst.path}
|
|
|
|
|
echo >&2 "update snapshot current; $rsync_dst <- $rsync_src"
|
2016-02-07 15:21:58 +00:00
|
|
|
|
'';
|
|
|
|
|
}}
|
2016-12-22 19:53:04 +00:00
|
|
|
|
# In `dst-rsync`'s `mkdir m 0700 -p` above, we care only about permission
|
|
|
|
|
# of the deepest directory:
|
|
|
|
|
# shellcheck disable=SC2174
|
2016-04-17 08:25:59 +00:00
|
|
|
|
${local.rsync} >&2 \
|
2022-01-05 01:54:53 +00:00
|
|
|
|
-aAX --delete \
|
|
|
|
|
--filter='dir-merge /.backup-filter' \
|
2016-04-17 08:25:59 +00:00
|
|
|
|
--rsh=${shell.escape ssh} \
|
|
|
|
|
--rsync-path=${shell.escape remote.rsync} \
|
|
|
|
|
--link-dest=${shell.escape plan.dst.path}/current \
|
|
|
|
|
"$rsync_src/" \
|
|
|
|
|
"$rsync_dst/.partial"
|
|
|
|
|
|
|
|
|
|
dst_exec() {
|
|
|
|
|
${getAttr plan.method {
|
|
|
|
|
push = ''exec ${ssh} "$ssh_target" -T "exec$(printf ' %q' "$@")"'';
|
|
|
|
|
pull = ''exec "$@"'';
|
|
|
|
|
}}
|
|
|
|
|
}
|
2016-04-17 01:50:49 +00:00
|
|
|
|
dst_exec env \
|
|
|
|
|
start_date="$start_date" \
|
2016-04-17 08:25:59 +00:00
|
|
|
|
flock -n ${shell.escape plan.dst.path} \
|
2016-04-17 01:50:49 +00:00
|
|
|
|
/bin/sh < ${toFile "backup.${plan.name}.take-snapshots" ''
|
2015-12-28 18:43:31 +00:00
|
|
|
|
set -efu
|
2016-04-17 08:25:59 +00:00
|
|
|
|
: $start_date
|
|
|
|
|
|
|
|
|
|
dst_path=${shell.escape plan.dst.path}
|
2015-12-28 18:43:31 +00:00
|
|
|
|
|
2016-04-17 01:51:55 +00:00
|
|
|
|
mv "$dst_path/current" "$dst_path/.previous"
|
|
|
|
|
mv "$dst_path/.partial" "$dst_path/current"
|
|
|
|
|
rm -fR "$dst_path/.previous"
|
2015-12-28 18:43:31 +00:00
|
|
|
|
echo >&2
|
|
|
|
|
|
2016-02-07 15:21:58 +00:00
|
|
|
|
snapshot() {(
|
|
|
|
|
: $ns $format $retain
|
|
|
|
|
name=$(date --date="@$start_date" +"$format")
|
2016-04-17 01:51:55 +00:00
|
|
|
|
if ! test -e "$dst_path/$ns/$name"; then
|
2016-02-07 15:21:58 +00:00
|
|
|
|
echo >&2 "create snapshot: $ns/$name"
|
2016-04-17 01:51:55 +00:00
|
|
|
|
mkdir -m 0700 -p "$dst_path/$ns"
|
2016-02-07 15:21:58 +00:00
|
|
|
|
rsync >&2 \
|
2022-01-05 01:54:53 +00:00
|
|
|
|
-aAX --delete \
|
|
|
|
|
--filter='dir-merge /.backup-filter' \
|
2016-04-17 01:51:55 +00:00
|
|
|
|
--link-dest="$dst_path/current" \
|
|
|
|
|
"$dst_path/current/" \
|
|
|
|
|
"$dst_path/$ns/.partial.$name"
|
|
|
|
|
mv "$dst_path/$ns/.partial.$name" "$dst_path/$ns/$name"
|
2016-02-07 15:21:58 +00:00
|
|
|
|
echo >&2
|
|
|
|
|
fi
|
|
|
|
|
case $retain in
|
|
|
|
|
([0-9]*)
|
|
|
|
|
delete_from=$(($retain + 1))
|
2016-04-17 01:51:55 +00:00
|
|
|
|
ls -r "$dst_path/$ns" \
|
2016-02-07 15:21:58 +00:00
|
|
|
|
| sed -n "$delete_from,\$p" \
|
|
|
|
|
| while read old_name; do
|
|
|
|
|
echo >&2 "delete snapshot: $ns/$old_name"
|
2016-04-17 01:51:55 +00:00
|
|
|
|
rm -fR "$dst_path/$ns/$old_name"
|
2016-02-07 15:21:58 +00:00
|
|
|
|
done
|
|
|
|
|
;;
|
|
|
|
|
(ALL)
|
|
|
|
|
:
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
${concatStringsSep "\n" (mapAttrsToList (ns: { format, retain, ... }:
|
|
|
|
|
toString (map shell.escape [
|
|
|
|
|
"ns=${ns}"
|
|
|
|
|
"format=${format}"
|
|
|
|
|
"retain=${if retain == null then "ALL" else toString retain}"
|
|
|
|
|
"snapshot"
|
|
|
|
|
]))
|
|
|
|
|
plan.snapshots)}
|
|
|
|
|
''}
|
2015-12-28 18:43:31 +00:00
|
|
|
|
'';
|
|
|
|
|
|
2016-02-07 04:09:56 +00:00
|
|
|
|
# XXX Is one ping enough to determine fastest address?
|
|
|
|
|
fastest-address = host: ''
|
2021-01-21 16:29:01 +00:00
|
|
|
|
{ ${pkgs.fping}/bin/fping </dev/null -a -e \
|
2016-02-07 04:09:56 +00:00
|
|
|
|
${concatMapStringsSep " " shell.escape
|
2016-02-07 23:31:26 +00:00
|
|
|
|
(mapAttrsToList (_: net: head net.aliases) host.nets)} \
|
2021-01-21 16:29:01 +00:00
|
|
|
|
| ${pkgs.gnused}/bin/sed -r 's/^(\S+) \(([0-9.]+) ms\)$/\2\t\1/' \
|
|
|
|
|
| ${pkgs.coreutils}/bin/sort -n \
|
|
|
|
|
| ${pkgs.coreutils}/bin/cut -f2 \
|
|
|
|
|
| ${pkgs.coreutils}/bin/head -n 1
|
|
|
|
|
}
|
2016-02-07 04:09:56 +00:00
|
|
|
|
'';
|
|
|
|
|
|
2015-12-28 18:43:31 +00:00
|
|
|
|
in out
|
2016-01-05 21:57:16 +00:00
|
|
|
|
# TODO ionice
|
|
|
|
|
# TODO mail on missing push
|
|
|
|
|
# TODO don't cancel plans on activation
|
|
|
|
|
# also, don't hang while deploying at:
|
|
|
|
|
# starting the following units: backup.wu-home-xu.push.service, backup.wu-home-xu.push.timer
|
|
|
|
|
# TODO make sure that secure hosts cannot backup to insecure ones
|
|
|
|
|
# TODO optionally only backup when src and dst are near enough :)
|
|
|
|
|
# TODO try using btrfs for snapshots (configurable)
|
|
|
|
|
# TODO warn if partial snapshots are found
|
|
|
|
|
# TODO warn if unknown stuff is found in dst path
|