class EasySync::Rsync

Attributes

current[R]
destination[R]
exclude_file[R]
last_snapshot[R]
logging[R]
source[R]
sync_name[R]

Public Class Methods

new(options) click to toggle source
# File lib/easy_sync/rsync.rb, line 9
def initialize(options)
  @sync_name = options[:sync_name]
  @source = options[:source]
  @destination = options[:destination]
  @exclude_file = options[:exclude_file]
  @logging = options.fetch(:logging){:off}
end

Public Instance Methods

current_snapshot() click to toggle source
# File lib/easy_sync/rsync.rb, line 21
def current_snapshot
  @current = File.join destination, Time.now.strftime("%Y-%m-%d")
end
latest_snapshot() click to toggle source
# File lib/easy_sync/rsync.rb, line 17
def latest_snapshot
  @last_snapshot = Dir["#{destination}/*"].max
end
remove_old_backups() click to toggle source
# File lib/easy_sync/rsync.rb, line 25
def remove_old_backups
  backups = Dir["#{destination}/*"]

  (backups - backups.last(5)).each do |r|
    puts "Removing #{r}"
    FileUtils.remove_dir(r, true)
  end

end
sync() click to toggle source
# File lib/easy_sync/rsync.rb, line 35
def sync
  commands = [
              "rsync",
              "-avhiPH",
              "--exclude-from", "#{exclude_file}",
              "--link-dest", "#{latest_snapshot}"
              ]

  commands << ['--log-file', "#{ENV['HOME']}/easy_sync.log" ] if logging == :on
  commands << ["#{source}", "#{current_snapshot}"]
  commands = commands.flatten

  puts "\n\n\n------------------ Running #{sync_name} ------------------\n\n\n"

  remove_old_backups

  puts "latest snapshot #{@last_snapshot}"

  IO.popen(commands).each_line do |l|
    puts l
  end
end