rsync code will run, but not in cron

12,580

Solution 1

I had a similar issue. Mine was the HOME directory was encrypted.

If your user is logged, it works the known_hosts.

But when it's a cron, the cron uses the right user BUT it does not have access to your $HOME/~/.ssh directory because is encrypted :-(

Solution 2

Its hard to tell whether cron is using the wrong rsync binary or whether rsync requires some variable which is not being set in cron. Please set the stdout/stderr as shown below and pass on the output of the log file

Also, try doing a "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 [email protected]:/home/backups /home/myuser/odin > /tmp/cron_output.log 2>&1

EDIT :

Can you create a shell script called SOME_DIR/cron_job_rsync.sh which contains the following. Make sure you set the execute bit.

#!/bin/sh
/usr/sbin/rsync -avz -e ssh [email protected]:/home/backups /home/myuser/odin

And modify the cronjob as shown below

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

Solution 3

i got the same error just like you.

I finally found user home directory is an 'mount point', when logged in, it changed.

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

So, i logged in and 'cd /', then do

```

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

```

There is may failed, because you need to check the ${HOME} if you have the right to write, if not, try sudo or add writable to ${HOME}.

After that, every thing being fine.

Share:
12,580
peteyreplies
Author by

peteyreplies

Updated on July 26, 2022

Comments

  • peteyreplies
    peteyreplies almost 2 years

    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 [email protected]:/home/backups /home/myuser/odin
    

    If I enter this into the command line, everything rsyncs perfectly:

    myuser@jofur:~$ rsync -avz -e ssh [email protected]:/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 run every morning, so I edited my crontab to read this:

    0 4 * * * rsync -avz -e ssh [email protected]:/home/backups /home/myuser/odin
    

    This doesn't work. The following message is deposited in /var/mail/myuser:

    Could not create directory '/home/myuser/.ssh'. Host key verification failed. rsync: connection unexpectedly closed (0 bytes received so far) [Receiver] rsync error: unexplained error (code 255) at io.c(605) [Receiver=3.0.9]

    I'm not sure what this error means. I'm wary of futzing blindly with permissions because I don't want to leave any backdoors open. Any suggestions?

  • peteyreplies
    peteyreplies over 11 years
    Hi Kevin - "which rsync" returns: /usr/bin/rsync But when I add this to the end of my cron command: > /tmp/cron_output.log 2>&1 crontab fails to run at all, even when set to run every minute. I know this because when I set it to run every minute (* * * * * *) it will throw errors to mail, but if I put your code in, it doesn't throw any errors, nor does it update the output log.
  • peteyreplies
    peteyreplies over 11 years
    Hi Kevin - sorry for the delay. Here's what cron_output.log says: /bin/sh: 1: /home/user/scripts/cron_rsync.sh: Permission denied
  • souser
    souser over 11 years
    Make sure the execution bit is set for "cron_rysnc.sh". Also, the first line needs to be "#!/bin/sh"
  • peteyreplies
    peteyreplies over 11 years
    Hi Kevin - I was missing the exclamation point. Once I did that, the log file read /home/user/scripts/cron_rsync.sh: 2: /home/user/scripts/cron_rsync.sh: /usr/sbin/rsync: not found. Indeed, there was no rsync under /usr/sbin/, but there was under /usr/bin, so I modified cron_rsync.sh to read #!/bin/sh /usr/bin/rsync -avz -e ssh [email protected]:/home/backups /home/user/mydomain. Now the log reads receiving incremental file list sent 23 bytes received 2368 bytes 4782.00 bytes/sec total size is 497303674 speedup is 207989.83. Does this mean it's working?
  • souser
    souser over 11 years
    If the output matches what you see on the command line then yes. But please confirm by checking the file on the target server.
  • peteyreplies
    peteyreplies over 11 years
    This is odd - it didn't transfer at 4 AM. So I checked the output log and it read /bin/sh: 1: /home/user/scripts/cron_rsync.sh: not found.But that's definitely where cron_rsync.sh is, the execution bit is definitely set, and if I copy/paste the code in cron_rsync.sh to the command line then rsync definitely runs correctly.
  • souser
    souser over 11 years
    From the command line execute /home/user/scripts/cron_rsync.sh
  • peteyreplies
    peteyreplies over 11 years
    I did; it runs perfectly: user@jofur: /home/user/scripts/cron_rsync.sh receiving incremental file list backups/www/ backups/www/www-2013-01-11-06-51.tgz sent 70 bytes received 12598479 bytes 3599585.43 bytes/sec total size is 521668061 speedup is 41.41
  • Steve Robbins
    Steve Robbins over 10 years
    This isn't an answer, more a comment. Where is your solution?