Ubuntu 16.04 How to install rsync server for other systems to access via ssh using cron

Setup: system "prime" with Ubuntu 16.04 and a second hard drive just for backups.

/dev/sdb1 is mounted at /mnt via using blkid to get the uuid then adding a line to /etc/fstab

I created a subdirectory named /mnt/full/prime for the first backup, of the prime boot SSD contents.

--> Running the command:

sudo rsync -aAXv / --delete --ignore-errors --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} /mnt/full/prime

does indeed make a nice mirror copy of the root file system into that directory /mnt/full/prime

It is totally cool - like Robocopy with the /MIR option - it only transfers changes so runs a lot faster after the files have been copied the first time.

Now, to set it up for others to access, I found this article

How to Use rsync to Backup Your Data on Linux

It says to run the following command:

sudo apt-get install ssh rsync

it installed normally.
--EDIT: This turns out to be unnecesary. --END EDIT--

All of my systems have the passwordless login set up in both directions.

ssh-copy-id -i ~/.ssh/id_rsa.pub <target-system>

and I have no problem with passwordless scp copies and ssh.

So I then created a subdirectory under mnt/full for my next system "solar" - /mnt/full/solar and using ssh into the Raspbian system "solar" I issued the same rsync command but with the target directory being pi@prime:/mnt/full/solar

mkdir /mnt/full/solar
ssh solar
sudo rsync -aAXv / --delete --ignore-errors --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} pi@prime:/mnt/full/solar

but it prompted me for the password to pi@prime - rsync does not respect the normal passwordless login setup.

Once I entered the password the copy proceeded normally.

So now all seems well except this won't work as a daily cron job if it is going to prompt for a password.

The question is in the title: Ubuntu 16.04 rsync via ssh prompts me for password, how to make it a cron job?

  • 2
    I think it's that it works as the login user but not the sudo user. – Larry R. Irwin Jul 25 '17 at 18:09
  • 1
    Can you log in via SSH as super-user if you specify the key file manually, i. e. sudo ssh -i ~/.ssh/id_rsa pi@prime? Also, it may be better to run the job as your regular user altogether via user cron jobs. – David Foerster Jul 26 '17 at 2:02
  • Larry: I am running this from the command line, logged in as pi on both ends. David: You can see in the question that I did that precise command. But I also agree that I don't need this to be done automatically, really. I'll settle for just doing the entire file system once a month or so. I already have other cron jobs that use scp to transfer the data files that change on a daily basis. But it is a curiosity. – SDsolar Jul 26 '17 at 3:34
  • @SDsolar no, you show ssh-copy-id being run without sudo and rsync being run with sudo. – muru Jul 27 '17 at 1:47

This should do it:

sudo rsync -e 'ssh -i /path/to/pi's/id_rsa' -aAXv / --delete --ignore-errors --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/m‌​nt/*","/media/*","/l‌​ost+found"} pi@prime:/mnt/solar
| improve this answer | |
  • 1
    @SDsolar, It's because the command is executing as root - so it was looking in /root/.ssh or in /etc/ssh vs. looking in pi's home directory. – Larry R. Irwin Jul 26 '17 at 21:18
  • @SDsolar I rejected your edit, because -e is for setting the SSH command that rsync will use, using ssh-copy-id instead will simply cause rsync to fail. "e, --rsh=COMMAND specify the remote shell to use" – muru Jul 27 '17 at 2:23
  • When I do this it asks for a passphrase - showing that it found the correct /home/pi/.ssh/id_rsa.pub file. When I originally did ssh-keygen I made my key to have a null passphrase. --> I can ssh from the workstation terminal and get into the server without any prompting, so I know the keys are working... Ideas? – SDsolar Mar 6 '18 at 4:07
  • sudo ssh -i /home/pi/.ssh/id_rsa.pub pi@prime:/mnt/solar<br/> is not getting a prompt for a passphrase? – Larry R. Irwin Mar 7 '18 at 16:13
  • That's correct. I am mystified. For now have a set of scp scripts for directories that are being updated frequently. It works in cron with no problem. I don't know why rsync is different. – SDsolar Jun 10 '18 at 22:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.