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

March 25, 2008

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).)


Comments on this page:

From 203.144.24.214 at 2008-03-26 05:27:51:

GNU Date is such a handy tool. I miss the GNU tools when they're not around (i.e. somebody else's Mac I haven't had my mitts on yet).

Anyway, if you're thinking of using anything more granular than a day, one may want to use UTC mode like date --utc +%H.

 - Jason

Sidenote: UTC and ISO 8601 are your friends :)

From 72.91.79.100 at 2008-03-26 18:48:58:

If you add a few lines, you ensure your most recent backup always has the same name:

 DAY=$(date +%a)
 pg_dump >db-back.$DAY
 rm -f db-back.cur
 ln -s db-back.$DAY db-back.cur

Thus, db-back.cur is always the most recent backup.

Written on 25 March 2008.
« The two sorts of (programmer) certification
Why authenticated email won't stop phish spam »

Page tools: View Source, View Normal, Add Comment.
Search:
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Tue Mar 25 22:58:26 2008
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.