Class: LSync::Methods::RSync

Inherits:
LSync::Method show all
Defined in:
lib/lsync/methods/rsync.rb

Direct Known Subclasses

RSyncSnapshot

Instance Attribute Summary

Attributes inherited from LSync::Method

#logger

Instance Method Summary collapse

Methods included from EventHandler

#abort!, #fire, #on, #try

Constructor Details

#initialize(direction, options = {}) ⇒ RSync

Returns a new instance of RSync.



25
26
27
28
29
30
31
32
# File 'lib/lsync/methods/rsync.rb', line 25

def initialize(direction, options = {})
  super(options)
  @direction = direction
  @command = options[:command] || "rsync"
  
  @options = options
  @connection = nil
end

Instance Method Details

#run(master_server, target_server, directory) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/lsync/methods/rsync.rb', line 34

def run(master_server, target_server, directory)
  arguments = (@options[:arguments] || ["--archive"]) + (directory.options[:arguments] || [])

  local_server = nil
  remote_server = nil

  if @direction == :push
    local_server = master_server
    remote_server = target_server

    dst = remote_server.connection_string(directory)
    src = local_server.full_path(directory)
  else
    local_server = target_server
    remote_server = master_server

    src = remote_server.connection_string(directory)
    dst = local_server.full_path(directory)
  end

  arguments += connect_arguments(local_server, remote_server)

  # Create the destination backup directory
  @connection = target_server.connect
  @connection.send_object([:mkdir_p, target_server.full_path(directory)])

  @logger.info "In directory #{Dir.getwd}..."
  Dir.chdir(local_server.root) do
    if run_handler(src, dst, arguments) == false
      raise BackupMethodError.new("Backup from #{src.dump} to #{dst.dump} failed.", :method => self)
    end
  end
end

#run_command(cmd) ⇒ Object



82
83
84
# File 'lib/lsync/methods/rsync.rb', line 82

def run_command(cmd)
  return LSync.run_command(cmd, @logger) == 0
end

#run_handler(src, dst, arguments) ⇒ Object



68
69
70
# File 'lib/lsync/methods/rsync.rb', line 68

def run_handler(src, dst, arguments)
  run_command([@command] + arguments + [src, dst])
end

#should_run?(master_server, current_server, target_server) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
76
77
78
79
80
# File 'lib/lsync/methods/rsync.rb', line 72

def should_run?(master_server, current_server, target_server)
  if @direction == :push
    return current_server == master_server
  elsif @direction == :pull
    return target_server.is_local?
  else
    return false
  end
end