Skip to content

Instantly share code, notes, and snippets.

@schnapster
Last active June 23, 2024 16:35
Show Gist options
  • Save schnapster/b6a70f426f2b0e408d1032d87f31a18b to your computer and use it in GitHub Desktop.
Save schnapster/b6a70f426f2b0e408d1032d87f31a18b to your computer and use it in GitHub Desktop.
Backup Prometheus v2.1+ snapshots to b2 backblaze. Requires b2, curl, jq, tar
#!/bin/bash
#
# Prerequisites:
# sudo apt install pipx curl jq tar
# sudo pipx install b2
#
# Enabled admin api in your prometheus, see: https://prometheus.io/docs/prometheus/2.1/querying/api/#tsdb-admin-apis
#
# pass 6 args:
# - prometheus base url, example: http://localhost:9090
# - prometheus data path, example: /opt/prometheus/data
# - instance (meta information), example: <machine name>
# - b2 bucket name (target of the upload), example: prom-backups
# - backblaze account id
# - backblaze app key
#
# Full example: ./prometheus_b2_backup.sh "http://localhost:9090" "/opt/prometheus/data" "$(hostname)" prom-backups foo bar
set -e
echo $(date)
# Verify we are root
if [ "$EUID" -ne 0 ]; then
echo "Please run as root"
exit 1
fi
# Read arguments
URL=$1
DATA_PATH=$2
INSTANCE=$3
BUCKET=$4
source $5
#B2_ACCOUNT_ID=$5
#B2_APP_KEY=$6
echo "Backing up prometheus ${INSTANCE} located at ${URL} to bucket ${BUCKET}"
if [ ! -d "${DATA_PATH}" ]; then
echo "Data directory ${DATA_PATH} does not exist or is not a directory!"
exit 1
fi
# Prepare directory variables
TMP_DIR="/tmp"
FILE_NAME=prometheus_${INSTANCE}_$(date +%Y-%m-%d).tar.gz
TAR_FILE=${TMP_DIR}/${FILE_NAME}
B2_INFO="--info app=prometheus --info instance=${INSTANCE}"
mkdir -p ${TMP_DIR}
# Clean up any old backups
if [ -f "${TAR_FILE}" ]; then
rm -f "${TAR_FILE}"
fi
# Ask prometheus api to create a snapshot
PAYLOAD=$(curl -XPOST ${URL}/api/v1/admin/tsdb/snapshot)
STATUS=$(echo "${PAYLOAD}" | jq -r .status)
if [ "${STATUS}" != "success" ]; then
echo "Prometheus response not success, but: ${STATUS}"
exit 1
fi
SNAPSHOT_PATH=$(echo "${PAYLOAD}" | jq -r .data.name)
echo "Snapshot created at ${SNAPSHOT_PATH}"
FULL_PATH=${DATA_PATH}/snapshots/${SNAPSHOT_PATH}
if [ ! -d "${FULL_PATH}" ]; then
echo "Snapshot at ${FULL_PATH} does not exist or is not a directory!"
exit 1
fi
echo "Snapshot located at ${FULL_PATH}"
# Create tarball
cd ${DATA_PATH}/snapshots
tar -zcf ${TAR_FILE} ${SNAPSHOT_PATH}
echo "Tarball created at ${TAR_FILE}"
# Calculate sha1 sum
SHA1=$(sha1sum ${TAR_FILE} | sed -En "s/^([0-9a-f]{40}).*/\1/p")
echo "sha1sum is ${SHA1}"
# log in to backblaze
b2 account authorize ${B2_ACCOUNT_ID} ${B2_APP_KEY}
echo "Logged into b2"
# Upload to backblaze
b2 file upload --sha1 ${SHA1} \
${B2_INFO} \
--quiet \
${BUCKET} \
${TAR_FILE} \
${FILE_NAME}
echo "Uploaded to b2"
#log out of backblaze
b2 account clear
echo "Logged out of b2"
# Clean up tar file
if [ -f "${TAR_FILE}" ]; then
rm -f "${TAR_FILE}"
fi
# Clean up snapshot
if [ -d "${FULL_PATH}" ]; then
rm -rf "${FULL_PATH}"
fi
echo "Cleaned up and done"
@franck-grenier
Copy link

Very useful !
Thanks

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