ezequiel-garzon
ezequiel-garzon

Reputation: 3117

Does `tar /home/user/file` change the /home owner to root?

I'm trying to back up some key files and directories of a machine, as root, including some of the /home data, hand-picking some files to reduce the tarball size. Everything is OK for the most part, since most files are owned by root anyway, but say I just try this:

# tar -cf backup.tar /home/user/file

When I restore the contents, /home/user/file is as expected owned by user, but /home/user is owned by root. I tried, however,

# tar -cf backup.tar /home

and in this case all /home owners are preserved. (Note that I don't need the -p flag as I'm root. Still I tried it...)

Is this normal behavior? If so, is there a way to hand-pick regular-user files to back up while keeping the /home ownership information? My goal is to simply untar everything from /.

Thanks!

Upvotes: 1

Views: 1182

Answers (1)

Michael Slade
Michael Slade

Reputation: 13877

To properly set permissions for directories, the tarball needs to contain entries for those directories, so you need to add them to the tarball.

When you create a tarball that only contains /home/user/file, and not /home/user, there is then no information about the permissions of /home/user in the tarball, so tar doesn't know what to do. It automatically creates the directories but has no permissions, owner or group to give them, so they get the defaults.

You could add the directories as well:

# tar -cf backup.tar --no-recursion /home/user /home/user/file

But this may not make things any simpler for you. Note the --no-recursion - that tells tar to not add everthing under a directory, just the directory itself. If you wanted to actually also add direcory trees, you would have to use find(1) to pass it every file under that directory manually. That would get ugly quickly.

Keep in mind that we are talking about backing up and restoring particular files. If you were to ever restore, you would presumably also create the unix accounts to restore to if they weren't there already. So there would be no need to set permissions on home directories, at least. The same cannot be said of subdirectories of those though.

Upvotes: 2

Related Questions