Tech Musings

Thursday, December 15, 2005

shell scripting, crontab and other fun stuff!

This a post that I knew I'd eventually get to, but I figured I'd wait to write about it until I had to undoubtedly re-read all the documentation I perused last year to perform another backup routine.

So, here goes. To create automated backups, it is imperative that you understand a little bit about shell scripting, the UNIX/LINUX crontab feature in OS X, backup tools or utilites (cp, ditto, rsync, psync), ftp using scp, MySQL dump procedures, tar and gzip, and some other little goodies thrown in for good measure.

I learned most of this by reading the incredibly helpful Learning the Terminal articles written by Chris Stone of the O'Reilly network.

crontab: The cron app runs continuously in the background executing commands in crontab files. Each user can have their own crontab file. The system crontab is located at /private/etc and belongs to root. You can "pico" the crontab file and change the times tasks are run. Click here for more information. To edit your user crontab, type crontab -e [crontab -l displays it and -r removes it]. Click here for another useful aid that explains how to schedule a cron task, although this was written for Sun Solaris 9 so the location of the crontab file is different.

sendmail: Sendmail needs to be configured properly in order to have reports e-mailed to you. Chris Stone's Part II article explains how to set up sendmail for this. There is another great, more detailed article written by James Duncan Davidson on how to configure sendmail located here. To mail a report, the command should read from the prompt ~/bin/backup.sh | mail -s "Daily Report" jim.

**Added 3/8/2006 ***
I came across a pretty good little article explaining how to create a crontab job to clear out the sendmail queue every so often here. The sendmail queue resides at /var/spool/clientmqueue/.

Copy Utilities:

cp preserves permissions by not resource forks
ditto preserves permissions including resource forks
rsync preserves permissions by not resource forks, plus syncs changes only
psync preserves permissions and resource forks (I think)

Example:
ditto -V rsrc ~/Documents/somefilename.pdf /Volumes/Macintosh\ HD2/somefilename.pdf

[the -V flag turns on verbose copying which prints a line for each file copied.]

shell scripts:
The conventional directory for storing user scripts is ~/bin [mkdir ~/bin if not there]
Scripts end in .sh like backup.sh
The first line of a shell script should be entered as #!/bin/sh
Be sure it is executable [chmod +x ~/bin/backup.sh]
To execute a shell script, just type backup.sh at the prompt. (example #1,
example #2, example #3)

You can set your editor with the command setenv EDITOR pico. To make this permanent, do this ........ (research).

To view running tasks with the process watcher, type top -u from the prompt.

scp (ftp):
NOTE! spaces in filenames are "No-No's" and will cause problems with this operation!

To copy a file from remote to local type:
scp user@remote:/remote/path/remotefile /local/path/remotefile

To copy from local to remote type:
scp -r /local/path/localdirectory user:password@remote:/remote/path/localdirectory

0 Comments:

Post a Comment

<< Home