Skip to content

Automating the backup process

François edited this page Jun 22, 2015 · 10 revisions

It's generally a good idea to automate your backup process, so you don't have to think about it, and you don't have to run it manually.

Using systemd (Linux)

Using systemd to schedule the job allows you to precisely allocate resources for the job to run properly (IOSchedulingPriority, Nice, ...). It also allows you to set dependencies between units, eg. mounting the destination volume before starting the backup.

Let's see how to setup such a systemd unit.

First, create the timer unit, /etc/systemd/system/timer-hourly.timer :

[Unit]
Description=Hourly Timer

[Timer]
OnBootSec=5min
OnUnitActiveSec=1h
Unit=timer-hourly.target

[Install]
WantedBy=basic.target

Then create the target, /etc/systemd/system/timer-hourly.target :

[Unit]
Description=Hourly Timer Target
StopWhenUnneeded=yes

Finally, create the needed directory :

mkdir /etc/systemd/system/timer-hourly.target.wants

Enable the hourly timer you just created :

systemctl enable timer-hourly.timer

To make something run each hour, you just have to create a systemd service file for it in /etc/systemd/system/timer-hourly.target.wants.

So let's create /etc/systemd/system/timer-hourly.target.wants/WABACMachine.service :

[Unit]
Description=The WABAC Machine

[Service]
Type=simple
Nice=19
IOSchedulingPriority=7
IOSchedulingClass=best-effort
StandardOutput=journal
StandardError=journal
ExecStart=/path/to/WABACMachine.sh backup -c /path/to/config_file.conf

At this point you might want to disable the hourly timer if you haven't setup your WABAC Machine properly. To do so, run :

systemctl disable timer-hourly.timer

Setup your WABAC Machine, test it, and re-enable timer-hourly.timer after.

Important note : those are examples that are known to work. You may want to specify others values, depending on your setup, needs and system.

Using launchd (OSX)

With OSX, the best way to automate a process such as the WABAC Machine is to create a deamon. Hopefully, launchd makes it pretty easy. Let's see how.

First, you will need to create a property list file that will tell launchd what to do, how and when. As root, create /Library/LaunchDeamons/org.kubler.WABACMachine.plist and edit it :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>The WABAC Machine</string>
    <key>ProgramArguments</key>
    <array>
        <string>/path/where/WABACMachine/is/WABACMachine.sh</string>
        <string>backup</string>
        <string>-c</string>
        <string>/path/to/your/config_file.conf</string>
    </array>
    <key>WorkingDirectory</key>
    <string>/path/where/WABACMachine/is/</string>
    <key>StandardOutPath</key>
    <string>/var/log/WABACMachine.log</string
    <key>StandardErrorPath</key>
    <string>/var/log/WABACMachine.log</string>
    <key>KeepAlive</key>
    <false/>
    <key>AbandonProcessGroup</key>
    <true/>
    <key>RunAtLoad</key>
    <false/>
    <key>StartInterval</key>
    <integer>3600</integer>
</dict>
</plist>

Basically, you created a config file for launchd that asks it to run /path/where/WABACMachine/is/WABACMachine.sh backup -c /path/to/your/config_file.conf every hours (3600 seconds) and to output everything (including errors) in /var/log/WABACMachine.log.

This is just one example. You can create your own property list file to suit your needs (you can restrict CPU and/or memory usage, start the job on a precise schedule, etc.).

Also notice that Apple strongly recommends to put your scripts in /usr/local/libexec. So that's where your WABACMachine.sh file should go (don't forget to edit the WorkingDirectory entry of your property list file to reflect this).

Once you are done, save the file and load it, so that launchd knows that it has to deal with it. As root :

launchctl load /System/Library/org.kubler.WABACMachine.plist

That's it, you're done !

For further help regarding launchd and creating launchd deamons/agents, you may want to read :

Using cron

If you want to keep your scheduled tasks in cron, edit your crontab as root :

crontab -e

And add an entry for the WABAC Machine :

@hourly /path/to/WABACMachine.sh backup -c <config_file>

If you want to monitor the WABAC Machine, it's a good idea to redirect the WABAC Machine outputs to a specific file. The crontab entry becomes :

@hourly /path/to/WABACMachine.sh backup -c <config_file> > /var/log/WABACMachine.log 2>&1

Save the file. You're done !