Tuesday, August 18, 2009

rsync, root and sudo

SkyHi @ Tuesday, August 18, 2009
Here is the thing, the other day I wanted to copy one subdirectory from one computer to another, I can not rely on scp because I needed root permissions, neither tar worked because there was symlinks, different file permissions and owners, and there wasn’t space enough to do it (of course, you can send the tar using netcat…). The perfect solution to do such a copy is use rsync, it works nice, and can be used to reupdate a backup, and so on.

The problem is I need both root permissions on both machines, on the local machine having root permissions is the easy part but how should we proceed to get root permissions at the other end ?

You can do several things, like creating the root user, disable sudo asking for password, … but I won’t recommend them. The solution I came across ( I don’t remember from where ) is simple, but quite forgivable (that’s why I’m writing a post-to-myself). Here it is:
view plaincopy to clipboardprint?

1. stty -echo; ssh myUser@REMOTE_SERVER "sudo -v"; stty echo
2. rsync -avze ssh --rsync-path='sudo rsync' myUser@REMOTE_SERVER:/REMOTE_PATH/ LOCAL_PATH

stty -echo; ssh myUser@REMOTE_SERVER "sudo -v"; stty echo
rsync -avze ssh --rsync-path='sudo rsync' myUser@REMOTE_SERVER:/REMOTE_PATH/ LOCAL_PATH

The second line tells sudo to execute “sudo rsync” instead of “rsync” on the remote host. Without the first line sudo will prompt for a password (and we won’t be able to input it), the “sudo -v” is the one which does the trick. It simply touches the timestamp sudo has to avoid asking the password on each call.

The “stty [-]echo” avoid others to have a look at our passwords while we type them


Reference: http://www.pplux.com/2009/02/07/rsync-root-and-sudo/