Only keep 10 snapshots, and push metrics.
This commit is contained in:
@@ -103,11 +103,14 @@ in
|
||||
] ++ (lib.forEach cfg.standbys (standby: {
|
||||
"replicate-services-to-${standby}" = {
|
||||
description = "Replicate /persist/services to ${standby}";
|
||||
path = [ pkgs.btrfs-progs pkgs.openssh pkgs.coreutils pkgs.findutils pkgs.gnugrep ];
|
||||
path = [ pkgs.btrfs-progs pkgs.openssh pkgs.coreutils pkgs.findutils pkgs.gnugrep pkgs.curl ];
|
||||
|
||||
script = ''
|
||||
set -euo pipefail
|
||||
|
||||
START_TIME=$(date +%s)
|
||||
REPLICATION_SUCCESS=0
|
||||
|
||||
SSH_KEY="/persist/root/.ssh/btrfs-replication"
|
||||
if [ ! -f "$SSH_KEY" ]; then
|
||||
echo "ERROR: SSH key not found at $SSH_KEY"
|
||||
@@ -134,11 +137,13 @@ in
|
||||
ssh -i "$SSH_KEY" -o StrictHostKeyChecking=accept-new root@${standby} \
|
||||
"btrfs receive /persist/services-standby"; then
|
||||
echo "Incremental send completed successfully"
|
||||
REPLICATION_SUCCESS=1
|
||||
else
|
||||
echo "Incremental send failed (likely missing parent on receiver), falling back to full send"
|
||||
btrfs send "$SNAPSHOT_PATH" | \
|
||||
ssh -i "$SSH_KEY" -o StrictHostKeyChecking=accept-new root@${standby} \
|
||||
"btrfs receive /persist/services-standby"
|
||||
REPLICATION_SUCCESS=1
|
||||
fi
|
||||
else
|
||||
# First snapshot, do full send
|
||||
@@ -146,10 +151,28 @@ in
|
||||
btrfs send "$SNAPSHOT_PATH" | \
|
||||
ssh -i "$SSH_KEY" -o StrictHostKeyChecking=accept-new root@${standby} \
|
||||
"btrfs receive /persist/services-standby"
|
||||
REPLICATION_SUCCESS=1
|
||||
fi
|
||||
|
||||
# Cleanup old snapshots on sender (keep last 24 hours = 288 snapshots at 5min intervals)
|
||||
find /persist -maxdepth 1 -name 'services@*' -mmin +1440 -exec btrfs subvolume delete {} \;
|
||||
# Cleanup old snapshots on sender (keep last 10 snapshots, sorted by name/timestamp)
|
||||
ls -1d /persist/services@* 2>/dev/null | sort | head -n -10 | xargs -r btrfs subvolume delete
|
||||
|
||||
# Calculate metrics
|
||||
END_TIME=$(date +%s)
|
||||
DURATION=$((END_TIME - START_TIME))
|
||||
SNAPSHOT_COUNT=$(ls -1d /persist/services@* 2>/dev/null | wc -l)
|
||||
|
||||
# Push metrics to Prometheus pushgateway
|
||||
cat <<METRICS | curl --data-binary @- http://pushgateway.service.consul:9091/metrics/job/nfs_replication/instance/${standby}
|
||||
# TYPE nfs_replication_last_success_timestamp gauge
|
||||
nfs_replication_last_success_timestamp $END_TIME
|
||||
# TYPE nfs_replication_duration_seconds gauge
|
||||
nfs_replication_duration_seconds $DURATION
|
||||
# TYPE nfs_replication_snapshot_count gauge
|
||||
nfs_replication_snapshot_count $SNAPSHOT_COUNT
|
||||
# TYPE nfs_replication_success gauge
|
||||
nfs_replication_success $REPLICATION_SUCCESS
|
||||
METRICS
|
||||
'';
|
||||
|
||||
serviceConfig = {
|
||||
|
||||
Reference in New Issue
Block a user