ruby - 在 Linux 上使用 skript 同步两个目录的特殊类型

标签 ruby linux rsync

我在家中使用 Linux 服务器将我的照片以全分辨率存储在名为 pics/FULL 的目录中。为了加快通过我的 dlna 服务器(minidlna)的交付速度,我准备了第二个目录 pics/SMALL,具有相同的子目录名称和相同的文件名。但该目录中的所有图片都会转换为较小的分辨率。我通过一个小脚本来完成此操作,该脚本创建 SMALL,然后迭代 FULL 中的所有子目录并转换每张图片。

但是 FULL 是我的主目录。因此,如果我更改图片或路径(删除、旋转、向现有子目录添加一个附加图片、移动图片等),我总是在 pics/FULL 中执行此操作。现在我需要一个脚本,它只检测 SMALL 和 FULL 之间的变化(例如新图片/子目录、具有 FULL 时间戳的图片比 SMALL 中的相同图片更新)。该脚本应每晚运行。

我可以编写一个脚本(更喜欢 Ruby)来执行此操作,但我想知道是否已经有方法或 gem 可以执行此操作?与 rsync 类似,不处理差异,但为每个差异调用脚本?

也许有人对现有脚本有很好的提示?

汤姆

最佳答案

我建议使用rb-inotify gem(它是 linux inotify 内核子系统的包装器,它为您的应用程序提供有关文件系统更改的事件)。

使用它,您可以监控 FULL 目录并将每个操作镜像到 SMALL 目录中。

以下 stub 应该可以帮助您继续:

require 'rb-inotify'

notifier = INotify::Notifier.new
# you might want events like :moved_to etc. - have a look at the documentation :)
notifier.watch("path/to/FULL", :create, :modify, :delete) do |e|
  puts e.absolute_name # gives you the absolute path to the file/directory, which caused the event
  puts e.flags # gives you the event types that caused this event (e.g. :modified)
  if e.flags.include? :create
    if e.flags.include? :isdir
      # create matching dir in SMALL
    else
      # create new image in SMALL
    end
  elsif e.flags.include? :modify
    # generate new SMALL image if it's not a dir
  elsif e.flags.include? :deleted
    # delete matching dir/file in SMALL
  end
end

notifier.run # loops forever and waits for filesystem events
# alternatively use
notifier.process # blocks untils first filesystem changes was processed

请查看gem documentationinotify documentation (查看哪些事件类型是可能的)。

关于ruby - 在 Linux 上使用 skript 同步两个目录的特殊类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18175440/

相关文章:

javascript - 无法验证 Stripe Payments 的 CSRF token [Rails 4]

ruby-on-rails - 在关联的 Rails 控制台中创建模型?

ruby - 具有 NaN 值的变量在 Ruby 中不等于自身

ruby - 如何在我的类上动态设置 HTTParty 配置参数?

php - 如何确定哪个 PHP 代码打开了未关闭的 MySQL 连接

django - 跨机器同步 PostgreSQL 数据库的替代解决方案

python - Ubuntu 在使用 Firefox 的系统启动时运行 python 脚本

c++ - 如何检测终端中的unicode字符串宽度?

linux - 使用 rsync 时如何排除二进制文件

linux - 目标服务器上的 rsync 命令位于与源服务器上不同的目录中。如何使用rsync?