require 'pty' require 'expect' # # Synchronize files and directories between two hosts using Rsync. # # Author:: FUJIWARA Teruyoshi # Copyright:: Copyright (c) 2010 FUJIWARA Teruyoshi # License:: Distributes under the same terms as Ruby # Date:: July 3, 2010 # Version:: 1.0 # # == Note # This script requires 'pty' and 'expect' modules. # So be careful of follwoing issues: # * It won't work on Windows and requires Unix/Cygwin environment. # * Be careful of locale. It changes message from gpg and may cause trouble with 'expect'. class Rsync @@rsync_cmd = "/usr/bin/rsync" @@rsync_option = "-avz --exclude \'*.old\' -e ssh" @@verbose = false # Synchronize files and directories using rsync. # === Arguments # [src] data source # [dest] destination # [password] password of remote host # [delete] specify if deleting files remotely that no longer exist locally(true/false) # === Return value # * null def self.exec(src, dst, password, delete) if (delete) opt = "#{@@rsync_option} --delete" else opt = @@rsync_option end cmd = "#{@@rsync_cmd} #{opt} #{src} #{dst}" puts(cmd) if @@verbose begin PTY.spawn(cmd) do |r, w| w.sync = true puts("login...") if @@verbose r.expect(/password:/) { w.puts("#{password}\n") } puts("waiting finish.") if @@verbose r.expect(/total size/) do |l| fmt_message(l.to_s) puts("done.\n") if @@verbose end end rescue PTY::ChildExited => e puts("rsync process finished.\n") if @@verbose end end # Format message from rsync. (NOT implemented yet) # === Arguments # [message] message from rsync # === Return value # * formatted text def self.fmt_message(msg) puts(msg) end end