如何压缩另一个进程正在使用的文件?
我正在归档一个目录。 该目录有一个文件正在由另一个进程写入。 当我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当您用“Linux”标记问题时,您可能正在使用 LVM 分区。
如果您确实在 LVM 分区上运行,则可以使用 LVM 快照功能。
以下是相关有关如何执行该操作的 LVM 文档的链接。
以下是 LVM 快照简介的一部分:
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:
尝试先复制文件...
...然后将所有内容压缩在一起...
...并清理:
如果这不起作用,则持有文件句柄的进程不想共享读取访问权限...
Try copying the files first...
...then tarball everything together...
...and clean up:
If that doesn't work then the process holding the file handle doesn't want to share read access...
您的第二个输出是在第一个输出之后生成的,这是不对的。 我猜
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.您可能会发现这取决于所使用的文件系统和访问该文件的应用程序。 最接近通用的解决方案是使用支持快照的文件系统并在运行 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.
正如其他人所说,这取决于文件系统和文件系统。 正在使用的操作系统。 首先
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.