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.



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

def initialize env
  @max_days = (env['DAYS'] || MAX_DAYS).to_i
  @target_date = env['DATE']
  @backup_folder = env['DATE'] || 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_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

#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



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

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

#listObject



57
58
59
# File 'lib/leipreachan.rb', line 57

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

#restore!Object



49
50
51
# File 'lib/leipreachan.rb', line 49

def restore!
  dbrestore! get_file_for_restore
end

#restorelast!Object



53
54
55
# File 'lib/leipreachan.rb', line 53

def restorelast!
  dbrestore! get_lastfile_for_restore
end