then one day, chris ran across an article about writing shell scripts. shell scripts made chris very happy because now he could write one to take care of his archiving problems, and they all lived happily ever after.
maybe shell scripts won't help you to live happily ever after, but they can make your life easier. shell scripts are nothing more than fancy dos batch files. one really cool thing about UNIX is that you can set permissions on any file to make it executable (unlike DOS which you must name a file .bat, or a binary .exe). so i can write any file name i like (not just 8.3) and make it executable. here's how it works:
- get the path to the interpreter it will run under and put it at the beginning of the file like this:
i don't know if this is the proper terminology, but essentially you can write a script, but it must be interpreted by something. a traditional program is compiled into binary, so it interprets itself. you can write simple scripts like this one, that are interpreted by a shell, but there are scripting languages like perl and python that are much more powerful. you can find this with the
which command. just type
which bash for the bash shell. there are many shells in unix, and i don't really know enough about them, i usually use bash or ksh (on irix boxes). there are shells in NT called CMD, and i winblows, it's called command.com. the explorer shell (kind of like a window manager is in X) is a different sort of shell that behaves differently. in unix, different shells do different things. an example is csh, which works like C, so if you are a C programmer, you can write shell scripts which are very similar to C. i don't know anything about C, so don't quote me on that.
- write in the commands that you want the script to execute like this:
cp bakup/web.tar.gz bakup/webold.tar.gz
in case it's not obvious, all this does is copy the current backup to webold, delete the current backup (i could use mv, to do this in one step but i don't trust mv, call me crazy), use tar to create a new backup, user tar again to list the contents of that backup and redirect the output to bakup.txt, then compress the backup with gzip. easy money.
tar -cvf bakup/web.tar www
tar -tvf bakup/web.tar > bakup/bakup.txt
this script might work verbatim for you, it might not. a few minutes with which, man, and your favorite editor can go a long way to boosting your productivity ;-)
after doing more reading i found a Better Way to do the same thing. so here it goes:
ls -lR www > bakup/www_bakup_`date '+%d-%B-%Y'`.txt
tar -zcvpf bakup/www_bakup_`date '+%d-%B-%Y'`.tar.gz --directory www
again, line 1 redirects ls into a text file, only this time it includes the result of the date command. the second line creates the tar file and compresses it in one pass, this time naming it in a similar fashion as the .txt file using the date command. since each tar file now has a different name, there is no need to futz with cp, mv or rm.