Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rsync code will run, but not in cron

Tags:

cron

backup

rsync

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?

like image 220
peteyreplies Avatar asked Dec 28 '12 17:12

peteyreplies


1 Answers

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
like image 174
souser Avatar answered Nov 03 '22 21:11

souser