Run restic unlock before backup, fix shellcheck
This commit is contained in:
46
backup.sh
Normal file → Executable file
46
backup.sh
Normal file → Executable file
@@ -1,34 +1,58 @@
|
||||
#!/bin/sh -e
|
||||
|
||||
BACKUP_PATH=${BACKUP_PATH:-$1}
|
||||
INSTANCE="$(echo ${INSTANCE:-${TAG}} | envsubst)"
|
||||
INSTANCE="$(echo "${INSTANCE:-${TAG}}" | envsubst)"
|
||||
KEEP_ARGS=${KEEP_ARGS:-"--keep-hourly 12 --keep-daily 7 --keep-monthly 3"}
|
||||
export RESTIC_REPOSITORY="$(echo ${RESTIC_REPOSITORY} | envsubst)"
|
||||
RESTIC_REPOSITORY="$(echo "${RESTIC_REPOSITORY}" | envsubst)"
|
||||
export RESTIC_REPOSITORY
|
||||
|
||||
echo -n > /tmp/metrics
|
||||
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 || restic init
|
||||
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 backup --verbose --tag ${TAG} ${BACKUP_PATH}
|
||||
restic unlock
|
||||
echo "restic_unlock_duration $(echo "$(date +%s) - $ts" | bc)" >> /tmp/metrics
|
||||
|
||||
log_info "Running restic backup"
|
||||
ts=$(date +%s)
|
||||
restic backup --verbose --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}
|
||||
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 > /tmp/stats.json
|
||||
restic snapshots --json latest > /tmp/snapshots.json
|
||||
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
|
||||
|
||||
cat /tmp/stats.json | jq -r '"restic_stats_total_size_bytes \(.total_size)\nrestic_stats_total_file_count \(.total_file_count)"' >> /tmp/metrics
|
||||
cat /tmp/snapshots.json | jq -r 'max_by(.time) | .time | sub(".[0-9]+Z$"; "Z") | fromdate | "restic_stats_last_snapshot_timestamp \(.)"' >> /tmp/metrics
|
||||
cat /tmp/metrics | curl --data-binary @- "http://prometheus-pushgateway.monitoring.svc.cluster.local:9091/metrics/job/backup/instance/${INSTANCE}"
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user