Method: Maid::Tools#sync
- Defined in:
- lib/maid/tools.rb
#sync(from, to, options = {}) ⇒ Object
Simple sync two files/folders using rsync
.
The host OS must provide rsync
. See the rsync
man page for a detailed description.
man rsync
Options
:delete => boolean
:verbose => boolean
:archive => boolean
(default true
)
:update => boolean
(default true
)
:exclude => string
:prune_empty => boolean
Examples
Syncing a directory to a backup:
sync('~/music', '/backup/music')
Excluding a path:
sync('~/code', '/backup/code', :exclude => '.git')
Excluding multiple paths:
sync('~/code', '/backup/code', :exclude => ['.git', '.rvmrc'])
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 |
# File 'lib/maid/tools.rb', line 607 def sync(from, to, = {}) # expand removes trailing slash # cannot use str[-1] due to ruby 1.8.7 restriction from = (from) + (from.end_with?('/') ? '/' : '') to = (to) + (to.end_with?('/') ? '/' : '') # default options = { :archive => true, :update => true }.merge() ops = [] ops << '-a' if [:archive] ops << '-v' if [:verbose] ops << '-u' if [:update] ops << '-m' if [:prune_empty] ops << '-n' if @file_options[:noop] Array([:exclude]).each do |path| ops << "--exclude=#{ sh_escape(path) }" end ops << '--delete' if [:delete] stdout = cmd("rsync #{ ops.join(' ') } #{ sh_escape(from) } #{ sh_escape(to) } 2>&1") log("Fired sync from #{ sh_escape(from) } to #{ sh_escape(to) }. STDOUT:\n\n#{ stdout }") end |