I’m attempting to run a weekly backup on a mac mini (MacOS 11.4) using rsync and scheduling it using launchd. However, when it runs, instead of copying files, it gives me a bunch of errors. I don't think the issue is the rsync command, because the command (and script file) both work without issue when manually launched.
Here’s the command:
sudo rsync -ax --delete /Volumes/Office/ /Volumes/ Office\ Clone
Launchd requires a script file, so I put that line in a text document, named it officeBackup.sh, and gave it executable permissions. Running the script from the command line works flawlessly.
Since I won’t be able to type a password when the script executes, I removed the sudo from the start of the script file. I know I’ll need to execute the script with root privileges to make it work.
Next, I built a plist file to load into launchd.
<?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>com.company.officeBackup</string>
<key>ProgramArguments</key>
<array>
<string>/Library/backups/officeBackup.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>Nice</key>
<integer>1</integer>
<key>StartCalendarInterval</key>
<dict>
<key>Hour</key>
<integer>1</integer>
<key>Minute</key>
<integer>0</integer>
<key>Weekday</key>
<integer>1</integer>
</dict>
<key>StandardErrorPath</key>
<string>/Library/backups/log/backupErrors.err</string>
<key>StandardOutPath</key>
<string>/Library/backups/log/backupOutput.err</string>
</dict>
</plist>
This file was placed into the /Library/LaunchAgents folder. Permissions were changed to make the owner “root” and the group “wheel”. It was then loaded into launchd with:
sudo launchctl load /Library/LaunchAgents/com.company.officeBackup.sh
It is loading, triggering my selected script, and triggering at the scheduled time. However, instead of running, I’m getting a ton of errors in my backupErrors.err. They almost all fall into one of seven patterns: `
rsync: chown “______________“ failed: Operation not permitted (1)
rsync: failed to set times on “______________”: Operation not permitted (1)
rsync error: some files could not be transferred (code 23) at ________________ [sender=2.6.9]
rsync: opendir “________________” failed: Operation not permitted (1)
rsync: mkstemp “______________” failed: Operation not permitted (1)
rsync: recv_generator: mkdir “_____________” failed: Operation not permitted (1)
*** Skipping everything below this failed directory ***
Further, the only message in my backupOutput.err is
IO error encountered -- skipping file deletion
Prior to setting this up, I created a file called “Canary.txt” on /Volumes/Office/. It contains a time/date stamp. Launchd has failed to copy this file to the clone drive, giving me strong indicators that it’s failing to copy other things as well. I’m genuinely confused, because I thought that launchd would run this script as Root. I also rebooted, and when the script triggered (on load), it still returned these errors.
Am I doing something wrong? Is there a way to make my script run with root access?