When editing scripts and other files, it is a good idea to make numbered backups. Nothing is more frustrating than having something work, then break after some changes, and then not be able to get it working again quickly. With numbered backups you can always go back to a previous version that worked, and then use diff
to find the mistake. I sure learned this one the hard way.
The following is a backup script I wrote for the users of this book (the one I normally use is written in C). It is named mkbak
:
#!/bin/sh # mkbak script to create backup files if [ "$1" = "" ] ; then echo "Usage: mkbak filename(s)" echo "Creates numbered backup file(s) in the current directory." exit fi for i in $* ; do if [ ! -f $i ] ; then echo File $i not found. continue fi num=1 while [ 1 ] do ibak=bak-$num.$i if [ -f $ibak ] ; then num=`expr $num + 1` else break fi done cp $i $ibak rc=$? if [ $rc -eq 0 ] ; then echo File $i copied to $ibak else echo "An error has occurred in the cp command, rc: $rc" fi done
This script comes free of charge, but with some limitations. It will not handle filenames with blanks, and only works on files in the current directory. Note that you can cd
to the directory you want first and then run it.
The following is the script I use to backup the current book file I am working on:
#!/bin/sh # b1 script to copy book file # Date 1/22/2013 FN=startA1.txt # name of file to back up STARTDIR=`pwd` # remember the starting directory cp $FN /usb/book # copy to USB stick cd /usb/book # cd to it mkbak $FN # make the numbered backup cd $STARTDIR # go back to the starting directory cp $FN /megadrive/book # copy to USB external drive cd /megadrive/book # cd to it mkbak $FN # make the numbered backup cd $STARTDIR # go back to the starting directory sum $FN /usb/book/$FN /megadrive/book/$FN # use sum to check scp $FN $B2:/temp # copy to my other machine ssh $B2 /usr/bin/sum /temp/$FN # check the copy
While editing the file (the FN
variable), I will manually run this from time-to-time, usually after a lot of changes, and definitely just before I get up to take a break or whatever.