0

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-keygen
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?

SDsolar
  • 3,169
  • 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 02: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 03:34
  • @SDsolar no, you show ssh-copy-id being run without sudo and rsync being run with sudo. – muru Jul 27 '17 at 01:47

1 Answers1

2

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
  • 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 02: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 06 '18 at 04:07
  • sudo ssh -i /home/pi/.ssh/id_rsa.pub pi@prime:/mnt/solar
    is not getting a prompt for a passphrase?
    – Larry R. Irwin Mar 07 '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
  • @SDsolar - I had the same error as you ... you are probably accidentally specifying the public key instead of the private key. In the command, which you are running from a host with a specific keypair, you specify to rsync where your private key is located, and then rsync negotiates that private key against the public key on the target. Something like this is needed rsync 'ssh -i /home/user/.ssh/id_rsa' /home/user/Server/ user@10.1.999.3:/home/user/Server/ - this is what Larry's post states, but your comment indicates you specified the public key by accident with the -i flag. – oemb1905 Apr 10 '19 at 02:45
  • @LarryR.Irwin - this is a perfect answer and follow up explanations in the comments. – oemb1905 Apr 10 '19 at 02:46
  • this is obviously an error in rsync ... whenever you type sudo it will prompt for a password, and it will not be the sudo password but the remote ssh password ... providing a path to the ssh key works but is not ideal as it requires knowing the home dir (not easy if you are executing a script and not hardcoded). i created ssh keys for root and still prompted .. not sure where it is looking when sudo is applied on rsync, and you need sudo to chown and chmod properly. I am on rsync 3.07 and to me this problem still exists. – mjs Nov 28 '19 at 01:08
  • note, that this is not the case when the source is not a url but local. if your target is a remote url, then it works without prompting for password. only when source is a url does it do this. – mjs Nov 28 '19 at 01:10