4

I have been stuck with a peculiar problem, where rsync command is not running when it is executed through crontab.
Below is the code :

#!/bin/sh -x
PATH=/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/mysql/bin:/opt/android-sdk-linux/tools:/opt/android-sdk-linux/platform-tools:~/usr/lib/jvm/jdk-6/bin
/bin/sh /etc/profile
MyUSER="root"       # USERNAME
MyPASS="password"         # PASSWORD
MyHOST="localhost"  # Hostname
Password="" #Linux Password

MYSQL="$(which mysql)"
if [ -z "$MYSQL" ]; then
echo "Error: MYSQL not found"
exit 1
fi
MYSQLADMIN="$(which mysqladmin)"
if [ -z "$MYSQLADMIN" ]; then
    echo "Error: MYSQLADMIN not found"
    exit 1
fi
CHOWN="$(which chown)"
if [ -z "$CHOWN" ]; then
    echo "Error: CHOWN not found"
    exit 1
fi
CHMOD="$(which chmod)"
if [ -z "$CHMOD" ]; then
    echo "Error: CHMOD not found"
    exit 1
fi

GZIP="$(which gzip)"
if [ -z "$GZIP" ]; then
    echo "Error: GZIP not found"
    exit 1
fi
CP="$(which cp)"
if [ -z "$CP" ]; then
    echo "Error: CP not found"
    exit 1
fi
MV="$(which mv)"
if [ -z "$MV" ]; then
    echo "Error: MV not found"
    exit 1
fi
RM="$(which rm)"
if [ -z "$RM" ]; then
    echo "Error: RM not found"
    exit 1
fi
RSYNC="$(which rsync)"
if [ -z "$RSYNC" ]; then
    echo "Error: RSYNC not found"
    exit 1
fi

MYSQLBINLOG="$(which mysqlbinlog)"
if [ -z "$MYSQLBINLOG" ]; then
    echo "Error: MYSQLBINLOG not found"
    exit 1
fi
# Get data in dd-mm-yyyy format
NOW="$(date +"%d-%m-%Y-%T")"

DEST="/home/db-backup"
mkdir $DEST/Increment_backup.$NOW
LATEST=$DEST/Increment_backup.$NOW
$MYSQLADMIN -u$MyUSER -p$MyPASS flush-logs
newestlog=`ls -d /usr/local/mysql/data/mysql-bin.?????? | sed 's/^.*\.//' | sort -g | tail -n 1`
echo $newestlog
for file in `ls /usr/local/mysql/data/mysql-bin.??????`
do
        if [ "/usr/local/mysql/data/mysql-bin.$newestlog" != "$file" ]; then
     echo $file
     echo $Password | sudo -S $CHMOD 0777 $file
         #sudo $MYSQLBINLOG $file>$file.$NOW.sql
     $CP "$file" $LATEST
     #$RM "$file.$NOW.sql"
     #$MV $file.sql.gz /$LATEST
        fi
done
for file1 in `ls $LATEST/mysql-bin.??????`
do
 $MYSQLBINLOG $file1>$file1.$NOW.sql 
 $GZIP -9 "$file1.$NOW.sql"     
 $RM "$file1"
done
 $RSYNC -v -e ssh $LATEST abc@192.168.1.9:/home/rsync-backup/
#FILE=$LATEST/"mysql-bin.??????"
#$MYSQLBINLOG $FILE>$FILE.$NOW.sql
#$GZIP -f "$FILE.$NOW.sql"
pwd

Rsync happens when the code is run manually, but fails when it is run through crontab. Rest of the commands are working fine. From the logs I got this information:

Host key verification failed.^M
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: unexplained error (code 255) at io.c(600) [sender=3.0.6]
Rudra
  • 199
  • 2
  • 6
  • 11
  • It can be a permission problem. Are you running the cron job under same user when running the script manually?? – Khaled Jan 31 '13 at 12:20
  • What means "fails"? Can you show exact output in that case? – gelraen Jan 31 '13 at 12:39
  • Thanks for the reply Gelraen. Fails means the script is running fine when executed manually, but in crontab nothing happens, no transfer of files, etc. – Rudra Jan 31 '13 at 12:40
  • Please show us your crontab entry. Additionally make sure you have error logging there also like this: '...script.sh >>/tmp/logfile 2>&1' – grassroot Jan 31 '13 at 14:15
  • */1 * * * * /home/db-backup/incrementaltest.sh>>/home/rsynclst 2>&1 is my cron entry. I'm getting this error **Host key verification failed.^M rsync: connection unexpectedly closed (0 bytes received so far) [sender] rsync error: unexplained error (code 255) at io.c(600) [sender=3.0.6]** – Rudra Jan 31 '13 at 16:35

3 Answers3

2

ssh (which rsync executes) doesn't trust the host key of the server, and since there is no interactive terminal to prompt the user it simply fails. The cause is probably that ssh is looking at a different known_hosts file when running under cron that when you execute it manually.

If the cronjob runs as a different user to you, you need to manually accept the host key by doing something like this:

sudo -u cronuser HOME=/home/cronuser ssh abc@192.168.1.9

If the cronjob is running as your user then $HOME probably isn't set correctly. Specify that explicitly in the crontab:

HOME=/home/vishu
mgorven
  • 30,615
  • 7
  • 79
  • 122
  • Thanks for the reply mgorven. I'm running this script as a root. The script runs with rsync when executed manually, but is failing on crontab, should I fix this with some environment variable. – Rudra Feb 01 '13 at 07:12
  • You could add the option `-o StrictHostKeyChecking=no` to the ssh command in your script. – Jenny D Feb 01 '13 at 10:40
  • @Vishu Try setting `HOME=/root` in the crontab. Are there any prompts about SSH when running the script manually? – mgorven Feb 01 '13 at 17:47
1

Thank you all....got the answer finally. When I was running it manually I was running as a local user. But, on cron I was running it as root user, for which ssh key was not generated. I did ssh-keygen and created the keys. Now it works.

Rudra
  • 199
  • 2
  • 6
  • 11
0

I faced a similar error. This solved my problem: ssh to host machine where data is migrated with rsync. After which the host IP was Permanently added to list of known hosts.

Next I ran the same rsync command, and it executed just fine!

madhavi
  • 101
  • 2