Backup files before editing

It’s a fact that many text editors (or word processors) have the ability to keep a backup copy of every file you edit, just to assure you that you can always restore the previous version of the file. This is usually implemented by creating a copy of the file with a file extension of .bak or by appending a “~” character at the end of the current file extension, like .c~

Although being a useful option for a programmer (or a writer), it lacks the capability of keeping more backup copies, like a CVS (Concurrent Versions System) where the author can go back in time and find the text file as is was e.g. a month ago (very handy if you want to restore something you have deleted just before one week).

In order to avoid the complexity of a CVS, yet having the option to “travel” back in time, I wrote a small shell script, which I’m running just before my editing sessions. The script looks like this:

datetime=`date +%Y%m%d_%H%M`

mkdir $bdir 2> /dev/null

for a in $*; do
cp -av $a $bdir/$datetime.$a
gzip -9 $bdir/$datetime.*

All it does is to keep a compressed copy of every file you want to edit in a directory named “.backup”, hence I named it “backup2.backup” and I run it as:

backup2.backup *.php *.html

It doesn’t check many things, but I’m using it for some time now. Feel free to enhance it as you like it.

Visit The Light of the LAMP blog for more…