Ssh – rsync over ssh fails as cronjob works on shell – NAS4Free

cronrsyncssh

This is my rsync.sh in the home directory of my backup user

$ cat rsync.sh
  #!/bin/sh
  rsync -avvvz --stats --log-file="/var/log/rsync_client.log" -e "ssh -i /mnt/stor0/backup/backup-rsync-key" /mnt/stor0/home/ nas@remotehost:/mnt/backup/

when I run ./rsync.sh as backup user on the shell it works as it should (passwordless)

$ ./rsync.sh
opening connection using: ssh -i /mnt/stor0/backup/backup-rsync-key -l nas remotehost rsync --server -vvvlogDtprze.iLsf . /mnt/backup/
sending incremental file list
[sender] make_file(.,*,0)
.
.
.
list of files to backup

Now my cronjob

<job>
 <enable/>
 <uuid>1c7ad7f4-0daa-477b-9e7b-eb7e5861e3d3</uuid>
 <desc>run backup to nas</desc>
 <minute>0</minute>
 <hour>4</hour>
 <day/>
 <month/>
 <weekday>1</weekday>
 <weekday>3</weekday>
 <weekday>5</weekday>
 <all_mins>0</all_mins>
 <all_hours>0</all_hours>
 <all_days>1</all_days>
 <all_months>1</all_months>
 <all_weekdays>0</all_weekdays>
 <who>backup</who>
 <command>/mnt/stor0/backup/rsync.sh</command>
</job>

When I try to run the job, it fails and client.log shows

rsync error: unexplained error (code 255) at io.c(605) [sender=3.0.9]
rsync: connection unexpectedly closed (0 bytes received so far) [sender]

Why is the command running when I execute it manually and why does it fail as cronjob on NAS4Free?

Best Answer

When you run the command manually, all your own environment settings will be used. When you run it from cron, neither your environment settings nor e.g. your ssh agent settings will be available to the script.

In this case, since you're using SSH as a transport agent, I'd guess that something goes wrong with your ssh connection when you run it from cron. If you're normally using an ssh agent, you'll need to make sure that that information is available as environment variables to the script. If you're instead using a key with no password, there's something else that makes the connection fail. Is ssh in the $PATH used by cron? Can you make the connection using "ssh -v" to get further information?