在 Python 中使用元数据(用户 ID 和 ctime)进行归档(tar 和压缩)

发布于 2024-08-20 00:05:31 字数 454 浏览 3 评论 0原文

我正在备份文件系统,我需要确保保存元数据(文件所有者和创建时间)。

Python 中的 tarfile 模块非常有用,我在我的解决方案中广泛使用它。但是,我无法使用保存元数据的文件创建 tar 文件(可能是因为 copycopy2 无法执行此操作)。

您将如何在 Python 中解决这个问题?

编辑:

只是为了向社区明确说明:Python 中的 tarfile 模块确实提供了通过 Tarinfo< 存储元数据的方法/代码> 对象。本质上,Tarinfo 对象是 Tar 对象的成员,它包含您可能需要的所有信息。请参阅已接受的帖子。

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

命比纸薄 2024-08-27 00:05:31

“想必”?你的意思是你不知道?你尝试过吗?也就是说,据我所知,tarfiles 不保留 ctime,并且它没有什么意义,因为解压时应该重置 ctime。不过,mtime 被保留,并且 tarfile 模块处理 mtime。

添加文件时,python tarfile 模块使用 TarInfo 对象。就像这样:

TarFile.addfile(tarinfo, fileobj=None)

TarInfo 对象包含文件信息:

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.

以及其他元数据的负载。请参阅http://docs.python.org/library/tarfile.html

"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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文