10

I'm installing some of my data from my old server to my new server.

Since I had my old server for ages, I have a huge amount of legacy data with, most certainly, legacy user and group names.

When extracting, tar does its best to match the user and group info by name and uses the identifiers as a fallback or the current user as a last resort.

What I'd like to do is make sure that all the users and groups exist before I do the extraction. That way all the files get the correct ids.

To do that, the best way I can think of is to list all the user and group names found in the tar file. I know I can use the tar tvf backup.tar command to list all the files, but then I'd have to come up with a way to extract the right two names.

I'm wondering whether there would be a simpler way than using the tv option. Some tool or command line options that only extracts the user name and group name, the I can use sort -u to reduce the list to unique entries.

Anyone knows of such a feature?

15

Interesting question. From a quick look through the man page (searching for "user" and when that didn't turn up results, searching for "owner") the following should do it:

tar xf thetarball.tgz --to-command='sh -c "echo $TAR_UNAME $TAR_GNAME"' | sort | uniq -c

Obviously, change the script according to your needs. You might want $TAR_UID and $TAR_GID instead of the names for some use cases.

I recommend also that you read up on the --owner-map and --group-map options for tar; they sound like they could greatly benefit your use case and would be a lot simpler than creating all the users and groups ahead of time.

  • Well! Look at that! The manual has evolved quite a bit since I last look at it. What I want really are the $TAG_UNAME and $TAR_GNAME parameters. The mapping is probably a good idea for some of the entries, but I know for quite a few I want the user on the new system. It's just difficult to make sure I have just the ones I need without a specific list. (Opposed to creating all the users I had on the old system...) – Alexis Wilke Oct 14 at 20:06
  • @AlexisWilke, great, glad I could help! I've edited the suggested command to more precisely suit what you're asking for. (Don't forget to accept the answer if it solved your question.) ;) – Wildcard Oct 14 at 20:10
  • 1
    He! He! I actually first clicked on the checkmark 14 seconds to soon :-) – Alexis Wilke Oct 14 at 20:30
  • 1
    I would just use sort -u instead of sort | uniq -c. Uniq count is probably not necessary here. – Christopher Hunter Oct 15 at 18:14
  • An important note about the mapping. It has to be done when you generate the tarball, now when you extract it. So if you already created it, you'll want to look into regenerating it. Also, the $TAG_UNAME and $TAG_GNAME need to be checked on the origin server. On the destination those that are missing are going to come out as an empty string! – Alexis Wilke Oct 23 at 5:24
7

Quickly assembled:

groups:

tar tvf thetarball.tgz | awk '{print $2}' | cut -d/ -f2 | sort -u

users:

tar tvf thetarball.tgz | awk '{print $2}' | cut -d/ -f1 | sort -u

user/groups pairs:

tar tvf thetarball.tgz | awk '{print $2}' | sort -u
  • 3
    Note that sort -u is probably faster than sort | uniq. – Alexis Wilke Oct 14 at 20:29
  • @AlexisWilke I upvoted your comment and edited the answer. – Eduardo Trápani Oct 14 at 20:36
  • 2
    (or just awk: awk '!seen[$2]++ {print $2}') – muru Oct 15 at 4:24

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.