iac/nixos/hosts/asxpio-services/replica.nix
Sergei Poljanski e27baf32ee
All checks were successful
nixos / nixos (push) Successful in 33s
replica: give rclone a home directory
rclone looked for one and logged three errors per bucket on every run,
though its remotes come from the environment.

Assisted-by: Claude:opus-5-5
2026-11-17 10:06:13 +04:00

94 lines
3.1 KiB
Nix

# Daily one-way replica of the hel1 buckets into their fsn1 twins.
# restic/ is left out: restic writes its own, independent fsn1 repository.
{
config,
lib,
pkgs,
...
}:
let
pairs = [
{
src = "asxpio-backups-main";
dst = "asxpio-backups-repl";
exclude = [ "restic/**" ];
}
{
src = "asxpio-tofu-main"; # OpenTofu state
dst = "asxpio-tofu-repl";
}
{
src = "asxpio-loki-main"; # Loki's chunks
dst = "asxpio-loki-repl";
}
{
src = "k8s-asxpio-main";
dst = "k8s-asxpio-repl";
}
];
# A run that would delete more than this stops and fails (and mails):
# a mistake on the hel1 side must not empty the replica the next morning
maxDelete = 1000;
textfileDir = "/var/lib/node_exporter/textfile";
in
{
sops.templates."s3-replicate.env".content =
let
remote = name: region: ''
RCLONE_CONFIG_${name}_TYPE=s3
RCLONE_CONFIG_${name}_PROVIDER=Other
RCLONE_CONFIG_${name}_ENDPOINT=https://${region}.your-objectstorage.com
RCLONE_CONFIG_${name}_REGION=${region}
RCLONE_CONFIG_${name}_ACCESS_KEY_ID=${config.sops.placeholder."s3/access_key"}
RCLONE_CONFIG_${name}_SECRET_ACCESS_KEY=${config.sops.placeholder."s3/secret_key"}
'';
in
remote "HEL1" "hel1"
+ remote "FSN1" "fsn1"
+ ''
RCLONE_CONFIG_FSN1_NO_CHECK_BUCKET=true
'';
systemd.services.s3-replicate = {
description = "Replicate the hel1 buckets to fsn1";
wants = [ "network-online.target" ];
after = [ "network-online.target" ];
onFailure = [ "alert-mail@%n.service" ];
path = [ pkgs.rclone ];
# rclone looks for a home directory even when its remotes come from
# the environment
environment.HOME = "/var/lib/s3-replicate";
serviceConfig = {
Type = "oneshot";
StateDirectory = "s3-replicate";
EnvironmentFile = config.sops.templates."s3-replicate.env".path;
TimeoutStartSec = "12h";
};
# One bucket at a time; a failing pair does not stop the others, but
# fails the run
script = ''
failed=0
${lib.concatMapStrings (p: ''
echo "s3-replicate: ${p.src} -> ${p.dst}"
rclone sync hel1:${p.src} fsn1:${p.dst} \
--fast-list --checkers 16 --transfers 8 --max-delete ${toString maxDelete} \
${lib.concatMapStringsSep " " (e: "--exclude ${lib.escapeShellArg e}") (p.exclude or [ ])} \
--stats-one-line -v || failed=1
'') pairs}
if [ $failed = 0 ] && [ -d ${textfileDir} ]; then
f=${textfileDir}/s3_replicate.prom
printf '# HELP s3_replicate_last_success_timestamp_seconds Last fully successful hel1 to fsn1 replica.\n# TYPE s3_replicate_last_success_timestamp_seconds gauge\ns3_replicate_last_success_timestamp_seconds %s\n' "$(date +%s)" > "$f.tmp" && mv "$f.tmp" "$f"
fi
exit $failed
'';
};
systemd.timers.s3-replicate = {
description = "Daily hel1 -> fsn1 bucket replica";
wantedBy = [ "timers.target" ];
timerConfig = {
OnCalendar = "*-*-* 06:00:00";
RandomizedDelaySec = "15m";
Persistent = true;
};
};
}