Automated Backup with Shell and AppleScripts

Automated Backup
Using Shell & AppleScripts

I like things to be quick and easy, so I’ve written a shell script and an AppleScript that can be run automatically by the Mac OS X/Unix system utility, cron. Once set up, these scripts run without any intervention on your part and keep your Entourage data safely backed up. The difficulty lies in the setup, which involves some use of the Terminal program and a Unix utility. I’ll try to explain those in detail to make it as easy as possible.

Here’s what the scripts do:

  • Every day, make a dated zipped archive of the Microsoft User Data folder, which includes all of my Entourage data and more. The archive is is stored in my Home folder.
  • Purge the number of zipped archives to keep only the most recent three.
  • Once a week, copy the latest archive to an external hard drive, and purge the number of archives kept on that drive to the latest ten, thus keeping backups going back ten weeks.

I use the freeware utility program cronnix (donationware), which offers a simple graphical interface to the Unix cron utility, to schedule the scripts to run on a daily basis. Here are the two scripts in their entirety. You will need to modify the disk names and file paths to correspond to your own system.

The backup shell script, named “copyerage.sh”

#!/bin/sh
echo “ditto beginning”
ditto -c -k -X –rsrc ~/Documents/Microsoft\ User\ Data “/Users/AllenW/Microsoft User Datadate +%y-%m-%d.zip”
echo “purge beginning”
osascript ~/Library/Scripts/Utility/CronTab/purge\ MUD\ backups
echo “CpErageDB ended”

The script can be written in any program that saves plain text files, such as TextEdit, BBEdit, or TexEdit Plus. Be certain you change the file paths in the “ditto” line to correspond to your system. I choose to store the archives in my Home folder, “/Users/AllenW”, but you can store them anywhere you wish to. The strange-looking file name that ends in “date +%y-%m-%d tells the utility to insert a date stamp as part of the file name, so the archive file gets named something like “Microsoft User Data09-02-07.zip”. That way you know what day the backup was made.

The line beginning “osascript” is a shell command that causes an AppleScript to be run. When the backup is complete (which takes a relatively short time, several minutes; the ditto utility is extremely fast), the AppleScript that you see below is run to purge the number of copies of the archive you are keeping, and once a week to move the latest archive to an external drive for longer storage. Here, you will need to substitute the name you give the AppleScript, with the file path to the location in which you store it. As you can see above, I’ve stored it four folder-levels deep in my Home folder (represented by the tilde, “~”). The script is named “purge MUD backups”. Because you cannot have spaces as part of a file name in a shell command, I had to “escape” the spaces by preceding them with a backslash. If you don’t have spaces in the name you give the script, you don’t need any backslashes.

After saving the shell script, you must give the script “execute” privilege. To do so, the simplest way is to use Terminal. You’ll find the Terminal program in your /Applications/Utilities folder. Double-click it to execute it.

Let’s assume you have stored the script in your ~/Library/Scripts folder. You have to navigate to that folder in Terminal. The easiest way to do so is to type this line:

cd ~/Library/Scripts

You’ll know the command succeeded if you see the word “Scripts” as part of the next prompt, like this: “Scripts AllenW$ “, except you will see your username instead of mine.
Now, list the current permissions on your script file. Let’s say you have named the file as I suggested above, “copyerage.sh”. Type:

ls -l copyerage.sh

That is the lowercase letter “L” after the hyphen, not a capital “I”. You should see a line that looks like this in response:

-rw-r–r–@ 1 AllenW staff 5432 Nov 28 2007 copyerage.sh

Don’t be concerned about the stuff in the middle, just the file name at right and the “-rw” stuff at the left. Notice there are no x’s in the line, which indicate “Execute” permission. You need to add that. For this, you use a Unix utility named “chmod.” Type this:

chmod 744 copymerge.sh

If it succeeds you just get another prompt; no “done” message is issued. To verify that the file is now executable, repeat your “ls” command (just tap Up Arrow twice to recall that command, then Return to run it). The result now should look like this:

-rwxr–r–@ 1 AllenW staff 5432 Nov 28 2007 GrowlTest.scpt

