Backup files before editing

ript>
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:

#!/bin/bash
datetime=`date +%Y%m%d_%H%M`
bdir=”.backup”

mkdir $bdir 2> /dev/null

for a in $*; do
cp -av $a $bdir/$datetime.$a
done
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…

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.