rsync, root and sudo
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:
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 🙂
| The solution […] is simple
Mmmm,
| stty -echo; ssh myUser@REMOTE_SERVER «sudo -v»; stty echo
| rsync -avze ssh –rsync-path=’sudo rsync’ myUser@REMOTE_SERVER:/REMOTE_PATH/ LOCAL_PATH
Yeah. Sure. Really simple. Trivial. Obvious. Everyone should be able to figure that out just by thinking a bit; no need of Internet or any documentation or help of anybody. I couldn’t think of any way of making that any easier.
These examples are what makes me love Unix simplicity. They make that and they make me wish I had a baseball bat and the person who created that shell commands near here. Just for having some fun, you know. Just some simple fun.
Was it sarcasm? Did unix hurt you? poor little guy… there, there, XDDD
You know, when someone spends hours reading errors out from malformed C++ templates, these things that are more or less readable, truly become quite simple!
BTW I would love to see a «simple» apple script do the same thing 🙂
Thanks, flipped it around a bit and backed up my /etc directory. I added an extra sudo…
sudo rsync –delete -avze ssh –rsync-path=’sudo rsync’ \
/etc REM_USER@REM_HOST:/backup/machines/laptop01
Very useful, now I’m ready to upgrade the OS.