How to get rsync to complain if user not found

permissionsrsyncusers

I'm moving files over from my old server to my new server. By the time I'm doing it for real (as opposed to just testing) I'll have all the "user" users (the non-system users, i.e. >= 1000) created on the new server. My only fear is that I have some file that I'm moving over in the home directory that will belong to one of these users (say apache for instance) that doesn't exist on the new server. I'm using

rsync -az -e ssh src dest

as user on the new server to do the copying. It does preserve the usernames (as opposed to ids) for the users that exist. However instead of complaining about non-existent users, it just falls back on the numeric ids if the user isn't found. The behavior seems to be as described in this paragraph from the man page:

If a user or group has no name on the source system or it has no
match  on  the  destination system, then the numeric ID from the
source system is used instead.  See also  the  comments  on  the
"use  chroot" setting in the rsyncd.conf manpage for information
on how the chroot setting affects rsync’s ability to look up the
names of the users and groups and what you can do about it.

While I haven't read the entire man page word for word, what I have read doesn't offer me any options to complain about non-existent users. What is the best way to make sure that whatever users exists as owners/groups of files under a directory (say /home) exist on the new machine. If it's not doable with rsync, what's the best way to get a list of all the users/groups that exist so I can manually check that they exist on the new machine, or fix them before copying them.

Summary:

How do I make sure that after I run rsync, none of the files have been copied using numeric ids instead of name ids?

Best Answer

The rsync command doesn't have a mechanism for handling this directly, so I would use a different approach. I would scan the source filesystem tree, collecting the usernames (and groups) of all files present there:

# List of usernames owning files under 'src'
find src -printf "%u\n" | sort -u | tee /tmp/src.users

# List of group memberships for files under 'src'
find src -printf "%g\n" | sort -u | tee /tmp/src.groups

# Copy files to target system
scp -p /tmp/src.{users,groups} dest:/tmp/

I would then ensure that all users existed on the target system ready for rsync to use. Run these commands on the target system:

# List "missing" users
getent passwd | cut -d: -f1 | sort -u | comm -13 - /tmp/src.users

# List "missing" groups
getent group | cut -d: -f1 | sort -u | comm -13 - /tmp/src.groups
Related Question