开发者

Archiving (tar and compress) with metadata (user id, and ctime) in Python

开发者 https://www.devze.com 2022-12-18 18:00 出处:网络
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).

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 pro开发者_如何学Goblem 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!


"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

0

精彩评论

暂无评论...
验证码 换一张
取 消