Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a backup.sh file that launch an rsync command. This rsync is to backup my dedicated server on a RaspberryPi running raspbian.

(I use keychain so I don't need to type any password etc ...)

The problem is that when I launch the batch manually everything works, but when it's crontab (with the same user) I have the following error :

2013/10/07 19:36:02 [6456] rsync: connection unexpectedly closed (0 bytes received so far) [Receiver]
2013/10/07 19:36:02 [6456] rsync error: error in rsync protocol data stream (code 12) at io.c(605) [Receiver=3.0.9]

Here is my backup.sh file

#!/bin/bash

echo "_ backup start "$(date +%H:%M:%S)
echo " "

/usr/bin/rsync -avh --rsync-path='/usr/bin/rsync' --delete --log-file='/home/user/rsync.'$(date +%d%m%Y-%H%M%S)'.log' --rsh='ssh -p 1234' user@server.com:/path/to/archives/ /media/backup/

echo " "
echo "_ backup end "$(date +%H:%M:%S)

And now the crontab line (crontab -e)

# m h  dom mon dow   command
 30 5  *   *   *     /home/user/backup.sh | mail -s "Backup RPi "$(date +\%d/\%m/\%Y-\%X) user+rpibackup@gmail.com

Regards

share|improve this question

3 Answers 3

up vote 3 down vote accepted

(I use keychain so I don't need to type any password etc ...)

Ok, so you need to tell the program running in your cron job how to find your keychain.

SSH looks for an SSH agent (which keychain emulates) via the environment variable SSH_AUTH_SOCK. So you need to set this environment variable in your crontab.

In a typical configuration, SSH_AUTH_SOCK is a path to a socket with a random name. Since you're using keychain, you can easily find an alternate name for that socket: keychain writes files to ~/.keychain that contain environment variable declarations that set SSH_AUTH_SOCK and other similar variables (SSH_AGENT_PID, GPG_AGENT_INFO). So just include the appropriate file in your cron job.

MAILTO=user+rpibackup@gmail.com
30 5  *   *   *     . ~/.keychain/$(hostname)-sh; /home/user/backup.sh

(Aside: cron has a built-in feature to send mail with the job's output, such that you receive a mail only if the job does produce some output. No need to fiddle with | mail.)

share|improve this answer
    
Thank you very much :) I discovered keychain only two days ago and I'm not very confortable with it. Anyway your answer solved my problem, so thank you very much again. And thank you for SSH mail tip but I'm using different email address something+crontask@gmail.com so I can easily label emails in Gmail. –  mephissto Oct 8 '13 at 19:45

The ssh connection isn't working from the cronjob. How is it supposed to be performing SSH authentication? No attempt to list an identity key or password is visible. I'd stick a '-i' with the path to your (unpassworded...) key if thats how its working. The main differences between command line and cronjob tends to be down to the environment (both things like environment variables, the lack of a controlling terminal etc)

share|improve this answer

I think you have a quoting problem on your subject:

mail -s "Backup RPi "$(date +\%d/\%m/\%Y-\%X) user+rpibackup@gmail.com

Should be this:

mail -s "Backup RPi $(date +\%d/\%m/\%Y-\%X)" user+rpibackup@gmail.com

Alternative Approach

I would wrap the email into the script as well.

#!/bin/bash

(
  echo "_ backup start "$(date +%H:%M:%S)
  echo " "

  /usr/bin/rsync -avh --rsync-path='/usr/bin/rsync' --delete \
     --log-file='/home/user/rsync.'$(date +%d%m%Y-%H%M%S)'.log' \
     --rsh='ssh -p 1234' user@server.com:/path/to/archives/ /media/backup/

  echo " "
  echo "_ backup end "$(date +%H:%M:%S)
) | mail -s "Backup RPi $(date +\%d/\%m/\%Y-\%X)" user+rpibackup@gmail.com

Then change the crontab entry to this:

30 5  *   *   *     /home/user/backup.sh

Also make sure that the account that is running this cron can in fact send email. Do a test.

share|improve this answer
    
It's working well, I've been using it like that for some time now. Thanks anyway ^^ –  mephissto Oct 8 '13 at 19:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.