![]() |
Run a shell script (before or after) the clone operation |
||
If there is functionality that you need that does not exist within CCC, pre and post clone shell scripts may be the solution for you. For example, suppose you want to back up your Open Directory Master with CCC at regular intervals, but you know that a simply file-level copy will not properly back up an open database. The following pre-clone shell script will archive your OD master to a disk image for later restoration via Server Admin. It also dumps copies of Server Admin configurations* as well as your MySQL databases:
#!/bin/sh
# Path to recovery directory (permissions should be 700 -- read-only root or admin)
recover="/etc/recover"
# mysqldump the databases
echo "Dumping MySQL databases"
mysqldump --user=root --password='s3kr!t' -A > $recover/mysql.dump
# grab the server configuration plists (only specify services that are enabled,
# use "serveradmin list" for a list of services)
echo "Generating serveradmin configuration plists"
services="afp dhcp dns ipfilter nat network swupdate vpn web"
for service in $services; do
serveradmin -x settings $service > $recover/serverconfig/$service.plist
sleep 1
done
# Backup Open Directory (if it's Thursday)
day=`date ''+%u''`
if [ $day != 4 ]; then exit 0; fi
od_backup=$recover/od_backup
ts=`date ''+%F''`
echo "dirserv:backupArchiveParams:archivePassword = s3kr!t" > $od_backup
echo "dirserv:backupArchiveParams:archivePath = $recover/od_$ts" >> $od_backup
echo "dirserv:command = backupArchive" >> $od_backup
serveradmin command < $od_backup
* Server Admin configuration plists are valuable as documentation of your settings, but they may not be importable to Server Admin directly.
Pre-clone shell scripts run after you authenticate, and before any other tasks (e.g. mounting a target disk image, erasing the target). Post-clone shell scripts run after all other tasks have completed successfully. CCC passes as parameters both the source and target paths. For example, the following shell script:
#!/bin/sh
echo "Running $0"
echo `date`
echo "Source: $1"
echo "Target: $2"
Would produce the following output (typically in /Library/Logs/CCC.log, though you can redirect as desired):
Running /etc/postaction.sh
Wed Aug 14 21:55:28 CDT 2007
Source: /Volumes/Home
Target: /Volumes/Offsite Backup
Output from your pre- and post-clone shell scripts will be logged in /Library/Logs/CCC.log. CCC may drop the last output from your script, however, because output handling is handled asynchronously to avoid impacting performance of the backup task. To force all output to be logged, you can add "sleep 1" at the last line of your script.
Also, if your pre-clone script exits with a non-zero exit status, it will cause CCC to abort the backup operation. This can be used to your advantage if you want to apply preconditions to your backup operation. Alternatively, you can add "exit 0" at the end of your script to prevent any errors in your pre-clone script from aborting the backup task.
Mounting a volume before a scheduled task and unmounting a volume after the scheduled task
Unmounting a target or source volume at the end of a scheduled task is currently only possible with the use of a postflight shell script. You could use the following shell script as a postflight script to unmount the backup volume (it imposes a 1-minute delay to let the filesystem "settle down", otherwise a system component might dissent the unmount request):
#!/bin/sh
(sleep 60; diskutil unmount "$2" >> /Library/Logs/CCC.log 2>&1) &
Mounting the disk prior to backup is a bit trickier, because CCC runs preflight scripts only after the sanity checks (e.g. is the target disk present?) have been performed. The best way to approach this is to schedule the task to run "When the target is reconnected" and implement that postflight script to unmount the backup volume when finished. Backing up is really simple then, you just plug in (or turn on, or mount) the backup disk whenever you want to back up.
Emailing the result of a scheduled task
This simple post-action script will email the results of the last scheduled task to the email address of your choice. No frills, just some basic information about the source and target volumes involved, the amount of data backed up, and a status code.
#!/bin/sh
(sleep 10; /usr/libexec/PlistBuddy -c "Print" /Library/Logs/CCC.stats | tail -n 9 | mail -s "CCC Scheduled Task Report" user@email.com) &