23

I need to rsync a directory to a remote server so that all files belonging to user X and group Y on the source (local) machine are mapped to user W and group Z on the destination (remote) machine. If possible by using ssh as the transport, but if I need to use the rsync daemon it's fine as well.

Is there a way to do that? I'm looking for a way to establish an arbitrary user/group map, such as

local user X => remote user W
local group Y => remote group Z
... and as many of these as needed.

This should be a pretty common use case, isn't it? E.g. I have files on my local computer where my username is X, and I need to upload them to a web server where they need to belong to a given user which doesn't have either the same name or the same UID as my user on my personal computer.

I can't find that on rsync's man page...

Linux on both local and remote machine (Ubuntu local, CentOS remote)

Command I tried:

rsync -avz /path/to/local root@myhost.com:/path/to/remote
Walf
  • 491
matteo
  • 4,559

5 Answers5

27

Rsync version 3.1.0 introduced the --usermap and --groupmap options precisely for that purpose. See the man page.

Flimzy
  • 4,465
10

Last version (at least 3.1.1) of rsync allows you to specify the "remote ownership":

--usermap=tom:www-data

Changes tom ownership to www-data (aka PHP/Nginx). If you are using Mac as the client, use brew to upgrade to the last version. And on your server, download archives sources, then "make" it!

2

As mentioned in Federico's and Torque's comments, there is a simpler way for a typical use case, which might be useful to OP, but is generally helpful any way. The accepted answer maps only the specified user and group, leaving any others as-is, which may not be desired. You would need to add a fallback mapping or first (as below) or check the ownerships of everything on the sending side.

--usermap='X:W,*:root' --groupmap='Y:Z,*:root'

If you just need all sent files to match a specific user & group on the destination, then use this:

--chown=USER:GROUP

Obviously, replace USER & GROUP with the actual destination's ones.

Walf
  • 491
1

If you want to change ownership of files to arbitrary users, you'll first need to be root on the destination box.

I don't think there is such a feature integrated by rsync, but you can achive it by running a find after doing your rsync.

Maybe, a command like this will do the trick : For example, translate from UID 1000 => 505 and UID 1001 => 700 :

find /your/rsynced/path -user 1000 -exec chown 505 {} \;
find /your/rsynced/path -user 1001 -exec chown 700 {} \;

If you have many users, you may consider using a loop with a mapping, in your predilection language.

Have fun.

Adrien M.
  • 209
-1

I am not sure I understand, to connect over ssh you need to provide a username. That username will be user W on the remote machine who belongs to group Z. Therefore, everything will be transferred exactly as you want it to be:

rsync /path/to/local userX@remote.com/path/to/remote

EDIT in answer to the OP's comment.

If you want to do this user mapping and not lose permissions settings, don't use -a. First, run the rsync with mywww@myhost.com to get the right username. Then, instead of -a which will cause you to preserve ownership, specify the options manually. From man rsync:

    -a, --archive               archive mode; equals -rlptgoD (no -H,-A,-X)
    -r, --recursive             recurse into directories
    -l, --links                 copy symlinks as symlinks
    -p, --perms                 preserve permissions
    -t, --times                 preserve modification times
    -g, --group                 preserve group
    -o, --owner                 preserve owner (super-user only)
    -D                          same as --devices --specials

So, -a activates all of the above options but you don't want to preserve the group or owner. This command should do what you want:

rsync -rlptDvz /path/to/local mywww@myhost.com:/path/to/remote

Since you are now logging in as mywww and no longer preserving owner/group information, the copies made by rsync will belong to the mywww user.

terdon
  • 54,564