主要步骤

主要步骤

我在通过 ssh 使用 rsync 传输文件时遇到问题。如果我运行

rsync -avz [email protected]:/home/devel/ $/home/user/rsync/servidor

由于公钥存储在远程计算机中,因此它可以按预期工作,但如果我在 crontab 上运行同一行,

* * * * * rsync -avz [email protected]:/home/devel/ $HOME/rsync/servidor

同步失败。

在浏览网页后,我发现 crontab 无法看到私钥,因为它的运行受到一些限制,因此我将 crontab 更改为以下内容

* * * * * cd /home/user/rsync;/bin/sh transfer.sh >> /home/user/rsync/log/cron.log 2>&1

transfer.sh 的位置是

#!/bin/sh
#BASH_ENV=/home/user/.bashrc
echo ""
echo "Cronjob started for back-up files" `date`
set -xv
/usr/bin/rsync -avz -e "/usr/bin/ssh -i $/home/user/.ssh/id_rsa" [email protected]:/home/devel/ /home/user/rsync/servidor

现在的想法是 rsync 明确使用 ssh 私钥连接到远程机器,但是我仍然对这种方法有问题。日志:

Cronjob started for back-up files Qua Fev 4 13:39:02 BRST 2015
#/usr/bin/rsync -vv [email protected]:/home/devel /home/user/rsync/servidor
#rsync -avz -e “ssh -i /home/user/.ssh/id_rsa” [email protected]:/home/devel     /home/user/rsync/servidor
/usr/bin/rsync -avz -e "/usr/bin/ssh -i $HOME/.ssh/id_rsa" [email protected]:/home/devel/ $HOME/rsync/servidor
+ /usr/bin/rsync -avz -e /usr/bin/ssh -i /home/user/.ssh/id_rsa [email protected]:/home/devel/ /home/user/rsync/servidor
Permission denied, please try again.
Permission denied, please try again.
Permission denied (publickey,password).
rsync: connection unexpectedly closed (0 bytes received so far) [Receiver]
rsync error: unexplained error (code 255) at io.c(226) [Receiver=3.1.0]

然而,

/usr/bin/rsync -avz -e "/usr/bin/ssh -i $/home/user/.ssh/id_rsa" [email protected]:/home/devel/ /home/user/rsync/servidor

从终端运行正常。

操作系统:Linux mint Quaina

任何帮助,将不胜感激

编辑:我创建 SSH 密钥时使用了密码。权限如下:

ls -l /home/user/.ssh/id_rsa
-rw------- 1 user user 1766 Dez 16 15:40 /home/user/.ssh/id_rsa

答案1

EDIT2:我使用无释义密钥并将其限制为 rsync 解决了该问题。我按照这个解释得非常好的教程来实现它: http://www.guyrutenberg.com/2014/01/14/restricting-ssh-access-to-rsync/

主要步骤

远程服务器

mkdir ~/bin
gunzip /usr/share/doc/rsync/scripts/rrsync.gz -c > ~/bin/rrsync
chmod +x ~/bin/rrsync

本地计算机

ssh-keygen -f ~/.ssh/id_remote_backup -C "Automated remote backup"      #NO passphrase
scp ~/.ssh/id_remote_backup.pub [email protected]:/home/devel/.ssh

远程计算机

cat id_remote_backup.pub >> authorized_keys

当地的

#rsync -e "ssh -i $HOME/.ssh/id_remote_backup" -av [email protected]:/home/devel/ /home/user/servidor
* */2 * * * cd /home/user/sync;/bin/sh sync.sh >> /home/user/servidor/cron.log 2>&1

本地文件:/home/user/sync/sync.sh:

#!/bin/sh
echo ""
echo ""
echo "CRON:" `date`
set -xv
rsync -e "ssh -i $HOME/.ssh/id_remote_backup" -avzP [email protected]:/ /home/user/servidor 


chmod u+x /home/user/sync/sync.sh

相关内容