Shadow.sh: A simple directory shadowing script for Linux
|Are you ever editing away on some program or document and suddenly realize that you're progressing down the wrong path? And then realize that the version you had two hours ago was the one you now need? Yes, the earlier version that you've since edited into something quite different is the one you wish you could have back.|
The following script is designed to help solve that problem as well as a few related ones, automatically. This script, shadow.sh, lets you take periodic snapshots of your document directory -- or source code directory -- and make backup copies of everything there. Further, it will keep the previous few versions of each file that you modify as well as the most recent version. The latest backup directory version of a file will have the same name as the original. The next oldest will have a .0 extension, and the version older than .0 will have a .1 extension, etc.
This little script is no replacement for a general purpose document management system, but it has its advantages. Once set up, it is automatic. It also works in situations where files change without direct human management.
I run the script automatically every hour on my main source code directory with a cron entry similar to this one:
0 * * * * /rayy/shadow.sh /rayy/sdir /rayy/bdir
This crontab line also demonstrates the parameters that shadow.sh requires which are simply the source directory name and the backup directory name:
shadow.sh [sourcedirectory] [backupdirectory]
You can, of course, run it manually using syntax similar to the above to test it out and to see how it works.
Type crontab -e from your shell prompt to create a new cron entry. If you are unfamiliar with cron, you can get more information on how to add and edit your crontab entries by typing man crontab at your shell prompt.
This version of the script keeps up to five versions of each changing file. Changing it to keep more or fewer versions of each file should be trivial. However, if you are not an experienced shell programmer, be very careful, use a test directory with no important files, and read the disclaimer below!
You can download the script by right clicking here and selecting "Save link..." and you can view it in your browser by clicking here. You can run it with no parameters for a quick synopsis of its useage.
The script is released under the terms of the GPL, version 2.
While this script is labeled 'for Linux', it should work just as well under any Unix variant by simply changing the pathnames for the commands to the appropriate locations for that system. Also, ksh can be used instead of bash on traditional Unix systems that don't have bash installed.
This script is free and comes with no guarantees of any kind. It has virtually no safeguards and might be tricky to modify since it recursively calls itself in order to copy arbitrary numbers of files and subdirectories elsewhere. It is probably possible to wipe out your files or fill up your disk drive so consider yourself warned! Neither Librenix.com or the author take any responsibility for any disasters that may arise out or your use of, or inability to use, this script.
|mail this link | score:8843 | -Ray, May 20, 2002 (Updated: April 18, 2007)|
More Sysadmin articles...||