有没有一种方法可以连续地将日志从一个目录复制到另一个目录

有没有一种方法可以连续地将日志从一个目录复制到另一个目录

我在某个目录中有一个日志文件/A。考虑到更新/B中的文件也将更新文件中的文件,而不重写整个文件,是否可以在目录中维护相同的内容?/A/B

答案1

如果您可以使用定期更新,您可以使用rsync --append

rsync --append source destination
--append
          This  causes rsync to update a file by appending data onto the end of
          the file, which presumes that the data that  already  exists  on  the
          receiving side is identical with the start of the file on the sending
          side. 

如果这还不够,您还可以尝试该--append-verify选项。

答案2

我没有尝试过,但tail -fn+0应该可以。这应该适用于纯文本文件的日志。创建日志的程序必须附加到文件。

(将此保存到文件copylog.sh

#!/bin/bash

## This script reads log $1 and saves copy to $2

# Usage: copylog.sh source dest

tail -fn+0 "$1" > "$2"

然后运行./copylog.sh /var/log/log-1 /home/user/log.txt将日志复制/var/log/log-1/home/user/log.txt.

相关内容