Skip to content

Instantly share code, notes, and snippets.

@veerendra2
Last active December 9, 2022 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save veerendra2/462264542e3de75b4ffbed7bba8a41b3 to your computer and use it in GitHub Desktop.
Save veerendra2/462264542e3de75b4ffbed7bba8a41b3 to your computer and use it in GitHub Desktop.
A script to run restic backup in cronjob and writes metrics to file for node exporter's textfile collector
#!/bin/bash
# Author: Veerendra K
# Description: Runs restic backup and writes metrics to file for node exporter's textfile collector
# Dependencies: restic, jq
#
# ** INFO ***
# https://www.robustperception.io/using-the-textfile-collector-from-a-shell-script/
# https://github.com/prometheus/node_exporter
# Set --collector.textfile.directory in node_exporter
RESTIC_REPO=""
RESTIC_BACKUP_DIR=""
RESTIC_PASSWORD_FILE=""
TEXTFILE_COLLECTOR_DIR="."
START="$(date +%s)"
restic -r $RESTIC_REPO \
--json \
-p $RESTIC_PASSWORD_FILE \
backup $RESTIC_BACKUP_DIR | tail -1 > /tmp/restic_stats.json
END="$(date +%s)"
cat << EOF > "$TEXTFILE_COLLECTOR_DIR/backup_restic.prom.$$"
backup_duration_seconds $(($END - $START))
backup_last_run_epoch_seconds $END
backup_restic_summary_files_new `jq .files_new /tmp/restic_stats.json`
backup_restic_summary_files_changed `jq .files_changed /tmp/restic_stats.json`
backup_restic_summary_files_unmodified `jq .files_unmodified /tmp/restic_stats.json`
backup_restic_summary_dirs_new `jq .dirs_new /tmp/restic_stats.json`
backup_restic_summary_dirs_changed `jq .dirs_changed /tmp/restic_stats.json`
backup_restic_summary_dirs_unmodified `jq .dirs_unmodified /tmp/restic_stats.json`
backup_restic_summary_data_blobs `jq .data_blobs /tmp/restic_stats.json`
backup_restic_summary_tree_blobs `jq .tree_blobs /tmp/restic_stats.json`
backup_restic_summary_data_added `jq .data_added /tmp/restic_stats.json`
backup_restic_summary_total_files_processed `jq .total_files_processed /tmp/restic_stats.json`
backup_restic_summary_total_bytes_processed `jq .total_bytes_processed /tmp/restic_stats.json`
backup_restic_summary_total_duration `jq .total_duration /tmp/restic_stats.json`
backup_restic_summary_snapshot_id `jq .snapshot_id /tmp/restic_stats.json`
EOF
mv "$TEXTFILE_COLLECTOR_DIR/backup_restic.prom.$$" "$TEXTFILE_COLLECTOR_DIR/backup_restic.prom"
@veerendra2
Copy link
Author

Metrics

backup_duration_seconds 1
backup_last_run_epoch_seconds 1670614247
backup_restic_summary_files_new 2
backup_restic_summary_files_changed 4
backup_restic_summary_files_unmodified 864
backup_restic_summary_dirs_new 0
backup_restic_summary_dirs_changed 10
backup_restic_summary_dirs_unmodified 458
backup_restic_summary_data_blobs 3
backup_restic_summary_tree_blobs 9
backup_restic_summary_data_added 29593
backup_restic_summary_total_files_processed 870
backup_restic_summary_total_bytes_processed 96204670
backup_restic_summary_total_duration 0.802369061
backup_restic_summary_snapshot_id "9fc08c2f"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment