3

I use rsync to backup my remote server on my local device but when I combine it with a cron job my ssh times out. Just to be clear, the data is stored on a remote server and I want it stored on my local server. The backup request must be sent from my local server to the remote server.

The command for backup up the data is working when I just type it in terminal like this:

rsync -chavzP --stats USERNAME@IPADDRES: PATH_TO_BACKUP LOCAL_PATH_TO_BACKUP

but when I combine it with a cron job like this:

10 11 * * * rsync -chavzP --stats USERNAME@IP_ADDRESS: PATH_TO_BACKUP LOCAL_PATH_TO_BACKUP

the ssh connection times out. When the cronjob executes it send a mail to the root user with the output like this:

From local.xx.xx.xx  Tue Jul  2 11:20:17 2013
X-Original-To: username
Delivered-To: [email protected]
From: [email protected] (Cron Daemon)
To: [email protected]
Subject: Cron <username@server> rsync -chavzP --stats USERNAME@IPADDRES: PATH_TO_BACKUP LOCAL_PATH_TO_BACKUP
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=username>  
X-Cron-Env: <USER=username>
X-Cron-Env: <HOME=/Users/username>
Date: Tue,  2 Jul 2013 11:20:17 +0200 (CEST)

ssh: connect to host IP_ADDRESS port XX: Operation timed out
rsync: connection unexpectedly closed (0 bytes received so far) [receiver]
rsync error: unexplained error (code 255) at /SourceCache/rsync/rsync-42/rsync/io.c(452)          [receiver=2.6.9]

So the rsync command is working when just typed in terminal but not when used by a cronjob. Can anybody explain this?

2
  • have you tried putting the rsync command in a script and call the script in crontab? Jul 2, 2013 at 9:44
  • im trying that now Jul 2, 2013 at 9:47

1 Answer 1

5

When you run a command from cron, it doesn't have access to your environment information the same way that it would if it were run from the shell. This means that it doesn't find your ssh key, or whatever environment variables you are using to set your SSH agent.

If you're using a passwordless key, change your rsync line to this:

rsync -chavzP -e "ssh -i /Users/username/.ssh/YOUR_PRIVATE_KEY_HERE" --stats USERNAME@IP_ADDRESS: PATH_TO_BACKUP LOCAL_PATH_TO_BACKUP

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .