I'm having issues transferring files with rsync via ssh. If I just run

rsync -avz devel@ $/home/user/rsync/servidor

it works as expected since the public key is stored in the remote computer, but if I run this same line on crontab,

* * * * * rsync -avz devel@ $HOME/rsync/servidor

the synchronization fails.

After browsing the web, I came across that crontab is not able to see the private key since it runs with some restrictions, so I changed crontab to the following

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

where the transfer.sh is

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" devel@ /home/user/rsync/servidor

The idea is that now rsync explicitly use ssh private key to connect to the remote machine, but I still have problems with this approach though. The log:

Cronjob started for back-up files Qua Fev 4 13:39:02 BRST 2015
#/usr/bin/rsync -vv devel@ /home/user/rsync/servidor
#rsync -avz -e “ssh -i /home/user/.ssh/id_rsa” devel@     /home/user/rsync/servidor
/usr/bin/rsync -avz -e "/usr/bin/ssh -i $HOME/.ssh/id_rsa" devel@ $HOME/rsync/servidor
+ /usr/bin/rsync -avz -e /usr/bin/ssh -i /home/user/.ssh/id_rsa 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]

However, the line

/usr/bin/rsync -avz -e "/usr/bin/ssh -i $/home/user/.ssh/id_rsa" devel@ /home/user/rsync/servidor

runs ok from the terminal.

OS: Linux mint Quaina

Any help would be appreciated

EDIT: I used a passphrase when I created the SSH key. The permissions are:

ls -l /home/user/.ssh/id_rsa
-rw------- 1 user user 1766 Dez 16 15:40 /home/user/.ssh/id_rsa
  • Is /home/user/.ssh/id_rsa protected with a passphrase?
    – Kenster
    Feb 4 '15 at 16:20
  • yes. Added to EDIT Feb 4 '15 at 17:28
  • 1
    If your key is protected with a passphrase and you don't want to remove it, create another keypair without passphrase, and restrict it on the remote side to run only the specific rsync command.
    – Alex
    Feb 4 '15 at 20:31
  • I'll try it tomorrow and see how it goes Feb 5 '15 at 1:23
  • Please don't edit your question to include an answer. Instead, post a self-answer, including the notable steps you took to solve the problem. (Make sure your answer is not a link-only answer.)
    – user
    Feb 6 '15 at 12:30

EDIT2: I solved the issue using a paraphraser-less key and restricting it to rsync. I implement it by following this very well explained tutorial: http://www.guyrutenberg.com/2014/01/14/restricting-ssh-access-to-rsync/

Main steps

REMOTE server

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

LOCAL computer

ssh-keygen -f ~/.ssh/id_remote_backup -C "Automated remote backup"      #NO passphrase
scp ~/.ssh/id_remote_backup.pub devel@

REMOTE computer

cat id_remote_backup.pub >> authorized_keys


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

LOCAL file: /home/user/sync/sync.sh :

echo ""
echo ""
echo "CRON:" `date`
set -xv
rsync -e "ssh -i $HOME/.ssh/id_remote_backup" -avzP devel@ /home/user/servidor 

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.