How to automate rsync without asking for password prompt [closed]
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
开发者_开发技巧 Improve this questionI would like to automate the rsync task as a cron job. Since it needs the passphrase I am not able to do the cronjob. I need to specify the passphrase along with the rsync command or I will store the passphrase in a file and I will read from it. My command will look like this:
rsync -aPe "ssh -i ' . $server->{'ssh_key'} . '" ' . $server_lock_dir;
So where do I put the password ?
You don't need to do that - just need to set up a pair of ssh keys and put the public key in the remote system's .ssh directory.
Then you just do this:
rsync -a -e ssh /local/path/ server:/remote/path/
(Note that -e ssh
has been the default for quite a few years now, so you can probably omit it, unless you're using a very old version.)
There's a "how to" guide on setting up the keys here.
If you want this to work from cron, you have several possibilities:
- setup password-less ssh keys - not very secure
- setup password-less ssh keys, but combine them with ssh ForceCommand in the authorized_keys file to limit the commands that can be run with that ssh key. See man sshd, google on "ssh ForceCommand"
- setup passworded ssh keys, but combine them with keychain to keep the key in memory between sessions. I've written a blog post on this: ssh, ssh-agent, keychain and cron notes
If you want to copy files remotely:
- Make sure you have a public key on your local machine that can log into the remote machine.(in this case the my ssh-key is "/home/myaccount/.ssh/id_rsa"
- Specify local folder you want to sync with the remote, in my case "/home/myaccount/mysourcefolder"
- Specify the destination folder full path in the remote server, in my case remoteaccount@remoteserver:"/home/remoteaccount/mydestinationfolder/"
Note:
- --progress is to show progress for each file being copied
- -a to transfer recusively all files in the mysourcefolder
- -v for verbosity
- -z to compress data portions for small files
My command will look like below:
rsync -avz --progress -e "ssh -i /home/myaccount/.ssh/id_rsa" /home/myaccount/mysourcefolder remoteaccount@remoteserver:"/home/remoteaccount/mydestinationfolder/"
精彩评论