Linux – How to Save and Restore User-ID/Group-ID with rsync

backuplinuxpermissionsrestorersync

I'm trying to understand how to save and restore user/group while performing a backup using rsync.
It might be that I got it all wrong, but I was running the following command:

 rsync -axqHAXWESR --fake-super --no-R --rsync-path="rsync --fake-super" /path/to/src/ <user>@<ip>:/path/to/dst/

I then went to <ip> and listed files under /path/to/dst/. they were, unsurprisingly, belong to <user>. I then ran the following:

 getfattr -dm '' -- /path/to/dst/*

I'm not sure what all parts of the output means, but the end of it seemed like the correct user-id/group-id is saved. here is one example output:

# file: /path/to/dst/some_file
user.rsync.%stat="100755 0,0 1000:100"

(and indeed, originally some_file was owned to user-id 1000 and group-id 100).

I then restored the content from <ip> to my machine (into a different directory):

 rsync -axqHAXWESR --fake-super --no-R --rsync-path="rsync --fake-super <user>@<ip>:/path/to/dst/ /path/to/import/

files under /path/to/import had (unsurprisingly) the user-id/group-id of whoami (which didn't originally owned some of the files). using getfattr to files under /path/to/import/ showed that original user-id/group-id still saved inside the XATTR.

now, I'm trying to figure out how to apply those original user-id/group-id on the imported files, but can't seem to find how to do it.
I already tried to dump the XATTR into a file, using getfattr -Rd for the files, and then restore them using setattr --restore. But it didn't seemed to be working. user-id/group-id remained as those of whoami.

will much appreciate any help on this.

Best Answer

The attributes are rsync-specific; it is --fake-super that "restores" them.

But when restoring from such a backup, the rsync receiver does not need --fake-super, only the sender does. (On the sender side this option means "read rsync.%stat instead of looking at real stat", on the receiver side it means "write rsync.%stat instead of doing real chown/chmod".)

The receiver does, however, need to be run with root privileges as normal users cannot chown files.

Related Question