Class: Leipreachan::DBBackup

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

Direct Known Subclasses

Backuper

Constant Summary collapse

MAX_DAYS =
30
DIRECTORY =
'backups'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ DBBackup

Returns a new instance of DBBackup.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/leipreachan.rb', line 26

def initialize env
  check_system_requirements!
  @max_days = (env['DAYS'] || MAX_DAYS).to_i
  @target_date = env['DATE']
  @backup_folder = env['DATE'].presence || Time.now.strftime("%Y%m%d")
  @directory = env['DIR'] || DIRECTORY
  datetime_stamp = Time.now.strftime("%Y%m%d%H%M%S")
  @base_path = File.join(Rails.root, directory)
  @file_for_restore = env['FILE']

  file_name = "#{datetime_stamp}.sql"
  @backup_file = File.join(backup_base_on(backup_folder), file_name)

  @db_config = ActiveRecord::Base.configurations[Rails.env]
end

Instance Attribute Details

#backup_fileObject

Returns the value of attribute backup_file.



17
18
19
# File 'lib/leipreachan.rb', line 17

def backup_file
  @backup_file
end

#backup_folderObject

Returns the value of attribute backup_folder.



17
18
19
# File 'lib/leipreachan.rb', line 17

def backup_folder
  @backup_folder
end

#base_pathObject

Returns the value of attribute base_path.



17
18
19
# File 'lib/leipreachan.rb', line 17

def base_path
  @base_path
end

#db_configObject

Returns the value of attribute db_config.



17
18
19
# File 'lib/leipreachan.rb', line 17

def db_config
  @db_config
end

#directoryObject

Returns the value of attribute directory.



17
18
19
# File 'lib/leipreachan.rb', line 17

def directory
  @directory
end

#file_for_restoreObject

Returns the value of attribute file_for_restore.



17
18
19
# File 'lib/leipreachan.rb', line 17

def file_for_restore
  @file_for_restore
end

#max_daysObject

Returns the value of attribute max_days.



17
18
19
# File 'lib/leipreachan.rb', line 17

def max_days
  @max_days
end

#target_dateObject

Returns the value of attribute target_date.



17
18
19
# File 'lib/leipreachan.rb', line 17

def target_date
  @target_date
end

Instance Method Details

#backup!Object



49
50
51
52
53
54
55
56
57
# File 'lib/leipreachan.rb', line 49

def backup!
  FileUtils.mkdir_p(backup_base_on(backup_folder))
  dbbackup!
  puts "Created backup: #{backup_file}.gz"
  backups_count = Dir.new(backup_base_on(backup_folder)).entries.select{|name| name.match(/sql.gz$/)}.size
  puts "#{backups_count} backups available for #{backup_folder} date"

  remove_unwanted_backups
end

#check_system_requirements!Object



42
43
44
45
46
47
# File 'lib/leipreachan.rb', line 42

def check_system_requirements!
  system_check_list.each do |command|
    system("#{command} --help > /dev/null 2>&1")
    raise "#{command} is required for Leipreachan backups" if $?.exitstatus > 0
  end
end

#listObject



67
68
69
# File 'lib/leipreachan.rb', line 67

def list
  target_date.present? ? single_date(target_date) : all_dates
end

#restore!Object



59
60
61
# File 'lib/leipreachan.rb', line 59

def restore!
  dbrestore! File.join(backup_base_on(backup_folder), get_file_for_restore)
end

#restorefile!Object



63
64
65
# File 'lib/leipreachan.rb', line 63

def restorefile!
  dbrestore! File.join(backup_base_on(backup_folder), file_for_restore || get_lastfile_for_restore)
end