Since I no longer have physical access to my main computer I can no longer just back up to a external firewire drive using SuperDuper! - well that and I’m also running NetBSD now, no longer OSX.
I’d already heard of and planned to use tarsnap and have discovered there are a lot of scripts already out there in addition to tarsnap generations to manage tarsnap backups. But since this is just for me and I’m not running a server hosting other peoples data, etc, weekly backups are fine and I don’t need a monthly-weekly-daily scheme. All I really want is just to be able to restore my home directory should I screw up an upgrade (NetBSD 6.0 to 6.1 was painless).
So what I have is the following, which isn’t even a script, just one line:
tarsnap --list-archives -v | sort -k 2 -r | awk '{ if (NR > 9) { system("tarsnap -d -f " $1) } } END { system("tarsnap -c -f `date +%s` /home/me/ /etc/") }'
- This gets a list of existing tarsnap archives using the verbose flag to get the dates of the archives as well.
- Which is then piped into
sort
, to sort based on date in reverse order - In turn, this is piped into
awk
. If the row number is greater than nine it deletes that archive (so limiting to a maximum of ten at a time and avoiding this issue) - And at the end it creates a new one using a timestamp as the name (archive names seem pretty irrelevant, just need something unique).
Run weekly via cron this gives me ten weeks of backups.
Whilst writing this post I thought of an alternative approach which is to do a weekly cron backup like so:
tarsnap -c -f `date +%s` /home/me/ /etc/
and then consolidate things down a bit on a monthly basis:
tarsnap --list-archives -v | sort -k 2 -r | awk '{ if (NR > 9) { system("tarsnap -d -f " $1) } }'
But I don’t think there are any advantages to this. The data pruning will cost the same data-transfer wise (it’s just all happening at once) and this also means paying for slightly more data storage between the monthly clean-ups.
EDIT: Need to escape % symbols in crontab. So the date bit above has to be date +\%s
. See bmk’s answer to “Unterminated Quotes String”