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, #raise_if_command_not_found!, #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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/backup/syncer/rsync.rb', line 48

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?
  write_password_file!

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

Instance Attribute Details

#additional_optionsObject

Additional options for the rsync cli



41
42
43
# File 'lib/backup/syncer/rsync.rb', line 41

def additional_options
  @additional_options
end

#compressObject

Returns Rsync syntax for compressing the file transfers



90
91
92
# File 'lib/backup/syncer/rsync.rb', line 90

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



115
116
117
118
119
120
121
122
# File 'lib/backup/syncer/rsync.rb', line 115

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



17
18
19
# File 'lib/backup/syncer/rsync.rb', line 17

def ip
  @ip
end

#mirrorObject

Returns Rsync syntax for enabling mirroring



84
85
86
# File 'lib/backup/syncer/rsync.rb', line 84

def mirror
  '--delete' if @mirror
end

#passwordObject

Returns Rsync syntax for setting a password (via a file)



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

def password
  @password
end

#pathObject

Path to store the synced files/directories to



29
30
31
# File 'lib/backup/syncer/rsync.rb', line 29

def path
  @path
end

#portObject

Returns Rsync syntax for defining a port to connect to



102
103
104
# File 'lib/backup/syncer/rsync.rb', line 102

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

#usernameObject

Server credentials



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

def username
  @username
end

Instance Method Details

#add(path) ⇒ Object

Adds a path to the @directories array



126
127
128
# File 'lib/backup/syncer/rsync.rb', line 126

def add(path)
  @directories << path
end

#archiveObject

Returns Rsync syntax for invoking “archive” mode



96
97
98
# File 'lib/backup/syncer/rsync.rb', line 96

def archive
  '--archive'
end

#optionsObject

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



78
79
80
# File 'lib/backup/syncer/rsync.rb', line 78

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

#perform!Object

Performs the RSync operation debug options: -vhP



67
68
69
70
71
72
73
74
# File 'lib/backup/syncer/rsync.rb', line 67

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

  remove_password_file!
end