Class: Backsum::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/backsum/server.rb

Defined Under Namespace

Classes: Dsl

Instance Method Summary collapse

Instance Method Details

#connectObject



17
18
19
20
# File 'lib/backsum/server.rb', line 17

def connect
  return nil if local
  username ? "#{username}@#{host}" : host if host
end

#execute_rsync(backup_path, linkdest_path, connect, source, options) ⇒ Object



28
29
30
31
32
33
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
# File 'lib/backsum/server.rb', line 28

def execute_rsync(backup_path, linkdest_path, connect, source, options)
  arguments = [ "--archive", "--delete" ]
  target_path = File.join(backup_path, self.host)
  arguments << "--verbose" if Backsum.verbose

  if linkdest_path
    linkdest_abosulte_path = File.absolute_path(linkdest_path) # rsync link dest must be abosulte path.
    linkdest_dir = File.join([linkdest_abosulte_path, self.host, options[:as], "/"].compact)
    arguments << "--link-dest=#{linkdest_dir}" if File.exists?(linkdest_dir)
  end

  if options[:as]
    target_path = File.join(target_path, options[:as])
  else
    arguments << "--relative"
  end
  
  FileUtils.mkdir_p target_path
  
  (options[:excluded] || []).each do |pattern|
    arguments << "--exclude=#{pattern}"
  end
  
  source = File.join(source, "/") if File.directory?(source)
  if connect
    arguments << "#{connect}:#{source}"
  else
    arguments << source
  end
  
  arguments << target_path
  
  copy_command = Shell.new("rsync", arguments.map {|arg| shell_param_escape(arg) }.join(' '))
  copy_command.run
end

#shell_param_escape(str) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/backsum/server.rb', line 64

def shell_param_escape(str)
  if str.include? " "
    "'" + str.gsub("'", "\\'") + "'" 
  else
    str
  end
end

#sync(backup_path, linkdest_path) ⇒ Object



22
23
24
25
26
# File 'lib/backsum/server.rb', line 22

def sync(backup_path, linkdest_path)
  self.folders.each_pair do |folder, options|
    execute_rsync(backup_path, linkdest_path, connect, folder, options)
  end
end