Host key verification failed [rsync: connection unexpectedly closed]

15,007

Solution 1

This is basically due to the first time authentication issue for ssh. If you were to ensure that the host is added to known_hosts manually or have an expect for the prompt in your script, it should work.

The authenticity of host '[IP]:20022 ([IP]:22)' can't be established.
RSA key fingerprint is bc:87:52:cf:ac:3e:67:74:1b:e1:0b:e3:e2:06:d8:21.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[IP]:22' (RSA) to the list of known hosts

Solution 2

This kind of error is usually caused by differences in the environment. A good troubleshooting step is to run "env" at the start of the program and compare the cron and non-cron version.

You should also run the command as "sh -x" which will show you all the expansions which will help identify which variable is not being set properly.

Share:
15,007
Rudra
Author by

Rudra

Updated on June 04, 2022

Comments

  • Rudra
    Rudra almost 2 years

    I'm 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 [email protected]:/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]
    
  • mae
    mae almost 3 years
    I've added it to known_hosts manually and the issue did not go away. Perhaps rsync is telling ssh to look for known_hosts somewhere else and it's just not seeing it?