I have a Bash script that executes an rsync command to sync files with a remote server.
#!/bin/bash
rsync -auvzP --exclude=.bundle --exclude=node_modules --exclude=tmp '/Volumes/Norman Data/me/.bash_profile' '/Volumes/Norman Data/me/Documents' --exclude=remote me@example.com:backup/
This works when I run it from the terminal as I have ssh key authentication set up for my user at example.com. However, when launchd calls this script, I get the following error indicating that it cannot find my user's key file or it is invalid:
Permission denied, please try again. Permission denied, please try again. me@example.com: Permission denied (publickey,password). rsync: connection unexpectedly closed (0 bytes received so far) [sender] rsync error: error in rsync protocol data stream (code 12) at /BuildRoot/Library/Caches/com.apple.xbs/Sources/rsync/rsync-52/rsync/io.c(453) [sender=2.6.9] I am specifying my user name in my plist file and when debugging, the script says it's being run by that same user both when running it from the terminal and from launchd alike.
My BackupDaemon.plist:
<?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>EnvironmentVariables</key>
<dict>
<key>HOME</key>
<string>/Volumes/Norman Data/me</string>
</dict>
<key>GroupName</key>
<string>staff</string>
<key>InitGroups</key>
<true/>
<key>Label</key>
<string>BackupDaemon</string>
<key>Program</key>
<string>/Volumes/Norman Data/me/backup</string>
<key>StandardErrorPath</key>
<string>/tmp/BackupDaemon.err</string>
<key>StandardOutPath</key>
<string>/tmp/BackupDaemon.out</string>
<key>StartCalendarInterval</key>
<dict>
<key>Hour</key>
<integer>16</integer>
<key>Minute</key>
<integer>6</integer>
</dict>
<key>UserName</key>
<string>me</string>
</dict>
</plist>
I can't figure out for the life of me what the issue might be or how to even go about further debugging. Any insight anyone has to share will be greatly appreciated. Thx!
-/Library/LaunchAgentsis for when you're logged in. See https://apple.stackexchange.com/a/249452/119271. You can run as a specific user Check this site for a tutorial: https://www.launchd.info/ – Allan Mar 16 '20 at 01:39/Library/LaunchDaemonsand run as root or the user specified with the keyUserName. I thought that's exactly what I had going on above... and it wasn't working. Am I missing something? – Daveh0 Mar 16 '20 at 02:16launchdto handle the user, let's try getting rsync to use an identity file. Add this to yourrsynccommand:rsync -auvzP -e ""ssh -i $HOME/.ssh/foo-bar-keyfile" ....rest of your command. This way, you're forcingrsyncto pick the appropriate keyfile to use. – Allan Mar 16 '20 at 06:13rsynccommand as suggested in the previous comment. @Allan, if you want to add that as an answer, I'll accept it. – Daveh0 Mar 17 '20 at 12:12