OSX和launchctl,rsync / ssh找不到密钥

问题描述

所以我一直在尝试为rsync编写launchctl守护程序,以便它每晚都可以远程备份我的笔记本电脑。

launchctl守护程序运行良好,它使用root用户调用rsync,并指示rsync使用ssh,并从我的用户目录中获取密钥文件。这就是有趣的地方。无论我做什么,ssh都会引发以下错误rsync: Failed to exec ssh -i /Users/anthony/.ssh/id_rsa: No such file or directory (2)

钥匙确实在那里。如果我从命令行单独调用ssh,则可以从我的用户帐户和root帐户使用该密钥。我假设这与launchctl的范围或特权有关?以下是launchctl使用的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>UserName</key>
    <string>root</string>
    <key>Label</key>
    <string>com.anthony.remoteBackup</string>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <false/>
    <key>StartCalendarInterval</key>
        <dict>
            <key>Hour</key>
            <integer>23</integer>
            <key>Minute</key>
            <integer>00</integer>
        </dict>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/bin/rsync</string>
        <string>-azb</string>
        <string>--exclude=.*</string>
        <string>--exclude=.*/</string>
        <string>--backup-dir="/home/anthony/Documents/Old Remote Backup/macbook_air/"</string>
        <string>--suffix=.old</string>
        <string>--delete</string>
        <string>--delete-excluded</string>
        <string>-e "ssh -i /Users/anthony/.ssh/id_rsa"</string>
        <string>/Users/anthony/Documents</string>
        <string>anthony@remote_domain_name:"/home/anthony/Documents/Remote Backups/macbook_air/"</string>
    </array>
    <key>StandardOutPath</key>
    <string>/tmp/remote_backup_test</string>
    <key>StandardErrorPath</key>
    <string>/tmp/remote_backup_test</string>
</dict>
</plist>

这是remote_backup_test的标准错误

rsync: Failed to exec ssh -i /Users/anthony/.ssh/id_rsa: No such file or directory (2)
rsync error: error in IPC code (code 14) at /AppleInternal/buildroot/Library/Caches/com.apple.xbs/Sources/rsync/rsync-54.120.1/rsync/pipe.c(86) [sender=2.6.9]
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: error in rsync protocol data stream (code 12) at /AppleInternal/buildroot/Library/Caches/com.apple.xbs/Sources/rsync/rsync-54.120.1/rsync/io.c(453) [sender=2.6.9]

解决方法

我怀疑问题在于您如何传递参数。您在多个参数中使用的双引号是shell语法,但是ProgramArguments数组的元素不会被shell解析,因此它们不应存在。最重要的是,-e之后的空格旨在由shell解析为参数定界符,因此请替换:

        <string>-e "ssh -i /Users/anthony/.ssh/id_rsa"</string>

具有:

        <string>-e</string>
        <string>ssh -i /Users/anthony/.ssh/id_rsa</string>

,只需从--backup-dir=和目标(anthony@...)参数中删除双引号即可。