Project description

Timegaps is a cross-platform command line program. It sorts a set of items into
rejected and accepted ones, based on the age of each item and user-given
time categorization rules.

Timegaps allows for thinning out a collection of items, whereas the time gaps
between accepted items become larger with increasing age of items. This is
useful for implementing backup retention policies with the goal to keep
backups “logarithmically” distributed in time, e.g. one for each of the last 24
hours, one for each of the last 30 days, one for each of the last 8 weeks, and
so on.

Timegaps is built with a focus on reliability. It is backed by a considerable
set of unit tests, including direct command line interface tests. Currently,
each commit is automatically tested
against CPython 2.7/3.3/3.4 on Linux via Travis CI. Releases are tested on Linux
as well as on Windows. Simplicity and compliance with the Unix philosophy are the major design goals of
timegaps. Version tags follow the concept of semantic versioning.

Requirements

Timegaps requires Python. Releases are tested on CPython
2.7/3.3/3.4, on Linux as well as on Windows. This is where you can expect it to
run properly.

Documentation and changelog

Hands-on introduction

Consider the following situation: all *.tar.gz files in the current working
directory happen to be daily snapshots of something. The task is to accept one
snapshot for each of the last 20 days, one for each for the last 8 weeks, and
one for each of the last 12 months, and to reject all others. Use timegaps for
performing this categorization into rejected and accepted items and print the
rejected ones:

This was a read-only, non-invasive operation. By default, timegaps prints the
rejected items to stdout, separated by newline characters (for compatibility
with other Unix command line tools). Repeat the operation and count the rejected
items:

$ timegaps days20,weeks8,months12 *.tar.gz | wc -l
125

Given this specific set of rules and set of items, timegaps identified 125 items
to be rejected. Move them to the directory notneededanymore (and suppress
stdout):

Count files in the newly created directory for validation purposes (must also be
125):

$ /bin/ls -1 notneededanymore/* | wc -l
125

Okay, so far the item modification time was determined from the inode via the
stat() system call. In a different mode of operation (--time-from-
basename), timegaps can read the “modification time” from the basename. The
file names of the tarred snapshots in this hands-on session carry meaningful
time information, in a certain format (daily-%Y-%m-%d-%H%M%S.tar.gz).
Providing this format string, we can instruct timegaps to parse the time from
these file names:

The above can be useful in cases where the actual file modification time is
screwed, and the real timing information is only contained in the file name. In
another mode of operation (--stdin), timegaps can read newline-separated
items from stdin, instead of reading items from the command line:

By default, the reference time for determining the age of items is the time of
program invocation. Use -t/--reference-time for changing the reference time
from now to an arbitrary date (January 1st, 2020 in this case):

With a different reference time and different rules the number of rejected items
obviously changed (from 125 to 153). Instead of printing the rejected items,
timegaps can invert the output and print the accepted ones:

For a detailed specification of program behavior and the time categorization
method, please confer timegaps --extended-help.

General description

Timegaps’ input item set is either provided with command line arguments or read
from stdin. The output is the set of rejected or accepted items, written to
stdout.

Timegaps by default treats items as paths. It retrieves the modification time
(st_mtime) of the corresponding file system entries via the stat system
call. By default, timegaps works in a non-invasive read-only mode and simply
lists the rejected (or accepted) items. If explicitly requested, timegaps can
also directly delete or move the corresponding file system entries, using well-
established functions from Python’s standard shutil module.

In a special mode of operation, timegaps can treat items as simple strings
without path validation and extract the “modification time” from each string,
according to a given time string format. This feature can be used for filtering
any kind of time-dependent data, but also for filtering e.g. ZFS snapshots.

Main motivation

The well-established backup solution rsnapshot
has the useful concept of hourly / daily / weekly / ... snapshots already
built in and creates such a structure on the fly. Unfortunately, other backup
approaches often lack such a fine-grained backup retention logic, and people
tend to hack simple filters themselves. Furthermore, even rsnapshot is not able
to post-process and thin out an existing set of snapshots. This is where
timegaps comes in: you can use the backup solution of your choice for
periodically (e.g. hourly) creating a snapshot. You can then — independently
and at any time — process this set of snapshots with timegaps and identify
those snapshots that need to be eliminated (removed or displaced) in order to
maintain a certain “logarithmic” distribution of snapshots in time. This is the
main motivation behind timegaps, but of course you can use it for filtering any
kind of time-dependent data.

How can the unit tests be run?

If you run into troubles with timegaps, or if you want to verify whether it
properly runs on your platform, it is a good idea to run the unit test suite
under your conditions. Timegaps’ unit tests are written for pytest. With timegaps/test being the current working
directory, run the tests like this:

$ py.test -v

Author & license

Timegaps is written and maintained by Jan-Philip Gehrcke.
It is licensed under an MIT license (see LICENSE file).