如何压缩另一个进程正在使用的文件?

发布于 2024-07-14 07:51:10 字数 544 浏览 6 评论 0原文

我正在归档一个目录。 该目录有一个文件正在由另一个进程写入。 当我使用 Linux tar/Perl Tar 模块压缩此文件时,在存档中该文件的条目在那里,但内容为空。

在压缩文件之前...

-rw-r--r--  1 irraju dba 28 Feb 18 02:22 a
-rw-r--r--  1 irraju dba 25 Feb 18 02:23 b
-rw-r--r--  1 irraju dba 29 Feb 18 03:38 c

解压之后

-rw-r--r-- irraju/dba       28 2009-02-18 02:22:58 a
-rw-r--r-- irraju/dba       25 2009-02-18 02:23:17 b
-rw-r--r-- irraju/dba        0 2009-02-18 03:33:12 c

如何解决此问题? 我希望该文件与存档时的内容一起保存在存档中。 该文件可以是日志文件,并假设我们在压缩之前无法关闭文件句柄。

I'm archiving a directory. This directory has a file that is being written by another process. When I tar this using Linux tar/Perl Tar module, in the archive the entry for the file is there but the contents are null.

Before tarring the files are...

-rw-r--r--  1 irraju dba 28 Feb 18 02:22 a
-rw-r--r--  1 irraju dba 25 Feb 18 02:23 b
-rw-r--r--  1 irraju dba 29 Feb 18 03:38 c

After untarring

-rw-r--r-- irraju/dba       28 2009-02-18 02:22:58 a
-rw-r--r-- irraju/dba       25 2009-02-18 02:23:17 b
-rw-r--r-- irraju/dba        0 2009-02-18 03:33:12 c

How can I fix this problem? I want the file to be in the archive with the contents it has at the instant it is archived. This file can be a log file and assume that we can not close the file handle before tarring.

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

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

发布评论

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

评论(5

挽你眉间 2024-07-21 07:51:10

当您用“Linux”标记问题时,您可能正在使用 LVM 分区。
如果您确实在 LVM 分区上运行,则可以使用 LVM 快照功能。

以下是相关有关如何执行该操作的 LVM 文档的链接。

以下是 LVM 快照简介的一部分:

LVM 提供的一个很棒的功能是“快照”。 这允许管理员创建一个新的块设备,它提供逻辑卷的精确副本,并在某个时间点冻结​​。 通常,当需要在逻辑卷上执行某些批处理(例如备份),但您不想停止正在更改数据的实时系统时,将使用此方法。 当快照设备使用完毕后,系统管理员只需删除该设备即可。 此功能确实要求在逻辑卷上的数据处于一致状态时创建快照 - LVM1 的 VFS 锁定补丁可确保某些文件系统在创建快照时自动执行此操作,并且许多文件系统会在创建快照时自动执行此操作。当创建快照时,2.6 内核中的文件系统会自动执行此操作,而无需修补。

As you tagged the question with "Linux" there's a chance you're using an LVM partition.
If indeed you're running on an LVM partition, you can use the LVM snapshot feature.

Here's a link to the relevant LVM documentation on how to perform the operation.

Here's a part of the LVM snapshot intro:

A wonderful facility provided by LVM is 'snapshots'. This allows the administrator to create a new block device which presents an exact copy of a logical volume, frozen at some point in time. Typically this would be used when some batch processing, a backup for instance, needs to be performed on the logical volume, but you don't want to halt a live system that is changing the data. When the snapshot device has been finished with the system administrator can just remove the device. This facility does require that the snapshot be made at a time when the data on the logical volume is in a consistent state - the VFS-lock patch for LVM1 makes sure that some filesystems do this automatically when a snapshot is created, and many of the filesystems in the 2.6 kernel do this automatically when a snapshot is created without patching.

浅黛梨妆こ 2024-07-21 07:51:10

尝试先复制文件...

cp a a.tmp
cp b b.tmp
cp c c.tmp

...然后将所有内容压缩在一起...

tar *.tmp abc.tar

...并清理:

rm *.tmp

如果这不起作用,则持有文件句柄的进程不想共享读取访问权限...

Try copying the files first...

cp a a.tmp
cp b b.tmp
cp c c.tmp

...then tarball everything together...

tar *.tmp abc.tar

...and clean up:

rm *.tmp

If that doesn't work then the process holding the file handle doesn't want to share read access...

白昼 2024-07-21 07:51:10

您的第二个输出是在第一个输出之后生成的,这是不对的。 我猜 tar 就在这里:当它执行其工作时,文件是空的。 您可能在这里处理竞争条件。

Your second output is made after your first, that can't be right. I'm guessing that tar is right here: when it was doing its job, the file was empty. You may be dealing with a race condition here.

丢了幸福的猪 2024-07-21 07:51:10

您可能会发现这取决于所使用的文件系统和访问该文件的应用程序。 最接近通用的解决方案是使用支持快照的文件系统并在运行 tar 之前创建快照。

You may find that this depends on the filesystem used and the application that is accessing the file. The closest to a generic solution is to use a filesystem that supports snapshots and create a snapshot before running tar.

东京女 2024-07-21 07:51:10

正如其他人所说,这取决于文件系统和文件系统。 正在使用的操作系统。 首先sync(或者文件系统上的任何等效项),将文件复制到临时目录,然后将它们打包。 如果文件系统不允许您复制打开的文件,那么您就完蛋了; Perl 无法绕过文件系统限制。

As others have said, it depends on the file system & OS being used. sync first (or whatever the equivalent is on your file system), copy the files to a temp directory and then tar them up. If the file system won't allow you to copy an opened file, then you're SOL; Perl can't get around file system limitations.

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