How can I use rsync via ssh in cron?
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 videos on Youtube
Arkadiusz Wieczorek
I'm focused on new web technologies especially connected with JavaScript, co-founder of internet-czas-dzialac.pl Sealcode.org, Front-end developer at Pearson IOKI. After hours podcaster at polish podcast "Internet. Czas działać!"
Updated on September 18, 2022Comments
-
Arkadiusz Wieczorek over 1 year
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 [email protected]:/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. Incron-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 [email protected]
Also, the
rsync
command works in terminal without any passwords:/usr/bin/rsync -rvvv /home/arkadiusz/scripts [email protected]:/home/backup/
What am I doing wrong?
-
Arkadiusz Wieczorek about 7 yearsWhat can I do to get it working? If I understand correctly it's a problem with my private key. Is it depends on access to file by permissions and cron has no access to this?
-
Jakuje about 7 yearsVerify if your key has a passphrase/is encrypted. If so, either remote the passphrase or generate separate key in different location used for your rsync and make sure you can connect with it ( use switches
-oIdentityFile=/path/to/key -oIdentitiesOnly=yes
). -
Arkadiusz Wieczorek about 7 yearsThanks @Jakuje and @Ravi I've used key without passphrase and works great under cron:
/usr/bin/rsync -rvvv -e "ssh -i $HOME/.ssh/backup-key" /home/arkadiusz/scripts [email protected]:/home/backup/