【问题标题】:OSX and launchctl, rsync/ssh can't find keyOSX 和 launchctl,rsync/ssh 找不到密钥
【发布时间】:2020-09-13 20:24:24
【问题描述】:

所以我一直在尝试为 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]

【问题讨论】:

    标签: macos ssh rsync launchctl


    【解决方案1】:

    我怀疑问题在于您如何传递参数。您在几个参数中的双引号是 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@...) 参数中删除双引号。

    【讨论】:

    • 做到了!非常感谢!
    猜你喜欢
    • 2018-09-13
    • 1970-01-01
    • 1970-01-01
    • 2017-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-17
    • 2014-12-12
    相关资源
    最近更新 更多