Score:0

cron rsync , how to redirect to a file on desktop

kn flag

I would like a regular backup of files on my disk to a backup server. Since I have lots of files (millions) I want to use rsync so only the differences are sent.

from Ubuntu 18.04 terminal, this produces output to a file:

rsync -av  /Disk2/ 192.168.1.80:/mnt/jjg-z2/39x-disk2/ &> /home/john/Desktop/nightly_disk2_logs.txt

But when I use crontab -e, for the 16 minute mark of the hour (to test)

16 * * * * rsync -av  /Disk2/ 192.168.1.80:/mnt/jjg-z2/39x-disk2/ &> /home/john/Desktop

I did not see the output in this file on my desktop. In the comments, steeldriver suggested the fix to problem 1. The crontab -e now looks like:

SHELL=/bin/bash
52 * * * * rsync -av  /Disk2/ 192.168.1.80:/mnt/jjg-14TB-z2/3970x-disk2/ &> /home/john/Desktop/nightly_disk2_logs.txt

Problem 2: Now I have output in the file but rsync has permissions issues. Is crontab's environment with respect to SSH keys not the same as the terminal's environment? Here is the output in the file:

Permission denied, please try again.
Permission denied, please try again.
[email protected]: Permission denied (publickey,password).
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: unexplained error (code 255) at io.c(235) [sender=3.1.2]
hr flag
Are you setting `SHELL=/bin/bash` in the crontab? Otherwise it will run in `/bin/sh` - which doesn't understand the bash specific `&>` redirection
hr flag
Yes add `SHELL=/bin/bash` as a separate line (anywhere) above - or change to POSIX redirection syntax `> /home/john/Desktop/nightly_disk2_logs.txt 2>&1`
cn flag
"Does crontab not use SSH keys?" No, what does crontab need ssh keys for!? Those are for external connections. So rsync needs them. I would suggest supplying it in the rsync command. --password-file and the file supplied needs 600 as permissions
grabbag avatar
kn flag
My wording could have been better. I should have said, "Is crontab's environment with respect to SSH keys not the same as the terminal's environment?" The answer is obviously no, given the --password capability in RSYNC you flagged. Thank for pointing this out.
grabbag avatar
kn flag
--password-file /home/john/.ssh/id_rsa.pub results in error The --password-file option may only be used when accessing an rsync daemon.
grabbag avatar
kn flag
I removed the pass phrase on the ssh key and now it works without --password-file. So It looks like SHELL=/bin/bash as first line is enough for crontab to give ssh that rsync uses access to the keys.
Score:0
kn flag

youtube describes the process.

My key takeaways:

  • no passphrase on ssh keys
  • add SHELL=/bin/bash as the first line of crontab -e
  • &> /home/john/Desktop/backup_disk2_logs.txt to create a log file too look at

My crontab -e looks like :

SHELL=/bin/bash
00 * * * * rsync -av  /Disk2/ 192.168.1.80:/mnt/jjg-z2/disk2/ &> /home/john/Desktop/disk2_logs.txt
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.