Class: Berty::Server

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hostname, options = {}) ⇒ Server

Returns a new instance of Server.



6
7
8
9
# File 'lib/berty/server.rb', line 6

def initialize(hostname, options = {})
  @hostname = hostname
  @options = options
end

Instance Attribute Details

#hostnameObject (readonly)

Returns the value of attribute hostname.



4
5
6
# File 'lib/berty/server.rb', line 4

def hostname
  @hostname
end

Instance Method Details

#backup!Object

Execute a backup of this server (backing up all directories to the appropriate locations)



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/berty/server.rb', line 36

def backup!
  run "mkdir -p #{backup_path}"
  for dir in directories
    run "mkdir -p #{dir[:destination]}/0 #{dir[:destination]}/1 #{dir[:destination]}/2 #{dir[:destination]}/3 #{dir[:destination]}/4"
    run "rm -Rf #{dir[:destination]}/4/"
    run "mv #{dir[:destination]}/3/ #{dir[:destination]}/4/"
    run "mv #{dir[:destination]}/2/ #{dir[:destination]}/3/"
    run "mv #{dir[:destination]}/1/ #{dir[:destination]}/2/"
    Berty.logger.info "  archiving previous backup: '#{dir[:path]}' to '#{dir[:destination]}/1/'"
    run "cp -a #{dir[:destination]}/0/ #{dir[:destination]}/1/"
    Berty.logger.info "  starting backup of: '#{dir[:path]}' to '#{dir[:destination]}'"
    run "rsync -a #{username}@#{hostname}:#{dir[:path]}/ #{dir[:destination]}/0/"
    Berty.logger.info "  finished backup of: '#{dir[:path]}' to '#{dir[:destination]}'" 
  end
  backup_mysql!
end

#backup_mysql!Object

Execute mysql backups?



71
72
73
74
75
76
77
78
79
80
# File 'lib/berty/server.rb', line 71

def backup_mysql!
  return unless mysql_backup?
  run "mkdir -p #{mysql_backup_path}"
  Berty.logger.info "  starting mysql backup on: #{hostname}"
  ## create a dump on the remote system as '/tmp/bert-mysqldump'
  ssh "mysqldump --all-databases -u root -p#{mysql_password} > /tmp/berty-mysqldump"
  ## pull the file over to us
  scp "/tmp/berty-mysqldump", File.join(mysql_backup_path, "#{Time.now.strftime("%Y-%m-%d-%H-%M")}.sql")
  Berty.logger.info "  finishing mysql backup on: #{hostname}"
end

#backup_pathObject

Return the full path where backups for this server should be stored on the local file system.



26
27
28
# File 'lib/berty/server.rb', line 26

def backup_path
  File.join(Berty.config['root'], hostname)
end

#directoriesObject

 Return all directories which need to be backed up



12
13
14
15
16
17
18
# File 'lib/berty/server.rb', line 12

def directories
  (@options['directories'] || []).map do |dir|
    path, label = dir.split(':')
    label ||= path.split('/').last
    {:path=> path, :label => label, :destination => File.join(backup_path, label)}
  end
end

#mysql_backup?Boolean

Should this server backup a mysql database?

Returns:

  • (Boolean)


61
62
63
# File 'lib/berty/server.rb', line 61

def mysql_backup?
  @options['mysql_password'].is_a?(String)
end

#mysql_backup_pathObject

 Return the full path to where mysql backups are stored



31
32
33
# File 'lib/berty/server.rb', line 31

def mysql_backup_path
  File.join(backup_path, 'mysql')
end

#mysql_passwordObject

What password should be used for accessing the database?



66
67
68
# File 'lib/berty/server.rb', line 66

def mysql_password
  @options['mysql_password']
end

#setup!Object

Setup the connection to this server by sshing to it and doing nothing. This gives the option to accept any host keys.



55
56
57
58
# File 'lib/berty/server.rb', line 55

def setup!
  key = File.read("#{ENV['HOME']}/.ssh/id_rsa.pub")
  ssh "echo '#{key.chomp}' >> ~/.ssh/authorized_keys"
end

#usernameObject

Which username should be used when connecting to this server?



21
22
23
# File 'lib/berty/server.rb', line 21

def username
  @options['username']
end