V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
cy97cool
V2EX  ›  问与答

Linux 下爬虫即将写满硬盘,有啥实时数据迁移方法?

  •  
  •   cy97cool · 2017-11-03 00:41:41 +08:00 · 1820 次点击
    这是一个创建于 2385 天前的主题,其中的信息可能已经有所发展或是发生改变。

    概括

    有爬取进程如wget正在持续对一个文件夹写入大量较小的文件,如何迁移已经完成写入的文件到其他服务器以腾出空间?

    需求

    • 不影响正在进行的数据爬取进程(不要迁移正在被写入的文件)
    • 迁移可靠,不会因为迁移而丢失数据
    • 迁移后所有数据都在新服务器上,旧服务器上 留下 0 字节文件 以便比对部分爬取是否失败(跳过已经存在的文件)
    • 不考虑加硬盘的方案 Orz

    更难一点的需求

    如果持续写入的不是多个小文件,而是一个整个大文件,即将写满硬盘

    这种情形下可不可能 分块完成迁移 且 对写入这个文件的进程 无感知?

    6 条回复    2017-11-03 08:46:51 +08:00
    oott123
        1
    oott123  
       2017-11-03 00:53:11 +08:00 via Android
    自己撸个 fuse 应该是能满足的
    cy97cool
        2
    cy97cool  
    OP
       2017-11-03 00:57:04 +08:00
    @oott123 写入进程已经在运行了。。。即使写 fuse 也来不及了吧 Orz
    cy97cool
        3
    cy97cool  
    OP
       2017-11-03 00:59:22 +08:00
    找到了个靠点边但不完全满足需求的:

    rsync 有这个参数可以复制完成之后删除,但它不能识别正在被写入的文件,而要求使用排除*.new 这种方法避免正在被写入的文件

    可以用这种方式多次调用逐步迁移进行写好的文件夹

    ```
    --remove-source-files
    This tells rsync to remove from the sending side the files (meaning non-directories) that are a
    part of the transfer and have been successfully duplicated on the receiving side.

    Note that you should only use this option on source files that are quiescent. If you are using
    this to move files that show up in a particular directory over to another host, make sure that the
    finished files get renamed into the source directory, not directly written into it, so that rsync
    can ’ t possibly transfer a file that is not yet fully written. If you can ’ t first write the files
    into a different directory, you should use a naming idiom that lets rsync avoid transferring files
    that are not yet finished (e.g. name the file "foo.new" when it is written, rename it to "foo"
    when it is done, and then use the option --exclude='*.new' for the rsync transfer).
    ```
    cy97cool
        4
    cy97cool  
    OP
       2017-11-03 01:04:53 +08:00
    可以用这种方式多次调用逐步迁移已经写完整的文件夹

    问题来了,有没有轮子自动完成这种事情:
    * 判断一个文件夹当前有没有被写入(也许比较 du -s 就够了)
    * 如果没有 此次循环跳过
    * 如果文件夹已经写好了,记录文件列表,调用 /通知远程调用 rsync --remove-source-files (服务器也许不能主动连接到数据迁移的目标服务器,比如迁移到自己电脑 自己电脑没有公网 IP ); rsync 完成后按文件列表恢复为 0 字节的文件
    * 循环直到爬虫进程结束&&所有文件迁移完成
    dobelee
        5
    dobelee  
       2017-11-03 01:48:40 +08:00 via Android
    这种写个小脚本就能完成吧,爬取的数据校验都不用。没必要搞那么复杂。
    stanjia
        6
    stanjia  
       2017-11-03 08:46:51 +08:00
    rsync + mount
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1032 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 20:41 · PVG 04:41 · LAX 13:41 · JFK 16:41
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.