Ubuntu – How to use rsync via ssh in cron

cronrsyncssh

I have a private virtual server to which I'm trying backup some directories from local my /home directory.

I've prepared this script:

#!/bin/bash

/usr/bin/rsync -rvvv /home/arkadiusz/scripts root@XXX.XXX.26.106:/home/backup/

/usr/bin/notify-send "Backup complete" "~/scripts -> vps" -i cryptkeeper

In user's crontab I've added entry:

*/1 * * * * /home/arkadiusz/scripts/vps.sh > /home/arkadiusz/cron-vps.log 2> /home/arkadiusz/cron-vps.error.log

After executing the shell script, my scripts directory doesn't appear in /home/backup/ on the remote server. In cron-vps.log file I see only this:

opening connection using: ssh -l root XXX.XXX.26.106 rsync --server -vvvre.iLsfx . /home/backup/  (9 args)
[sender] _exit_cleanup(code=12, file=io.c, line=226): about to call exit(255)

In cron-vps.error.log file turns out that this is an authorization problem:

Permission denied, please try again.
Permission denied, please try again.
Permission denied (publickey,password).
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: unexplained error (code 255) at io.c(226) [sender=3.1.1]

I've checked additionally sshd status on my remote server:

root@vpsXXXXXX:/home/backup# service sshd status
● ssh.service - OpenBSD Secure Shell server
   Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2017-04-12 01:04:09 CEST; 12h ago
 Main PID: 281 (sshd)
   CGroup: /system.slice/ssh.service
           └─281 /usr/sbin/sshd -D

...

Apr 12 13:41:01 vpsXXXXXX.ovh.net sshd[5019]: Failed password for root from XXX.XXX.232.122 port 21782 ssh2
Apr 12 13:41:01 vpsXXXXXX.ovh.net sshd[5019]: Failed password for root from XXX.XXX.232.122 port 21782 ssh2
Apr 12 13:41:01 vpsXXXXXX.ovh.net sshd[5019]: Connection closed by XXX.XXX.232.122 port 21782 [preauth]

Based on these logs, it seems to be a problem with authorization, but my SSH connection works when I try to log in to my remote sever with this command:

ssh root@XXX.XXX.26.106

Also, the rsync command works in terminal without any passwords:

/usr/bin/rsync -rvvv /home/arkadiusz/scripts root@XXX.XXX.26.106:/home/backup/

What am I doing wrong?

Best Answer

In cron-vps.error.log file turns out that this is an authorization problem:

The authentication and authorization is a different problem. This is authentication, which fails (in this case the password, as visible from the logs).

Most probably you have set up your private key to be encrypted, but you do not notice it it normal sessions, because you have got it stored in the ssh-agent. It works fine in your session, but that won't work in the session that is initiated from your crontab.

Related Question