bash - 本地副本更改时自动操作文件夹的远程副本

标签 bash ubuntu rsync inotifywait

EDIT: I see someone has voted to close this as "too broad a question", but the last time I posted asking how to do something, I was asked to provide more detail about what and why I wanted to do it. So... can't win! I'm only asking one question: "how to action remote copy of folder when local copy changes". I'm not asking how to rename, renumber or make zip files, just explaining that this is what I need to do as part of the copy. I really can't think of a less detailed way of asking this without the obvious (but wrong) answer being "just use rsync".



我想在 Ubuntu 18.04 上根据 Dropbox 文件夹(其他服务也可以添加文件)中完成的上传事件自动复制文件夹。我需要:
  • 保持源文件夹不变。
  • 如果还没有复制的文件名,则在数字上加上前缀 - ( find . -name '[[:digit:]]*.mp3' 等)
  • 清除撇号的文件名(例如,使用“detox”)。
  • 在远程端创建文件夹的 zip。
  • 如果将来源文件夹中有任何更改,请在远程副本上重新复制并重新创建 zip 文件夹。

  • 示例:20190203 的 SOURCE 文件夹
       apostrophe's.mp3
       track01.mp3
       zebra 4.mp3
    

    20190203的REMOTE文件夹(处理后)
       01-apostrophes.mp3
       02-track01.mp3
       03-zebra4.mp3
       20190302.zip
    

    如果远程用户在一个月后在源文件夹中添加 chickens.mp3 并删除 apostrophe's.mp3,则该过程将通过重新复制和重命名文件夹来更新远程文件夹,并自动重建 zip 文件。

    可能上传的所有单个文件都小于 10Mb,因此即使是最慢的连接也不会花费超过 15 分钟来上传任何一个文件,但上传整个文件夹可能需要长达 45 分钟。

    我无法根据文件夹大小、文件数量或修改日期检查更改,因为将 zip 文件添加到远程文件夹的操作将更改所有这些。

    目前,我有一个每小时 crontab 运行一个包含以下内容的脚本:
    SCANDIRS="$(find $BASEDIR -type f -mmin +15 -mmin -45 -printf "%h\\n" | sort -u | xargs -n 1 realpath)"
    然后它遍历scandirs并发挥作用,但这可能有很多我没有预见到的问题,每小时只能运行一次,并且不允许更新旧文件夹。

    我知道rsync -av --delete如果只是文件,则使用常规 crontab 可以工作,但我完全不知道如何做我想做的事。复制的文件夹将驻留在同一个本地文件系统上(然后获取 s3 sync 远程编辑,如果你想知道!)。

    我认为 inotifywait可能是一个解决方案,但我不确定如何处理“等到文件夹静止一段时间但允许将来随时更新”问题。

    谢谢你。

    最佳答案

    总结一下我的评论,一个用于检查更改的简单 bash 脚本框架可能类似于:

    SOURCE=/my/folder/to/check
    WORK=/my/state/folder
    
    is_initialised(){
        [ -f "$WORK/timestamp" ]
    }
    
    has_changed(){
        find "$SOURCE" -cnewer "$WORK/timestamp" | grep -q .
    }
    
    update_timestamp(){
        touch "$WORK/timestamp"
    }
    
    if ! is_initialised; then
        do_create_zip && update_timestamp || do_show_error
    elif has_changed; then
        do_update_zip && update_timestamp || do_show_error
    else
        echo "Nothing to do :)"
    fi
    

    关于bash - 本地副本更改时自动操作文件夹的远程副本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54505893/

    相关文章:

    linux - QDBus问题: getting org. freedesktop.DBus.Error.UnknownMethod,但方法存在

    linux - shell脚本rsync密码文件选项

    bash - 使用 rsync 或 scp 在多个级别或 SSH 跃点上复制文件

    linux - 按第一个字符复制文件夹的 Bash 脚本

    linux - udev 规则不起作用

    php - 如何从非拥有文件夹中的 apache 用户运行脚本?

    bash - 如何用find和sed生成随机数?

    ubuntu - 在 pbuilder 构建期间 : run setup. py --install-layout=deb 失败(无权写入/usr/lib/python2.7/dist-packages)

    bash - Cygwin 中的 Rsync 命令失败

    java - 我应该使用哪个 Eclipse 来使用 Java、C、Bash 和 Perl?