Class: EasySync::Rsync

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_sync/rsync.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Rsync

Returns a new instance of Rsync.



8
9
10
11
12
13
14
# File 'lib/easy_sync/rsync.rb', line 8

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

Instance Attribute Details

#currentObject (readonly)

Returns the value of attribute current.



6
7
8
# File 'lib/easy_sync/rsync.rb', line 6

def current
  @current
end

#destinationObject (readonly)

Returns the value of attribute destination.



6
7
8
# File 'lib/easy_sync/rsync.rb', line 6

def destination
  @destination
end

#exclude_fileObject (readonly)

Returns the value of attribute exclude_file.



6
7
8
# File 'lib/easy_sync/rsync.rb', line 6

def exclude_file
  @exclude_file
end

#last_snapshotObject (readonly)

Returns the value of attribute last_snapshot.



6
7
8
# File 'lib/easy_sync/rsync.rb', line 6

def last_snapshot
  @last_snapshot
end

#loggingObject (readonly)

Returns the value of attribute logging.



6
7
8
# File 'lib/easy_sync/rsync.rb', line 6

def logging
  @logging
end

#sourceObject (readonly)

Returns the value of attribute source.



6
7
8
# File 'lib/easy_sync/rsync.rb', line 6

def source
  @source
end

#sync_nameObject (readonly)

Returns the value of attribute sync_name.



6
7
8
# File 'lib/easy_sync/rsync.rb', line 6

def sync_name
  @sync_name
end

Instance Method Details

#current_snapshotObject



20
21
22
# File 'lib/easy_sync/rsync.rb', line 20

def current_snapshot
  @current = File.join destination, Time.now.strftime("%Y-%m-%d")
end

#latest_snapshotObject



16
17
18
# File 'lib/easy_sync/rsync.rb', line 16

def latest_snapshot
  @last_snapshot = Dir["#{destination}/*"].max_by{|s| File.mtime s}
end

#syncObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/easy_sync/rsync.rb', line 24

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

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

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