## Summary - Adds a second borgmatic config (`photos.yaml`) that backs up `/Volumes/photos` (sifaka SMB mount, ~128 GB) to a dedicated BorgBase repo (`immich-photos`), running daily at 4 AM - Separate launchd agent (`mcquack.eblume.borgmatic-photos`) so photo backups run independently from the main backup - Refactors `borgmatic_metrics` script to support multiple repos with a `repo` Prometheus label - Updates Grafana "Borg Backups" dashboard with a `repo` template variable so you can filter/compare repos - Docs updated: `backups.md`, `borgmatic.md` ## Prerequisites (manual) - [x] Create `immich-photos` repo on BorgBase with same SSH key - [ ] Upgrade BorgBase plan to Small ($24/yr) if currently on free tier (128 GB exceeds 10 GB limit) - [ ] After deploy: `borg init` the new repo (borgmatic does this automatically on first run) ## Test plan - [ ] Dry run: `mise run provision-indri -- --check --diff --tags borgmatic,borgmatic_metrics` - [ ] Deploy borgmatic role and verify both configs deployed - [ ] Run `borgmatic --config ~/.config/borgmatic/photos.yaml create --verbosity 1` manually for first backup (will take hours) - [ ] Verify metrics script collects from both repos: `~/.local/bin/borgmatic-metrics && cat /opt/homebrew/var/node_exporter/textfile/borgmatic.prom` - [ ] Sync grafana-config in ArgoCD and verify dashboard repo selector works 🤖 Generated with [Claude Code](https://claude.com/claude-code) Reviewed-on: #315
152 lines
7.1 KiB
Django/Jinja
152 lines
7.1 KiB
Django/Jinja
#!/bin/bash
|
|
# {{ ansible_managed }}
|
|
# Collects borg backup metrics for node_exporter textfile collector
|
|
# Supports multiple repositories with a repo label for Prometheus
|
|
|
|
set -euo pipefail
|
|
|
|
export BORG_PASSCOMMAND="{{ borgmatic_metrics_passcommand }}"
|
|
export BORG_RSH="ssh -o IdentitiesOnly=yes -i {{ borgmatic_metrics_ssh_key }}"
|
|
OUTPUT_FILE="{{ borgmatic_metrics_dir }}/borgmatic.prom"
|
|
TEMP_FILE="${OUTPUT_FILE}.tmp"
|
|
|
|
# Use absolute paths for LaunchAgent compatibility
|
|
BORG_CMD="/opt/homebrew/bin/borg"
|
|
JQ_CMD="/opt/homebrew/bin/jq"
|
|
|
|
# Start fresh
|
|
cat > "$TEMP_FILE" << 'EOF'
|
|
# HELP borgmatic_up Borg backup repository is accessible
|
|
# TYPE borgmatic_up gauge
|
|
# HELP borgmatic_repo_original_size_bytes Total original size of all archives (sum of what each backup contains)
|
|
# TYPE borgmatic_repo_original_size_bytes gauge
|
|
# HELP borgmatic_repo_compressed_size_bytes Total compressed size of all archives
|
|
# TYPE borgmatic_repo_compressed_size_bytes gauge
|
|
# HELP borgmatic_repo_deduplicated_size_bytes Actual disk usage after deduplication (unique data)
|
|
# TYPE borgmatic_repo_deduplicated_size_bytes gauge
|
|
# HELP borgmatic_repo_total_chunks Total number of chunks across all archives
|
|
# TYPE borgmatic_repo_total_chunks gauge
|
|
# HELP borgmatic_repo_unique_chunks Number of unique chunks (after deduplication)
|
|
# TYPE borgmatic_repo_unique_chunks gauge
|
|
# HELP borgmatic_archive_count Number of archives in the repository
|
|
# TYPE borgmatic_archive_count gauge
|
|
# HELP borgmatic_last_archive_original_size_bytes Original size of the last archive (data being backed up)
|
|
# TYPE borgmatic_last_archive_original_size_bytes gauge
|
|
# HELP borgmatic_last_archive_compressed_size_bytes Compressed size of the last archive
|
|
# TYPE borgmatic_last_archive_compressed_size_bytes gauge
|
|
# HELP borgmatic_last_archive_deduplicated_size_bytes Deduplicated size of last archive (new data added)
|
|
# TYPE borgmatic_last_archive_deduplicated_size_bytes gauge
|
|
# HELP borgmatic_last_archive_files Number of files in the last archive
|
|
# TYPE borgmatic_last_archive_files gauge
|
|
# HELP borgmatic_last_archive_timestamp Unix timestamp of the last backup
|
|
# TYPE borgmatic_last_archive_timestamp gauge
|
|
# HELP borgmatic_last_archive_duration_seconds Duration of the last backup in seconds
|
|
# TYPE borgmatic_last_archive_duration_seconds gauge
|
|
# HELP borgmatic_source_size_bytes Size of each backup source directory in bytes
|
|
# TYPE borgmatic_source_size_bytes gauge
|
|
EOF
|
|
|
|
collect_repo_metrics() {
|
|
local repo_path="$1"
|
|
local repo_label="$2"
|
|
|
|
# Get repository info
|
|
repo_json=$($BORG_CMD info --json "$repo_path" 2>/dev/null) || {
|
|
echo "Failed to get borg repo info for $repo_label" >&2
|
|
echo "borgmatic_up{repo=\"$repo_label\"} 0" >> "$TEMP_FILE"
|
|
return
|
|
}
|
|
|
|
# Get archive list
|
|
archives_json=$($BORG_CMD list --json "$repo_path" 2>/dev/null) || {
|
|
echo "Failed to list borg archives for $repo_label" >&2
|
|
echo "borgmatic_up{repo=\"$repo_label\"} 0" >> "$TEMP_FILE"
|
|
return
|
|
}
|
|
|
|
# Extract repository stats
|
|
total_size=$(echo "$repo_json" | $JQ_CMD -r '.cache.stats.total_size')
|
|
total_csize=$(echo "$repo_json" | $JQ_CMD -r '.cache.stats.total_csize')
|
|
unique_size=$(echo "$repo_json" | $JQ_CMD -r '.cache.stats.unique_size')
|
|
unique_csize=$(echo "$repo_json" | $JQ_CMD -r '.cache.stats.unique_csize')
|
|
total_chunks=$(echo "$repo_json" | $JQ_CMD -r '.cache.stats.total_chunks')
|
|
unique_chunks=$(echo "$repo_json" | $JQ_CMD -r '.cache.stats.total_unique_chunks')
|
|
archive_count=$(echo "$archives_json" | $JQ_CMD -r '.archives | length')
|
|
|
|
cat >> "$TEMP_FILE" << EOF
|
|
borgmatic_up{repo="$repo_label"} 1
|
|
borgmatic_repo_original_size_bytes{repo="$repo_label"} $total_size
|
|
borgmatic_repo_compressed_size_bytes{repo="$repo_label"} $total_csize
|
|
borgmatic_repo_deduplicated_size_bytes{repo="$repo_label"} $unique_csize
|
|
borgmatic_repo_total_chunks{repo="$repo_label"} $total_chunks
|
|
borgmatic_repo_unique_chunks{repo="$repo_label"} $unique_chunks
|
|
borgmatic_archive_count{repo="$repo_label"} $archive_count
|
|
EOF
|
|
|
|
# Get last archive info
|
|
last_archive_name=$(echo "$archives_json" | $JQ_CMD -r '.archives[-1].name // empty')
|
|
|
|
if [ -z "$last_archive_name" ]; then
|
|
return
|
|
fi
|
|
|
|
# Get detailed info for the last archive
|
|
last_archive_json=$($BORG_CMD info --json "${repo_path}::${last_archive_name}" 2>/dev/null) || {
|
|
echo "Failed to get last archive info for $repo_label" >&2
|
|
return
|
|
}
|
|
|
|
last_original_size=$(echo "$last_archive_json" | $JQ_CMD -r '.archives[0].stats.original_size')
|
|
last_compressed_size=$(echo "$last_archive_json" | $JQ_CMD -r '.archives[0].stats.compressed_size')
|
|
last_deduplicated_size=$(echo "$last_archive_json" | $JQ_CMD -r '.archives[0].stats.deduplicated_size')
|
|
last_nfiles=$(echo "$last_archive_json" | $JQ_CMD -r '.archives[0].stats.nfiles')
|
|
last_start=$(echo "$last_archive_json" | $JQ_CMD -r '.archives[0].start')
|
|
last_duration=$(echo "$last_archive_json" | $JQ_CMD -r '.archives[0].duration')
|
|
|
|
# Convert timestamp to unix epoch
|
|
last_timestamp=$(date -j -f "%Y-%m-%dT%H:%M:%S" "${last_start%.*}" "+%s" 2>/dev/null || echo "0")
|
|
|
|
cat >> "$TEMP_FILE" << EOF
|
|
borgmatic_last_archive_original_size_bytes{repo="$repo_label"} $last_original_size
|
|
borgmatic_last_archive_compressed_size_bytes{repo="$repo_label"} $last_compressed_size
|
|
borgmatic_last_archive_deduplicated_size_bytes{repo="$repo_label"} $last_deduplicated_size
|
|
borgmatic_last_archive_files{repo="$repo_label"} $last_nfiles
|
|
borgmatic_last_archive_timestamp{repo="$repo_label"} $last_timestamp
|
|
borgmatic_last_archive_duration_seconds{repo="$repo_label"} ${last_duration:-0}
|
|
EOF
|
|
|
|
# Collect per-source-directory sizes
|
|
$BORG_CMD list "${repo_path}::${last_archive_name}" --format "{size} {path}{NL}" 2>/dev/null | awk -v repo="$repo_label" '
|
|
{
|
|
size = $1
|
|
path = $2
|
|
# Map paths to friendly source names
|
|
if (path ~ /^Users\/[^\/]+\/Pictures/) { source = "Pictures" }
|
|
else if (path ~ /^Users\/[^\/]+\/Documents/) { source = "Documents" }
|
|
else if (path ~ /^Users\/[^\/]+\/devpi/) { source = "devpi" }
|
|
else if (path ~ /^Users\/[^\/]+\/code\/personal\/zk/) { source = "Zettelkasten" }
|
|
else if (path ~ /^Users\/[^\/]+\/.config\/borgmatic/) { source = "borgmatic_config" }
|
|
else if (path ~ /^Users\/[^\/]+\/.local\/share\/borgmatic/) { source = "k8s_dumps" }
|
|
else if (path ~ /^opt\/homebrew\/var\/forgejo/) { source = "Forgejo" }
|
|
else if (path ~ /^opt\/homebrew\/var\/loki/) { source = "Loki" }
|
|
else if (path ~ /^Volumes\/photos/) { source = "immich_photos" }
|
|
else if (path ~ /^borgmatic\/postgresql_databases/) { source = "PostgreSQL" }
|
|
else if (path ~ /^borgmatic\//) { source = "borgmatic_metadata" }
|
|
else { source = "other" }
|
|
|
|
totals[source] += size
|
|
}
|
|
END {
|
|
for (src in totals) {
|
|
printf "borgmatic_source_size_bytes{repo=\"%s\",source=\"%s\"} %.0f\n", repo, src, totals[src]
|
|
}
|
|
}' >> "$TEMP_FILE"
|
|
}
|
|
|
|
# Collect metrics for each configured repository
|
|
{% for repo in borgmatic_metrics_repos %}
|
|
collect_repo_metrics "{{ repo.path }}" "{{ repo.label }}"
|
|
{% endfor %}
|
|
|
|
# Atomic move
|
|
mv "$TEMP_FILE" "$OUTPUT_FILE"
|