Skip to content

Instantly share code, notes, and snippets.

@gcatlin
Created February 6, 2012 04:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gcatlin/1749686 to your computer and use it in GitHub Desktop.
Save gcatlin/1749686 to your computer and use it in GitHub Desktop.
Automatic rsync
#!/usr/bin/env ruby
require 'rb-fsevent'
require 'open3'
ignore_dirs = ['.git', '.hg', '.svn']
def popen cmd
Open3.popen3 cmd do |stdin, stdout, stderr|
stdout.read.split("\n").each do |line|
puts "#{Time.new} #{line}"
end
end
end
src = File.absolute_path ARGV[0] unless !File.directory? ARGV[0]
src << '/' unless src.end_with? '/'
dst = ARGV[1].dup
dst << '/' unless dst.end_with? '/'
rsync_opts = "-dlptgoDvz" # --delete"
rsync_excludes = ignore_dirs.collect {|d| "--exclude=" + d}.join(" ")
popen "rsync #{rsync_opts} #{rsync_excludes} #{src} #{dst}"
ignore_dirs.collect! {|d| "/" + d + "/"}
fsevent = FSEvent.new
fsevent.watch src do |dirs|
dirs.each do |dir|
unless ignore_dirs.any? {|d| dir.include? d}
dir.slice! src
popen "rsync #{rsync_opts} #{rsync_excludes} #{src}#{dir} #{dst}#{dir}"
end
end
end
fsevent.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment