I would like to run the following: (Centos 5.4)
rsync -avz /source_folder 192.168.1.1:/destination_folder
right now I'm running it from CLI but eventually I would like to run it from crontab (user:root).
when I run it, it always asks me to enter root password. I want to skip the password. I've seen a few of the answers regarding this but it either doesn't work or I'm not sure I understand completely how to do it. can someone assist?
As others have said you can do this with passphrase-less ssh keys. This is inherently insecure and you need to take additional steps to add some security back into the process. What I do is restrict the key's use to a host and a command. I've written this up from my notes, I don't think I've missed any steps out.
The first thing to do is to generate a public key pair that will be used solely for this backup process. When prompted for a pass phrase just press enter twice.
Transfer the public key
/root/.ssh/backup.id_rsa.pub
to the remote host and add it to the/root/.ssh/authorized_keys
file.On the remote host restrict the use of the public key by adding
from=
andcommand=
restrictions. Edit the/root/.ssh/authorized_keys
file, locate the backup key and add the address/name of the host that will be running the cron job as well as a command to run. This command will be run when something logs in with this key e.g.You can only use this key from 192.168.254.207 and you can only run the command /root/checkrsync and other functionality has been removed too.
Edit the file /root/checkrsync like this
echo $SSH_ORIGINAL_COMMAND >/tmp/rsync.cmd
save it and make it executable
chmod 700 /root/checkrsync
We'll change
/root/checkrsync
later once we know what $SSH_ORIGINAL_COMMAND is.On the host where we'll be running the cron job run the rsync command interactively
Don't worry about any error messages we only want to generate the /tmp/rsync.cmd file on the remote host
On the remote host make an exact note of the contents of the /tmp/rsync.cmd file (it will be something like
rsync --server -vlogDtprze.iLs . /destination_folder
).On the remote host edit
/root/checkrsync
and change it towhere the right hand side of the if statement is what was in /tmp/rsync.cmd.
Test the setup. On the host where we'll be running the cron job run the rsync command interactively again
Now it should run correctly if it does then install it into your crontab.
You need to set up passphrase-less ssh key authentication.
Bear in mind security concerns when setting this up. I recommend using a host access list to restrict the source IP using the key.
Check following link for seting up SSH keys. SSh keys will help you to setup password less ssh login.
http://techtrunch.com/linux/ssh-keys
Setting up CRON:
Open a terminal and type
crontab -e
, then enter following1 * * * * rsync -avz /source_folder 192.168.1.1:/destination_folder
Note
This will run on every hour.
For ssh without password (or rsync over ssh), you'll need a key pair without a passphrase.
You can generate your the pair (private & public key) on your source machine by
ssh-keygen -t rsa
(to~/.ssh/id_rsa
), then add your public key (contents of~/.ssh/id_rsa.pub
) to the destination machine to~/.ssh/authorized_keys
.Read the whole article on cron rsync backups: https://medium.com/@deltazero/linux-remote-backup-rsync-chroot-d797ba6babe5