views:

95

answers:

1

I am in the process of backing up a filesystem and I need to make sure that the metadata is conserved (the file owner and creation time).

The tarfile module in Python is really helpful, and I use it extensively in my solution. However, I cannot create the tar file with files conserving their metadata (presumably because copy and copy2 cannot do this).

How would you approach this problem from within Python?

EDIT:

Just to make it clear to the community: the tarfile module in Python does provide means to store metadata via the Tarinfo object. Essentially, a Tarinfo object is the member of a Tar object, and it has all the information you may need. Please refer to the accepted post.

Thanks!

+2  A: 

"Presumably"? You mean you don't know? Have you tried? That said, as far as I know, tarfiles doesn't preserve ctime, and there would be little point in it, as ctime should be reset when you unpack. mtime is preserved, though, and the tarfile module handles mtime.

The python tarfile module uses TarInfo objects when you add files. Like so:

TarFile.addfile(tarinfo, fileobj=None)

The TarInfo object contains the file information:

TarInfo.mtime
Time of last modification.

TarInfo.uid
User ID of the user who originally stored this member.

TarInfo.gid
Group ID of the user who originally stored this member.

And loads of other metadata. See http://docs.python.org/library/tarfile.html

Lennart Regebro
Please go ahead and downvote me... you are right... the Tarinfo fields contain everything I need. Sorry about the 'Lapsus Stupidus'.
Arrieta