Skip to main content

High Frequency Backup and Retention

Project description

High Frequency Backup and Retention

Backup important small files under a high frequency schedule, avoiding duplicates, and pruning less relevant snapshots as they get older.

Licensed under the Apache License 2.0.

Installation

uv tool install hfbr

Or, if you prefer pip:

pip install hfbr
  1. Copy example_settings.yaml into settings.yaml.
  2. Edit the newly created Settings File according to your needs, or do some basic testing with CLI Mode.
  3. Add hfbr to your crontab. My schedule is */20 * * * *, which means every 20 minutes.

Settings File

The settings file is a YAML file named settings.yaml placed in your working directory. It has three top-level keys: targets and plans, described below, and logging, following the dictConfig schema.

targets

This is the heart of your settings. In it you have a list of targets, each representing a backup and/or a retention task:

targets:
  - target_path: "/home/django/honesty/db.sqlite3"

  - backup_dir: "/home/backup/kindness"
    retention_plan: "important"

  - target_path: "/home/django/loyalty/db.sqlite3"
    backup_dir: "/home/backup/loyalty"
    retention_plan: "meh"

  - target_path: "/home/django/generosity/db.sqlite3"
    backup_dir: "/home/backup/generosity"
    retention_plan: "important"
    prune: false

  - target_path: "/home/django/laughter/db.sqlite3"
    backup_dir: "/home/backup/laughter"
    retention_plan: "important"
    pin:
      - "20150717-1155.sq3.bz2"

  - target_path: "/home/django/magic/db.sqlite3"
    backup_dir: "/home/backup/magic"
    retention_plan: [[null, 10], [month, 3]]
    prune: false

Available settings are:

  • target_path: Full path to the file to be backed up. It is read without any kind of locking or waiting. If not given, only the retention policy is performed at backup_dir.
  • backup_dir: Full path to the directory where the backups are stored. If not given, it is backed up in place, alongside the same directory of target_path.
  • retention_plan: Name or inline description of the retention plan. See plans for details. If not given, no files are pruned.
  • pin: A list of filenames that are not to be pruned. Pinned files fulfill the retention slots they fall in.
  • prune: Set to false to run the retention plan in pretend mode. Results go in the logs.

plans

This is a mapping of named retention plans, to be referenced by the targets:

plans:
  important:
    - [year, null]       # permanent yearly snapshots
    - [month, 9]         # 9 monthly snapshots
    - ["1 week", 6]      # 6 weekly snapshots
    - ["1 day", 5]       # 5 daily snapshots
    - ["1 hour", 18]     # 18 hourly snapshots
    - [null, 10]         # 10 latest snapshots
  meh:
    - [null, 5]          # 5 latest snapshots
    - ["1 day", 8]       # 8 daily snapshots

A retention plan is a list of retention slots. A retention slot is a pair of granularity and quantity.

  • Granularity is how far apart we're interested the backups to be, so that they remain relevant as they get older. It is a human-readable duration string like "1 week", "5 days", or "1 hour". There are also three special values: year, month, or null. null means look at all backups.

  • Quantity is how many backups to keep when looking within that granularity. The special value null means keep all of them forever, and it's a useful quantity to give to your largest granularity.

Every time the retention plan is applied to a backup directory, it will run each slot on all files. For each desireable slot it will try to find a file to be retained, and pin the earliest one if none is found.

Because we don't always make backups (only when the file changes), we will only count the slots when at least one file can fulfill a slot. For example, let's say your file only changes on weekdays, and you have 7 day retention. No files will be generated on weekends, and to fulfill 7 days we'll need to go over to the previous week until at least 7 files can be found, each in a different day. This way, 7 non-consecutive days will be retained.

It's recommended to define retention slots from the biggest granularity to the smallest. The reason is that if you define slots from the smallest to the biggest, you will lose your earliest backups because later backups will fulfill the same granularity.

CLI Mode

hfbr                               # reads ./settings.yaml
hfbr -c /etc/hfbr/settings.yaml    # reads given config
hfbr target_path [backup_dir]      # CLI mode (no config)

If you don't have a settings file, you can use just the command line interface (CLI) for simple change-detection backup without a retention plan. To do that, simply define the origin and destination. As when defined using the Settings File, if backup_dir is not provided, it'll back up in place.

Roadmap

  • Maybe detect changes based on mtime and size instead? Checksum seems a bit overkill...
  • Special cases for target_path:
    • Detect sqlite databases and use their backup function.
    • Detect directories and tar them.
      • Use GNU differential tar (-g) based on retention granularities. Will this work on non-Linux?
  • Ability to push backups to a remote server or something. What makes sense, scp, e-mail, or what? In my scenario, pulling was way easier to implement.

Project details


Release history Release notifications | RSS feed

This version

0.2

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hfbr-0.2.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hfbr-0.2-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file hfbr-0.2.tar.gz.

File metadata

  • Download URL: hfbr-0.2.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for hfbr-0.2.tar.gz
Algorithm Hash digest
SHA256 23d012eea8734e18c6b190b86458c8ac63012d63c74244e1ae206f0ac31fc9f6
MD5 0898797d6ca8ce11cfffe35ee82fbb99
BLAKE2b-256 44d259caf84c37d0e7984392165be37d5e6e57b447851b6da8183b9b29575434

See more details on using hashes here.

Provenance

The following attestation bundles were made for hfbr-0.2.tar.gz:

Publisher: pypi.yaml on Liz4v/hfbr

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hfbr-0.2-py3-none-any.whl.

File metadata

  • Download URL: hfbr-0.2-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for hfbr-0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5a6dc5b9345e91dc070c9ee9217b4b18652429a71adc97760abb1994ce42a87b
MD5 2fd7ee44091ff8f6a7737c1de5c3a600
BLAKE2b-256 66833275b569eae7addc2b8d25e2f93294e5393e0bda37950a9994701a1cc550

See more details on using hashes here.

Provenance

The following attestation bundles were made for hfbr-0.2-py3-none-any.whl:

Publisher: pypi.yaml on Liz4v/hfbr

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page