How to Back Up and Archive Prometheus Data Using "prometheus-collect-db.sh"

How to Back Up and Archive Prometheus Data Using "prometheus-collect-db.sh"

Overview

The prometheus-collect-db.sh script is designed to collect and archive Prometheus data based on specified time ranges. It supports archiving data within a specified date range and splitting the archive into parts of a defined size. The script can also split the resulting archive into multiple parts of a defined size.

Output archives are automatically named using the date range of the extracted data. If the archive is split, part identifiers are appended to each file.

Prerequisties

Before running the script, ensure the following permissions are in place:

  • Read access to the /var/lib/prometheus/data directory.

  • Write access to the directory where the backup archive will be created.

Usage

./prometheus-collect-db.sh [-s start_date] [-e end_date] [-p period] [-b part_size]

Options

  • By Date Range

    • -s start_date: Start date for the data collection.

    • -e end_date: End date for the data collection.

  • Or by Period

    • -p period: Specifies the data collection period. Use 'd' for days, 'w' for weeks, 'm' for months, 'y' for years.

  • Part Size (optional)

    • -b part_size: Specifies the size of each split part for the archive. If omitted, the default size of 800M will be used.

Example Usages

Requirement

Command

Requirement

Command

Collect data from 1 March 2023 to 31 March 2023

./prometheus-collect-db.sh -s 2023-03-01 -e 2023-03-31

Collect data from the past 7 days

./prometheus-collect-db.sh -p 7d

Collect data for 5 weeks, split into 800 MB parts

./prometheus-collect-db.sh -p 5w -b

Collect data for 1 month, split into 1 GB parts

./prometheus-collect-db.sh -p 1m -b 1G

Splits archive into 1GB parts:

- prometheus-db_<start_date>_<end_date>.tar.gz.01_of_N.tar.gz
- prometheus-db_<start_date>_<end_date>.tar.gz.02_of_N.tar.gz

Output Details

The script will provide real-time feedback on which blocks are being considered for the backup based on the given date range. At the end of its execution, a tarball (.tar.gz) will be created with the following naming convention:

prometheus-db_START-DATE_END-DATE.tar.gz

The tarball will contain:

  1. Relevant Prometheus database blocks from the specified date range.

  2. Prometheus configuration (prometheus.yml).

  3. Prometheus service file (prometheus.service).

  4. AlertManager data.

Extracting Split Archive Tarballs

To extract a split archive generated by the script, follow these steps:

  1. Make sure all parts of the split archive are in the same directory.

  2. Use the cat command to concatenate all split parts into a single file:

    cat prometheus-db_<date>_*.tar.gz > combined.tar.gz
  3. Extract the contents of the combined tarball using the tar command:

    tar xvf combined.tar.gz

Notes

  • The script creates split archive tarballs when the -b flag is used with a specified value. To extract these split files, combine the split parts into a single file using cat before extraction with tar.

  • Ensure that the split parts have the same prefix and are sequentially named for proper concatenation and extraction.

Tips

  1. Ensure you specify either a valid start and end date or a period. Not providing either will result in an error.

  2. To avoid unnecessary disk space usage, move or store the resulting tarball to a backup or storage solution as soon as possible.

Conclusion

The prometheus-collect-db.sh script is a valuable tool for system administrators and Prometheus operators who wish to back up or transfer their Prometheus time-series data. Proper usage of this script ensures that crucial metrics and alert data are not lost and can be restored or analyzed when needed.

Reference: Import Prometheus metrics from another SwarmTelemetry VM