Class: Backup::Syncer::RSync

Inherits:
Base
  • Object
show all
Defined in:
lib/backup/syncer/rsync.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Configuration::Helpers

#clear_defaults!, #getter_methods, #load_defaults!, #setter_methods

Methods included from CLI

#mkdir, #rm, #run, #utility

Constructor Details

#initialize(&block) ⇒ RSync

Instantiates a new RSync Syncer object and sets the default configuration specified in the Backup::Configuration::Syncer::RSync. Then it sets the object defaults if particular properties weren’t set. Finally it’ll evaluate the users configuration file and overwrite anything that’s been defined



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/backup/syncer/rsync.rb', line 44

def initialize(&block)
  load_defaults!

  @directories          = Array.new
  @additional_options ||= Array.new
  @path               ||= 'backups'
  @port               ||= 22
  @mirror             ||= false
  @compress           ||= false

  instance_eval(&block) if block_given?

  @path = path.sub(/^\~\//, '')
end

Instance Attribute Details

#additional_optionsObject

Additional options for the rsync cli



37
38
39
# File 'lib/backup/syncer/rsync.rb', line 37

def additional_options
  @additional_options
end

#compressObject

Returns Rsync syntax for compressing the file transfers



81
82
83
# File 'lib/backup/syncer/rsync.rb', line 81

def compress
  '--compress' if @compress
end

#directories(&block) ⇒ Object

If no block has been provided, it’ll return the array of @directories. If a block has been provided, it’ll evaluate it and add the defined paths to the @directories



100
101
102
103
104
105
106
107
# File 'lib/backup/syncer/rsync.rb', line 100

def directories(&block)
  unless block_given?
    return @directories.map do |directory|
      "'#{directory}'"
    end.join("\s")
  end
  instance_eval(&block)
end

#ipObject

Server IP Address and SSH port



13
14
15
# File 'lib/backup/syncer/rsync.rb', line 13

def ip
  @ip
end

#mirrorObject

Returns Rsync syntax for enabling mirroring



75
76
77
# File 'lib/backup/syncer/rsync.rb', line 75

def mirror
  '--delete' if @mirror
end

#passwordObject

Server credentials



9
10
11
# File 'lib/backup/syncer/rsync.rb', line 9

def password
  @password
end

#pathObject

Path to store the synced files/directories to



25
26
27
# File 'lib/backup/syncer/rsync.rb', line 25

def path
  @path
end

#portObject

Returns Rsync syntax for defining a port to connect to



93
94
95
# File 'lib/backup/syncer/rsync.rb', line 93

def port
  "--port='#{@port}'"
end

#usernameObject

Server credentials



9
10
11
# File 'lib/backup/syncer/rsync.rb', line 9

def username
  @username
end

Instance Method Details

#add(path) ⇒ Object

Adds a path to the @directories array



111
112
113
# File 'lib/backup/syncer/rsync.rb', line 111

def add(path)
  @directories << path
end

#archiveObject

Returns Rsync syntax for invoking “archive” mode



87
88
89
# File 'lib/backup/syncer/rsync.rb', line 87

def archive
  '--archive'
end

#optionsObject

Returns all the specified Rsync options, concatenated, ready for the CLI



69
70
71
# File 'lib/backup/syncer/rsync.rb', line 69

def options
  ([archive, mirror, compress, port] + additional_options).compact.join("\s")
end

#perform!Object

Performs the RSync operation debug options: -vhP



62
63
64
65
# File 'lib/backup/syncer/rsync.rb', line 62

def perform!
  Logger.message("#{ self.class } started syncing #{ directories }.")
  Logger.silent( run("#{ utility(:rsync) } -vhP #{ options } #{ directories } '#{ username }@#{ ip }:#{ path }'") )
end