Skip to content

Instantly share code, notes, and snippets.

@lox
Created April 19, 2011 20:00
Show Gist options
  • Save lox/929464 to your computer and use it in GitHub Desktop.
Save lox/929464 to your computer and use it in GitHub Desktop.
Synchronize two directories via fsevents and sftp
#!/usr/bin/env ruby
require 'rubygems'
require 'rb-fsevent'
require 'net/sftp'
remote_host = 'host.goes.here'
remote_user = 'ubuntu'
Net::SFTP.start(remote_host, remote_user) do |sftp|
fsevent = FSEvent.new
fsevent.watch %w(Projects/contests Projects/commerce) do |directories|
directories.each do |dir|
remote_dir = dir.gsub "/Users/lachlan/Projects", "/home/#{remote_user}"
remote_files = {}
# collect remote mtimes
sftp.dir.foreach remote_dir do |f|
remote_files[f.name] = f.attributes.mtime
end
# check local mtimes
Dir.foreach(dir) do |local_file|
f = "#{dir}#{local_file}"
if File.file?(f) and (!remote_files.key? local_file or remote_files[local_file] < File.mtime(f).to_i)
puts "Synchronizing #{f}"
sftp.upload! f, "#{remote_dir}#{local_file}"
end
end
end
end
fsevent.run
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment