Skip to main content

kind of like logrotate, but for backups

Project description

backups-rotate

You run a website with a database, and you want to backup your database every day, and keep the last 7 days of backups. This script will do that for you.

WORK IN PROGRESS

not yet published on pypi

What it does

You can define a policy on several frequency levels, e.g.

  • one per year - no matter how far back in time
  • and/or, keep one backup for each of the last 3 quarters
  • and/or, the last 2 months
  • and/or, the last 4 weeks
  • and/or, the last 5 days
  • and/or, the last 6 hours

Installation

pip install backups-rotate

Configuration

The tool comes with 6 predefined "periods": year, quarter, month, week, day, hour.

You define your folder and policy in a YAML file, like e.g (for the policy described above):

---
# this defines the area named "database" that can be used
# on the command line to specify what needs to be cleaned up

- name: database
  folder: /usr/lib/backups
  patterns:
    - "mydb*.sql"
    - "mydb*.sql.gz"
  policy:
    year: infinite
    quarter: 3
    month: 2
    week: 4
    day: 5
    hour: 6
  # optionally, you can specify the format of the timestamp in the file name
    # datetime_format: "%Y-%m-%d_%H-%M-%S"
  # OR, still optionally, you can specify to use the modification time
  # instead of the creation time
    # use_modification_time: true

Usage

Here are a few ways to run the tool

# run on all areas
backups-rotate

# on a specific area
backups-rotate database

# not doing anything, just showing what would be done
backups-rotate database --dry-run

# do it, and be verbose about it
backups-rotate database --verbose

# using a config file (default is /etc/backups-rotate.yaml)
backups-rotate --config my_policy.yaml database

which will remove, from the specified folder, all backups that are older than the policy you defined.

How policies are implemented

When run on a specific area, the tool will:

  • gather all matching files in the specified folder
  • then for each frequency present in the policy, starting with the shortest one:
    • split time into bins of that frequency, starting from now and going backwards
    • attach every file to corresponding bin
    • then for the number attached to that frequency (can be infinite), and starting from the most recent bin:
      • take the most recent file and mark it as kept
    • then list (if dry-run) or remove (if not dry-run) all files that are not marked as kept

Example

Based on sample2() in tests/samples.py, assume you have one file per hour between 2024-01-01 00:00 and 2024-11-15 23:00, then applying the policy above would keep:

2024-06-30 23:00    # for quarter
2024-09-30 23:00    # for quarter
2024-10-27 23:00    # for week
2024-10-31 23:00    # for month
2024-11-03 23:00    # for week
2024-11-10 23:00    # for week
2024-11-11 23:00    # for day
2024-11-12 23:00    # for day
2024-11-13 23:00    # for day
2024-11-14 23:00    # for day
2024-11-15 18:00    # for hour
2024-11-15 19:00    # for hour
2024-11-15 20:00    # for hour
2024-11-15 21:00    # for hour
2024-11-15 22:00    # for hour
2024-11-15 23:00    # for hour day week month quarter year

if instead the policy was defined with month: infinite, then the policy would also retain

2024-01-31 23:00
2024-02-29 23:00
2024-03-31 23:00
2024-04-30 23:00
2024-05-31 23:00
2024-07-31 23:00
2024-08-31 23:00

noting that the following 2 were already kept for quarter:

2024-06-30 23:00
2024-09-30 23:00

Timestamps

By default, time is taken from the file's creation time. If you want to use the file's modification time instead, you can use the use-modification-time flag in your yaml config.

Also if your files are named with a timestamp, you can use the datetime-format option to specify the format of the timestamp in the file name (using Python's datetime format); files that do not match the format will cause an error.

This means you can use either

Tests

pip install pytest
pytest

Project details


Download files

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

Source Distribution

backups_rotate-0.0.1.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

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

backups_rotate-0.0.1-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file backups_rotate-0.0.1.tar.gz.

File metadata

  • Download URL: backups_rotate-0.0.1.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for backups_rotate-0.0.1.tar.gz
Algorithm Hash digest
SHA256 e4c8b8b25e4efd573f5719d18c07856e3f3520eefc90f70e87f2df863946ca72
MD5 98067343d7de8b3ccc32d8cf3b8724a8
BLAKE2b-256 83a30e67be330ebc22ef1a73484641b20e7f6a30bed061caf7f1446ded2351f6

See more details on using hashes here.

File details

Details for the file backups_rotate-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: backups_rotate-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for backups_rotate-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e87fd34224fa263608f8f397a7f0f00b9e89e589fbd92be6bfb19a03929a6d38
MD5 c5cc0bc2973f363644a408f428282893
BLAKE2b-256 5960aa1dca089fdc0f84970fa4c232b1a701a415e5421b1a12acb8f114ddbbb1

See more details on using hashes here.

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