diff --git a/borgmatic/crontab.txt b/borgmatic/crontab.txt index 76e8d93..53173ab 100644 --- a/borgmatic/crontab.txt +++ b/borgmatic/crontab.txt @@ -1 +1 @@ -0 2 * * * /scripts/run.sh +0 2 * * * /scripts/run_all.sh diff --git a/borgmatic/scripts/mqtt.sh b/borgmatic/scripts/mqtt.sh deleted file mode 100755 index 07bb33a..0000000 --- a/borgmatic/scripts/mqtt.sh +++ /dev/null @@ -1,26 +0,0 @@ -CONFIG_FILE="$1" - -MQTT_BROKER="host" -MQTT_TOPIC="borgmatic/stats/$CONFIG_FILE" -MQTT_USER="borgmatic" -MQTT_PASSWORD="eH2dAiG7siCIFdB7qX0MHwEFzcr9aqUi" - -# Run Borgmatic and extract archive stats as JSON -PAYLOAD=$(borgmatic \ - --stats \ - -v 0 \ - -c "/config/$CONFIG_FILE.yaml" \ - --log-json \ - --no-color \ - --json \ - | tee /dev/tty \ - | sed --quiet '/^\[/p' \ - | jq '.[0].archive' -) - -# Publish to MQTT -mosquitto_pub -h "$MQTT_BROKER" \ - -t "$MQTT_TOPIC" \ - -u "$MQTT_USER" \ - -P "$MQTT_PASSWORD" \ - -m "$PAYLOAD" diff --git a/borgmatic/scripts/run-old.sh b/borgmatic/scripts/run-old.sh deleted file mode 100644 index f60e56c..0000000 --- a/borgmatic/scripts/run-old.sh +++ /dev/null @@ -1,10 +0,0 @@ -borgmatic --stats -v 0 -c /config/home.yaml >> /log/home.log 2>&1 -borgmatic --stats -v 0 -c /config/root.yaml >> /log/root.log 2>&1 -borgmatic --stats -v 0 -c /config/opt.yaml >> /log/opt.log 2>&1 -borgmatic --stats -v 0 -c /config/etc.yaml >> /log/etc.log 2>&1 - -export REMOTE=/remote/server/bram -mkdir -p ${REMOTE} -sshfs user@bram.veenboer.xyz:/media/helios/Bram ${REMOTE} -borgmatic --stats -v 0 -c /config/bram.yaml >> /log/bram.log 2>&1 -umount ${REMOTE} diff --git a/borgmatic/scripts/run.sh b/borgmatic/scripts/run_all.sh similarity index 55% rename from borgmatic/scripts/run.sh rename to borgmatic/scripts/run_all.sh index 596fc06..301cc69 100755 --- a/borgmatic/scripts/run.sh +++ b/borgmatic/scripts/run_all.sh @@ -1,5 +1,5 @@ #!/bin/bash for name in home root opt etc bram; do echo $name - /scripts/mqtt.sh "$name" >> "/log/$name.log" 2>&1 + /scripts/run_one.sh "$name" >> "/log/$name.log" 2>&1 done diff --git a/borgmatic/scripts/run_one.sh b/borgmatic/scripts/run_one.sh new file mode 100755 index 0000000..dd6c5d8 --- /dev/null +++ b/borgmatic/scripts/run_one.sh @@ -0,0 +1,56 @@ +#!/bin/bash +set -euo pipefail + +CONFIG_FILE="$1" + +LOKI_HOST=host + +MQTT_BROKER=host +MQTT_TOPIC="borgmatic/stats/$CONFIG_FILE" +MQTT_USER="borgmatic" +MQTT_PASSWORD="eH2dAiG7siCIFdB7qX0MHwEFzcr9aqUi" + +# Run Borgmatic and extract archive stats as JSON +ARCHIVE_JSON=$(borgmatic \ + --stats \ + -v 0 \ + -c "/config/$CONFIG_FILE.yaml" \ + --log-json \ + --no-color \ + --json \ + | tail -n1 \ + | jq '.[0].archive' +) +echo $ARCHIVE_JSON + +echo "----- ARCHIVE_JSON" +echo $ARCHIVE_JSON +echo "-----" + +# Publish to MQTT +mosquitto_pub -h "$MQTT_BROKER" \ + -t "$MQTT_TOPIC" \ + -u "$MQTT_USER" \ + -P "$MQTT_PASSWORD" \ + -m "$ARCHIVE_JSON" + +# Build Loki payload +LOKI_PAYLOAD=$(jq -n \ + --arg config "$CONFIG_FILE" \ + --argjson archive "$ARCHIVE_JSON" \ + --arg now "$(date +%s%N)" \ + '{streams: [ + { + stream: {job: "borgmatic", config: $config}, + values: [[$now, ($archive | tostring)]] + } + ]}') + +echo "----- LOKI_PAYLOAD" +echo $LOKI_PAYLOAD +echo "-----" + +# Send to Loki +curl -s -X POST "http://$LOKI_HOST:3100/loki/api/v1/push" \ + -H "Content-Type: application/json" \ + -d "$LOKI_PAYLOAD"