Class: Backup::Database::Redis

Inherits:
Base
  • Object
show all
Defined in:
lib/backup/database/redis.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#dump_path, #utility_path

Instance Method Summary collapse

Methods inherited from Base

#log!, #prepare!

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) ⇒ Redis

Creates a new instance of the Redis database object



31
32
33
34
35
36
37
38
# File 'lib/backup/database/redis.rb', line 31

def initialize(&block)
  load_defaults!

  @additional_options ||= Array.new

  instance_eval(&block)
  prepare!
end

Instance Attribute Details

#additional_optionsObject

Builds a Redis compatible string for the additional options specified by the user



27
28
29
# File 'lib/backup/database/redis.rb', line 27

def additional_options
  @additional_options
end

#hostObject

Connectivity options



23
24
25
# File 'lib/backup/database/redis.rb', line 23

def host
  @host
end

#invoke_saveObject

Determines whether Backup should invoke the SAVE command through the ‘redis-cli’ utility to persist the most recent data before copying over the dump file



19
20
21
# File 'lib/backup/database/redis.rb', line 19

def invoke_save
  @invoke_save
end

#nameObject

Name of and path to the database that needs to get dumped



9
10
11
# File 'lib/backup/database/redis.rb', line 9

def name
  @name
end

#passwordObject

Credentials for the specified database



13
14
15
# File 'lib/backup/database/redis.rb', line 13

def password
  @password
end

#pathObject

Name of and path to the database that needs to get dumped



9
10
11
# File 'lib/backup/database/redis.rb', line 9

def path
  @path
end

#portObject

Connectivity options



23
24
25
# File 'lib/backup/database/redis.rb', line 23

def port
  @port
end

#socketObject

Connectivity options



23
24
25
# File 'lib/backup/database/redis.rb', line 23

def socket
  @socket
end

Instance Method Details

#connectivity_optionsObject

Builds the Redis connectivity options syntax to connect the user to perform the database dumping process



50
51
52
53
54
# File 'lib/backup/database/redis.rb', line 50

def connectivity_options
  %w[host port socket].map do |option|
    next if send(option).nil?; "-#{option[0,1]} '#{send(option)}'"
  end.compact.join("\s")
end

#copy!Object

Performs the copy command to copy over the Redis dump file to the Backup archive



95
96
97
98
99
100
101
102
# File 'lib/backup/database/redis.rb', line 95

def copy!
  unless File.exist?(File.join(path, database))
    Logger.error "Redis database dump not found in '#{ File.join(path, database) }'"
    exit
  end

  run("#{ utility(:cp) } '#{ File.join(path, database) }' '#{ File.join(dump_path, database) }'")
end

#credential_optionsObject

Builds the Redis credentials syntax to authenticate the user to perform the database dumping process



43
44
45
# File 'lib/backup/database/redis.rb', line 43

def credential_options
  return "-a '#{password}'" if password; String.new
end

#databaseObject

Returns the Redis database file name



65
66
67
# File 'lib/backup/database/redis.rb', line 65

def database
  "#{ name }.rdb"
end

#invoke_save!Object

Tells Redis to persist the current state of the in-memory database to the persisted dump file



85
86
87
88
89
90
91
# File 'lib/backup/database/redis.rb', line 85

def invoke_save!
  response = run("#{ utility('redis-cli') } #{ credential_options } #{ connectivity_options } #{ additional_options } SAVE")
  unless response =~ /OK/
    Logger.error "Could not invoke the Redis SAVE command. The #{ database } file might not be contain the most recent data."
    Logger.error "Please check if the server is running, the credentials (if any) are correct, and the host/port/socket are correct."
  end
end

#perform!Object

Performs the Redis backup by using the ‘cp’ unix utility to copy the persisted Redis dump file to the Backup archive. Additionally, when ‘invoke_save’ is set to true, it’ll tell the Redis server to persist the current state to the dump file before copying the dump to get the most recent updates in to the backup



75
76
77
78
79
80
# File 'lib/backup/database/redis.rb', line 75

def perform!
  log!

  invoke_save! if invoke_save
  copy!
end