Wandering Thoughts archives


The easy way to keep a week's worth of something

As an example, say that you want to keep a week's worth of backups from your PostgreSQL database. The pg_dump command will get the dumps, but how do you handle keeping a week's worth (or only keeping a week's worth, depending on your perspective)?

Simple: use GNU date's ability to give you the day of the week to cycle through a set of seven backup files, one for each day. It would look something like this:

DAY=$(date +%a)
pg_dump >db-back.$DAY

(I admit that I tend to use %u, the day of the week as a number, instead of %a, the day of the week as a name.)

This has the great virtue that you don't need to write (or reuse) any code at all to age things; you get aging for free, at the cost of the most recent version not always having the same name.

This works equally well if you want to keep seven days worth of a collection of things; just put them in subdirectories named after the day. You can adopt it for other intervals that date gives you convenient indices for, such as day of the month (%d) to keep roughly a month's worth of something, or week of the year (%U et al) to keep a year's worth of a weekly backup.

(Attempts to take day of the year mod something are coercing this trick further than it should be pushed. And if you do hours, don't forget the effects of going into and out of daylight savings time (if you do).)

sysadmin/WeeksWorth written at 22:58:26; Add Comment

Page tools: See As Normal.
Login: Password:
Atom Syndication: Recent Pages, Recent Comments.

This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.