How to rsync with sudo on both sides
When migrating from one server to another, you will likely want to use rsync
over SSH to migrate directory trees. It's likely that these trees will not all be owned or even readable by you, meaning sudo
is necessary to make the transfer.
It's possible to make rsync
use sudo
on both ends of the transfer, meaning that in combination with the -a
flag, ownership and permissions are maintained even given a mix of file owners & permissions.
To do this, run a command like the following, which transfers /opt/docker
from source
to destination.example.com
. This particular invocation is run on source
:
me@source$ sudo rsync -aX \
-e 'ssh -T -q' \
--rsync-path='sudo rsync' \
/opt/docker/ me@destination.example.com:/opt/docker/
With this particular invocation, the remote user will need the ability to run sudo rsync
without entering a password. To do this, add a file to /etc/sudoers.d
with the following content, replacing me
by your Linux username:
me ALL=(ALL) NOPASSWD: /usr/bin/rsync
You can remove this file once you're done transferring files to the new machine..