59 lines
2.0 KiB
Bash
Executable File
59 lines
2.0 KiB
Bash
Executable File
#!/bin/sh -e
|
|
|
|
BACKUP_PATH=${BACKUP_PATH:-$1}
|
|
INSTANCE="$(echo "${INSTANCE:-${TAG}}" | envsubst)"
|
|
KEEP_ARGS=${KEEP_ARGS:-"--keep-hourly 12 --keep-daily 7 --keep-monthly 3"}
|
|
RESTIC_REPOSITORY="$(echo "${RESTIC_REPOSITORY}" | envsubst)"
|
|
export RESTIC_REPOSITORY
|
|
|
|
PUSHGATEWAY_URL=${PUSHGATEWAY_URL:-"http://prometheus-pushgateway.monitoring.svc.cluster.local:9091"}
|
|
|
|
log_info() {
|
|
echo "[INFO] $*"
|
|
}
|
|
|
|
log_warn() {
|
|
echo "[WARN] $*" >&2
|
|
}
|
|
|
|
echo > /tmp/metrics
|
|
ts_start=$(date +%s)
|
|
|
|
log_info "Initializing restic"
|
|
ts=$(date +%s)
|
|
restic cat config >/dev/null || {
|
|
log_warn "Repository ${RESTIC_REPOSITORY} does not exist, creating";
|
|
restic init;
|
|
}
|
|
echo "restic_init_duration $(echo "$(date +%s) - $ts" | bc)" >> /tmp/metrics
|
|
|
|
log_info "Cleaning stale locks"
|
|
ts=$(date +%s)
|
|
restic unlock
|
|
echo "restic_unlock_duration $(echo "$(date +%s) - $ts" | bc)" >> /tmp/metrics
|
|
|
|
log_info "Running restic backup"
|
|
ts=$(date +%s)
|
|
restic backup --verbose --no-scan --tag "${TAG}" ${BACKUP_PATH}
|
|
echo "restic_backup_duration $(echo "$(date +%s) - $ts" | bc)" >> /tmp/metrics
|
|
|
|
log_info "Cleaning up old snapshots"
|
|
ts=$(date +%s)
|
|
restic forget --prune --tag "${TAG}" -g paths,tags ${KEEP_ARGS}
|
|
echo "restic_forget_duration $(echo "$(date +%s) - $ts" | bc)" >> /tmp/metrics
|
|
|
|
|
|
# Metrics
|
|
log_info "Collecting metrics"
|
|
ts=$(date +%s)
|
|
restic stats --json latest | tee /tmp/stats.json
|
|
restic snapshots --json latest | tee /tmp/snapshots.json
|
|
echo "restic_stats_duration $(echo "$(date +%s) - $ts" | bc)" >> /tmp/metrics
|
|
|
|
echo "restic_total_duration $(echo "$(date +%s) - $ts_start" | bc)" >> /tmp/metrics
|
|
|
|
log_info "Sending metrics to pushgateway"
|
|
jq -r '"restic_stats_total_size_bytes \(.total_size)\nrestic_stats_total_file_count \(.total_file_count)"' < /tmp/stats.json >> /tmp/metrics
|
|
jq -r 'max_by(.time) | .time | sub(".[0-9]+Z$"; "Z") | fromdate | "restic_stats_last_snapshot_timestamp \(.)"' < /tmp/snapshots.json >> /tmp/metrics
|
|
curl --data-binary @- "${PUSHGATEWAY_URL}/metrics/job/backup/instance/${INSTANCE}" < /tmp/metrics
|