0

We need to do a crude type of backup on one of our mac's - we need to copy files from it onto our windows fileserver once a day regardless of whether someone is logged onto it or not. I can find scripts that do it while a user is logged on and the windows share is mounted but i can't figure out a way of doing it when they are not logged on.

Is there a unix / osx geek out there that knows how to do this?

4 Answers 4

3

Running a script when no one is logged in

To run a daemon that runs no matter whether you have a user logged in or not you'll want to look at launchd for managing the script that will actually doing the backup. Since Mac OS X 10.4 cron has been replaced by a program called launchd (man page) which combines cron, init.d, rc, and a couple other utilities all into one program.

For reference, launchd runs Launch Agents (run as a user when a user is logged in) and Launch Daemons (run regardless of when a user is logged in). These tasks are defined by xml .plist files that can be found in one of the following locations:

/System/Library/LaunchDaemons (system tasks - don't modify),
/Library/LaunchAgents (computer wide tasks that run when any user is logged in),
/Library/LaunchDaemons (computer wide tasks that run no matter who is logged in),
~/Library/LaunchAgents (user specific tasks that run when that user is logged in).

More information on the loading, unloading, etc. can all be found on the man page.

The easiest method I've found is to create a shell script to run your backup.

You can then load it and have it run by placing the following .plist file in /Library/LaunchDaemons and then run sudo launchctl load -w /Library/LaunchDaemons/org.example.script.plist to load it to run.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>org.example.script</string>
    <key>OnDemand</key>
    <true/>
    <key>ProgramArguments</key>
    <array>
        <string>/PATH/TO/BACKUP/script.sh</string>
    </array>
    <key>RunAtLoad</key>
    <false/>
    <!-- This runs the script every day at 2AM -->
    <key>StartCalendarInterval</key>
    <dict>
        <key>Hour</key>
        <integer>2</integer>
        <key>Minute</key>
        <integer>0</integer>
    </dict>
</dict>
</plist>

Additionally if you want to edit or create launchd plists I'd recommend looking up Lingon a GUI for editing and creating such .plist files.

Backup script

A really simple backup script could involve the following elements:

1) Mounting the windows file share:

eg.

mkdir /tmp/MountLocation
mount -t smbfs //domain;user:password@server/sharepath /tmp/MountLocation

2) Use rsync to backup the files.

3) Unmount the windows files share when done:

eg.

if [`umount /tmp/MountLocation`] 
then 
    rmdir /tmp/MountLocation
fi

More info about mount -t smbfs can be found on it's man page or man mount.

Minor Caveats

On the off hand, it should be noted that in Tiger (10.4) StartCalendarInterval has a rather annoying bug when dealing with sleeping that has since been fixed in Leopard (10.5) and later.

3

I highly recommend checking out the 'cron' command. It allows you to schedule arbitrary commandline tasks at various times with repetition.

1
  • 1
    cron has been replaced by launchd in OS X since 10.4. It still 'works' for legacy applications but launchd is doing all the work.
    – Chealion
    Jun 6, 2009 at 6:21
1

In addition to cron, rsync may also be helpful if you just want to keep a current copy of all the files. Here's a howto from the Ubuntu forums which describes the process pretty similarly to what you would do in OS X. You can skip ahead to to the part on rsync, and fink or macports can be substituted for the aptitude commands to get packages.

The instructions my look a bit involved, but once you get going, it shouldn't be too hard.

1
  • +1 rsync rocks! Of course, it's not as easy to get on windows; check Cygwin.
    – Javier
    Jun 5, 2009 at 19:42
1

You'll need to put together a script that creates a mountpoint, mounts the Windows server using mount_smbfs at that mountpoint, copies the files, and then unmounts the server. You'll have to decide what behavior you want if there is a user logged in-- you probably don't want them trying to mount the same share twice. Here's are some bits from a script I use for a similar task (by the way, nice formatting that turns every bash comment into a header, O Server Fault team): `

  • First, create a mountpoint

if [ -e /private/tmp/Volumes ] ; then mkdir /private/tmp/Volumes/Terminal/ else mkdir /private/tmp/Volumes/ mkdir /private/tmp/Volumes/Terminal/ fi

  • Now, mount the Sophos computer there

mount_smbfs //username:[email protected]/InterChk /private/tmp/Volumes/Terminal

  • If the mount was unsuccessful, bail!

if [ ! -e /private/tmp/Volumes/Terminal/ESXP ] ; then exit fi

  • Remove the old Sophos folders before backing the current one up

if [ -e /Library/WebServer/Documents/Sophos.old ] ; then rm -r /Library/WebServer/Documents/Sophos.old fi

if [ -e /Library/WebServer/Documents/Sophos ] ; then mv /Library/WebServer/Documents/Sophos /Library/WebServer/Documents/Sophos.old fi

  • Create a new one

mkdir /Library/WebServer/Documents/Sophos

  • And copy the latest CIDs

cp -Rp /private/tmp/Volumes/Terminal/ESXP /Library/WebServer/Documents/Sophos/ESXP cp -Rp /private/tmp/Volumes/Terminal/ES9x /Library/WebServer/Documents/Sophos/ES9x cp -Rp /private/tmp/Volumes/Terminal/ESOSX /Library/WebServer/Documents/Sophos/ESOSX cp -Rp /private/tmp/Volumes/Terminal/savlinux /Library/WebServer/Documents/Sophos/savlinux

  • Set permissions for Web access

chown -R _www /Library/WebServer/Documents/Sophos

  • And unmount the volume

umount /private/tmp/Volumes/Terminal/

  • And remove the mountpoint

rmdir /private/tmp/Volumes/Terminal/ `

Hope that helps get you started. Then use cron or launchd to set tit up on a repeating schedule, or if you prefer you can set it up as a logout script so when the user logs out it automatically backs up files.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .