4

My apologies for the silly/simple question - yet after searching the web and SE, I cannot find an answer for this specific issue.

Question:
How does one change the owner and group (system-wide) only for files owned by a specific owner?

Use-case:
We have a number of RasPis running as various servers and use rsync to back them up. When we're unfortunate enough to have to perform a restore, the owner and group of all 'user' files is pi:pi, rather than the original owner adminuser:adminuser, for example.

Without hunting the files owned by pi, is where a way to accomplish the owner/group reassignment?

Edit: This is the rsync command:

sudo rsync -azh -e 'ssh -pNNNN' --stats --delete --exclude-from="${exc_path}" "${src_path}" "${dst_addr}:${dst_path}"
2
  • Sounds like you're not using -numeric-ids and/or -fake-super for your backups (and restores)
    – roaima
    Dec 5 '20 at 20:54
  • Thank you very much. We run the script containing this line each night, where the destination is an ServerBackups/.../rsync/ directory on the NAS server. The restore is the exact inverse ... basically just reverse the src_path and dst_path paths.
    – S3DEV
    Dec 5 '20 at 21:01
9

You're not using -numeric-ids and/or -fake-super for your backups (and restores). If you modify your rsync command a little you'll get the mappings saved and restored correctly.

In these examples, the -M tells rsync to apply the next option, i.e. the fakery, on the remote side of the connection. An extra side effect is you don't need the remote side (where the backups are stored) to run as root

This pushes the backups from the client to the backups server

sudo rsync -azh -e 'ssh -pNNNN' --stats --delete --numeric-ids -M--fake-super --exclude-from="${exc_path}" "${src_path}" "${dst_addr}:${dst_path}"

This would pull backups from the client (i.e. restore)

sudo rsync -azh -e 'ssh -pNNNN' --stats --delete --numeric-ids -M--fake-super --exclude-from="${exc_path}" "${dst_addr}:${dst_path}" "${src_path}"

And this, run on the backups server, would push the backups to the client (i.e. restore)

sudo rsync -azh -e 'ssh -pNNNN' --stats --delete --numeric-ids --fake-super "${dst_path}" "${src_host}:${src_path}"
2
  • Excellent, thank you very much for the time putting this together. I'll modify the scripts to use the --numeric-ids and -M--fake-super args. Much appreciated.
    – S3DEV
    Dec 5 '20 at 21:09
  • 1
    Just as a follow-up, I re-restored the backup using the two args, and the owner/group is back to how it should be. Thanks again mate!
    – S3DEV
    Dec 5 '20 at 21:32
6

Each file stores its owner information individually, there's no data structure to index files based on their owners. So, whatever you do, you will have to hunt for the files, and change the UIDs on each of them, individually. Fortunately, that's not very hard to do; this should do:

find "$dir" -user olduser -exec chown newuser {} +

Of course, it would be better to change the backup system to store (and restore) the correct UIDs, especially if you might have backed up files belonging to multiple users.

1
  • Thank you very much for taking the time to post this excellent answer. I'll keep this command in my pocket, and you're absolutely right with the 'fix the source' comment. That's the plan. Thank you.
    – S3DEV
    Dec 5 '20 at 21:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.