Notice the “x” after the “rw”? That indicates that you, as owner of the file, have permission to execute this script. Everyone except you only has “r” (read) access to the file.

The AppleScript, named “purge MUD backups”

Here is the content of the AppleScript. It’s a bit longer:

tell application “Finder”
set dir to path to home folder
set fList to files in dir whose name begins with “Microsoft User”
— Copy weekly archive to external hard drive
set dow to weekday of (current date)
if dow is Thursday then
set oldest to item 1 of fList
— Change the folder and disk names below to match your system!!
duplicate oldest to folder “Documents” of disk “LaCie Disk” with replacing
end if
if (count fList) > 5 then
repeat with a from 1 to ((count fList) – 5)
delete {item a of fList}
end repeat
end if
— Purge the weekly archives down to last ten weeks
— Change disk and folder names in next line also!
set fList to files in folder “Documents” of disk “LaCie Disk” whose name begins with “Microsoft User”
if (count fList) > 10 then
repeat with a from 1 to ((count fList) – 10)
delete {item a of fList}
end repeat
end if
empty trash
if dow is Thursday then
— Change disk and folder names below as well.
return “Daily copy made; Weekly copy of MUD stored in LaCie Disk/Documents folder.”
else
return “Daily copy made of MUD folder in Users folder; last 5 retained.”
end if
end tell

As indicated in three places in the script comments, you will need to modify the script by changing the disk and folder name to those of a folder on your external disk. If you don’t have an external disk drive I urge you to get one. Backups that are stored on the same drive as the original data are not really secure. If your disk goes belly up, you’ll lose the backup along with the original.

Setting Up Automated Runs

That’s it for the two scripts, but now you need to tell the system when to run them, and how often. For that, use the freeware utility “Cronnix.” Here is the line I have set up in Cronnix to run the shell script, which in turn runs the AppleScript:

If you examine the column headings in Cronnix, you will be able to figure out that this line of data indicates that at 4:00 AM every day of the month, and every month (asterisks mean “all”), the script is to be run. Note too that the checkbox at the left of the line is checked.

If you wish, you can test the script by selecting the line you have entered in Cronnix and then selecting “Run Now” from the “Task” menu. The script will run, and you can verify that the zipped archive has been created.

To be sure your schedule works as desired, be sure to check each day for the next few days to see that a new archive is created, the archives get purged as you wish, and the weekly copy is being made to the external drive. Once a week has gone by and all has happened as it should, you can just forget about it and rest secure that your Entourage data is being backed up every day.

Quit Entourage and the Microsoft Database daemon

Hopefully, you are not using Entourage at 4AM, but to be safe you should quit Entourage and the Microsoft Database daemon before backing up. If the database gets written to during the backup you can either (1) lose some data or (2) have a corrupt backup file. To be absolutely safe, you should quit Entourage and the Microsoft Database daemon before backup. You can use these scripts to quit.

Share this:
  • Twitter
  • Facebook
  • del.icio.us
  • Digg
  • StumbleUpon
  • Google Bookmarks
  • PDF
  • Print

2 comments to Automated Backup with Shell and AppleScripts

  • Joshua Adom

    This really good but I need help.
    I have written a backup script as follows
    echo “Taking Domain Backup…”
    hostname
    echo “Tarring intellectprod”
    sleep 2
    cd /usr/weblogic/bea/user_projects/domains
    tar -cvf intellectprod_date “+%d%b%Y”.tar intellectprod
    echo “Zipping intellectprod”
    sleep 2
    gzip intellectprod_date “+%d%b%Y”.tar
    echo “Moving intellectprod_date.tar.gz to Backup folder”
    sleep 2
    mv intellectprod_date “+%d%b%Y”.tar.gz /usr/weblogic/Backup
    what I want to do now is to reverse the process in case there is a disaster.
    i want to be able to identfy the most current folder and use for restore.
    can you help?

  • Allen Watson

    Joshua, I’m afraid I’m only a novice at shell scripting. I really don’t have a clue how to reverse the process of your script. Obviously you need, some way, to “identify the most current folder”…and I’m stumped right there.
    Maybe someone else knows Unix shell scripting more than I do?