All checks were successful
nixos / nixos (push) Successful in 34s
The infrastructure is applied with OpenTofu; name it so. terraform/
becomes tofu/, the files end in .tofu (OpenTofu 1.8+ prefers them), the
local variables file is secrets.auto.tfvars, and both servers carry
managed_by=opentofu (an in-place label change).
The state moved to new buckets asxpio-tofu-main/-repl, key
tofu/state/tofu.tfstate, with `tofu init -migrate-state`; a plan against
it shows no changes. The old asxpio-terraform-* buckets stay, renamed
in the config with moved blocks, until the new state has been used for
a while.
asxpio-loki-main, created by hand, is adopted with an import block, and
a new asxpio-loki-repl in fsn1 receives it in the daily replica, which
now also copies the new state bucket instead of the old one.
What keeps the Terraform name is OpenTofu's own: the `terraform {}`
block and .terraform.lock.hcl.
Assisted-by: Claude:opus-5-5
90 lines
2.9 KiB
Nix
90 lines
2.9 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 ];
|
|
serviceConfig = {
|
|
Type = "oneshot";
|
|
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;
|
|
};
|
|
};
|
|
}
|