Thursday, July 14, 2011

rsync with ssh on a different port (and sudo)

If you want to rsync data from/to a remote server with ssh, you could do something like this:

rsync -av [email protected]:/backup/* /storage/monday/

But what if ssh listens on port 2222?

rsync -av --rsh='ssh -p2222' [email protected]:/backup/* /storage/monday/

But what if you need sudo permissions?
Server-side configuration (/etc/sudoers)

#sudo access for myusername running rsync backup
myusername ALL=NOPASSWD:/usr/bin/rsync

Client-side:

rsync -av --rsh='ssh -p2222' --rsync-path="sudo rsync" [email protected]:/backup/* /storage/monday/ .

And in case you want to include multiple directories:

rsync -av --rsh='ssh -p2222' --rsync-path="sudo rsync" [email protected]:{/backup,/root,/etc} /storage/monday/