1

Let's say that there are two dedicated servers, freshly installed, with identical software and users on both of them.

Suppose that, for whatever reason, I would like to copy ownership patterns from one of these servers to another. Is that possible?

FiddlingAway
  • 123
  • 6
  • 2
    `If that's not possible, what are my options?` That's simple: Restore from backup... – Sven Jun 22 '17 at 10:56
  • @Sven Which I don't have. :| – FiddlingAway Jun 22 '17 at 11:03
  • @Sven I have completely re-worded the question, so it's not a duplicate of the one linked (even though the cause for asking it is still the same). – FiddlingAway Jun 23 '17 at 09:20
  • DNS records can hardly be relevant to this question. – kasperd Jun 23 '17 at 10:29
  • @kasperd They were included in the question because there was a double setup of DNS records - one from within the Sentora admin panel which was on the server(s) in question, and the other (identical record setup) on the hosting account, on my hosting provider's site. I was unsure (`find` and `grep` came back empty, btw) whether my server was storing this info somewhere. – FiddlingAway Jun 26 '17 at 05:56

1 Answers1

1

The first paragraph makes hardly any sense, but your actual question:

Can I recover and set up file permissions and ownerships using another server as a reference/template?

The short answer is: maybe...

The longer answer is that such a strategy will fail for every file that does not exist on both servers. And as far as scripting goes, you'll need to take particular good care with special files, (symbolic) links, special characters in file/directory names etc. etc.

But both the chmod and chown commands support a --reference flag. You can point to an existing file and chmod will use the permissions of that file instead of you needing to supply MODE values when changing the file mode.
Similarly chown will use owner and group of that reference file/directory rather than specifying OWNER:GROUP values.

The exercise is then:

  1. Make the source, the reference directory trees you need available as a template on the destination server. (If you have sufficient empty space simply copy the source directory while preserving ownerships and mode settings, or alternatively use for instance NFS to export the source directory tree and mount that as the template.)
  2. Then run these crude find commands that execute chown and chmod:

(Please note that these are only conceptual and untested. Please add restrictions for find to only locate files and directories for instance and omit following symbolic links and such... )
To reset ownerships and modes on a corrupted /etc/:

cd /template/etc
find . -exec chmod -v --reference='{}' /etc/'{}' \;
find . -exec chown -v --reference='{}' /etc/'{}' \;
HBruijn
  • 72,524
  • 21
  • 127
  • 192
  • Sorry for taking this long to reply. Will try the suggested solution, and see if I break anything. I've thought about doing a similar thing - dumping permissions and ownerships from server1 and server2 into separate *.txt, then dumping and keeping only the same files and folders from both into a third *.txt, but wasn't really sure if it would work or not. – FiddlingAway Jun 30 '17 at 13:27
  • That would also work somewhat, i.e. to dump the permissions on `/etc/` in the form of a `chmod` script would look something like this: `find /etc/ -type f -exec stat -c "chmod %a %n" {} \;` where `/etc/` is a bad example, those are system files and you should using your package manager to restore permissions instead. – HBruijn Jun 30 '17 at 19:26