Rsync code will run, but not in cron

I have a web server (odin) and a backup server (jofur). On jofur, I can run the following code to rsync my web directories (via key authentication) from odin to jofur:

rsync -avz -e ssh backups@odin.mydomain.net :/home/backups /home/myuser/odin 

If I enter this on the command line, all rsyncs are fine:

 myuser@jofur :~$ rsync -avz -e ssh backups@odin.mydomain.net :/home/backups /home/myuser/odin receiving incremental file list sent 23 bytes received 1921 bytes 1296.00 bytes/sec total size is 349557271 speedup is 179813.41 

I want this to start every morning, so I edited my crontab to read this:

 0 4 * * * rsync -avz -e ssh backups@odin.mydomain.net :/home/backups /home/myuser/odin 

This does not work. The following message is written to / var / mail / myuser:

Failed to create directory /home/myuser/.ssh. Error verifying host key. rsync: connection unexpectedly closed (0 bytes received) [Receiver] Error rsync: unexplained error (code 255) in io.c (605) [Receiver = 3.0.9]

I am not sure what this error means. I fear blind follow with permissions because I do not want to leave open backdoors. Any suggestions?

+4
source share
7 answers

It’s hard to tell if cron uses the wrong rsync binary or if rsync requires a variable that is not set in cron. Set stdout / stderr as shown below and pass the output of the log file

Also try running "which rsync" from the command line; this will tell you which rsync you are using from the command line.

  0 4 * * * rsync -avz -e ssh backups@odin.mydomain.net : / home / backups / home / myuser / odin> /tmp/cron_output.log 2> & 1

EDIT:

You can create a shell script called SOME_DIR / cron_job_rsync.sh that contains the following. Make sure you set the execution bit.

  #! / bin / sh
 / usr / sbin / rsync -avz -e ssh backups@odin.mydomain.net : / home / backups / home / myuser / odin

And change the cronjob as below

  0 4 * * * SOME_DIR / cron_job_rsync.sh> /tmp/cron_output.log 2> & 1
+2
source

I had a similar problem. My directory has been encrypted.

If your user is registered, it works with known_hosts.

But when it is cron, cron uses the right user, but it does not have access to your $ HOME / ~ / .ssh file, because it is encrypted: - (

+1
source

I got the same error as you.

Finally, I found that the user's home directory is a "mount point"; when I logged in, it changed.

You can use the mount shell command to check if you have the same way of using your home directory.

So, I logged in and "cd /", then do

`` ``

 cp -ar ${HOME}/.ssh /tmp/ sudo umount ${HOME} mv /tmp/.ssh ${HOME} 

`` ``

Perhaps this did not work because you need to check $ {HOME}, if you have the right to write, if not, try sudo or add an entry to $ {HOME}.

After that, everything is fine.

+1
source

Please follow the instructions below to avoid the error http://umasarath52.blogspot.in/2013/09/solved-rsync-not-executing-via-cron.html

0
source

I solved this problem by contacting the administrators of my server. Here is what they told me:

To improve security and performance, we use 1H (Hive), which uses a chrooted environment for users. Libraries and binaries must be copied to the chrooted environment to make them available.

They sent me the following email stating that Rellevent packages were installed. At this point, the problem was resolved. Unfortunately, I did not receive any additional information from them. The host was Arvixe, but I assume that anyone using 1H (Hive) will run into a similar problem. Hope this answer is helpful.

0
source

Use the rrsync script along with the dedicated ssh key as follows:

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@10.10.10.83 :/home/devel/.ssh 

REMOTE computer

 cat id_remote_backup.pub >> authorized_keys 

Prepare the following for the newly added row

 command="$HOME/bin/rrsync -ro ~/backups/",no-agent-forwarding,no-port-forwarding,no-pty,no-user-rc,no-X11-forwarding 

To make the result look like

 command="$HOME/bin/rrsync -ro ~/backups/",no-agent-forwarding,no-port-forwarding,no-pty,no-user-rc,no-X11-forwarding ssh-rsa AAA...vp Automated remote backup 

LOCAL

Put the following script with x resolution in crontab :

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

Source: http://www.guyrutenberg.com/2014/01/14/restricting-ssh-access-to-rsync/

0
source

I took a few steps to get it working.

  • Check your paths. For each command you will use check which [command] and use this full path for crontab

  • Open crontab as the user you want to run so that he has access to this ssh-key user

  • Add (remember user which) ssh-agent && [your ssh-command] so that it can connect via ssh.

  • When authentication is still not performed at this point. Try creating an ssh key without a password. This way you can skip the password hint.

For debugging, it is useful to add -vvv to the ssh command in rsync. This makes it clear what went wrong.

0
source

All Articles