Class: Leipreachan::DBBackup
- Inherits:
-
Object
- Object
- Leipreachan::DBBackup
- Defined in:
- lib/leipreachan.rb
Direct Known Subclasses
Constant Summary collapse
- MAX_DAYS =
30
- DIRECTORY =
'backups'
Instance Attribute Summary collapse
-
#backup_file ⇒ Object
Returns the value of attribute backup_file.
-
#backup_folder ⇒ Object
Returns the value of attribute backup_folder.
-
#base_path ⇒ Object
Returns the value of attribute base_path.
-
#db_config ⇒ Object
Returns the value of attribute db_config.
-
#directory ⇒ Object
Returns the value of attribute directory.
-
#file_for_restore ⇒ Object
Returns the value of attribute file_for_restore.
-
#max_days ⇒ Object
Returns the value of attribute max_days.
-
#target_date ⇒ Object
Returns the value of attribute target_date.
Instance Method Summary collapse
- #backup! ⇒ Object
- #check_system_requirements! ⇒ Object
-
#initialize(env) ⇒ DBBackup
constructor
A new instance of DBBackup.
- #list ⇒ Object
- #restore! ⇒ Object
- #restorefile! ⇒ Object
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_file ⇒ Object
Returns the value of attribute backup_file.
17 18 19 |
# File 'lib/leipreachan.rb', line 17 def backup_file @backup_file end |
#backup_folder ⇒ Object
Returns the value of attribute backup_folder.
17 18 19 |
# File 'lib/leipreachan.rb', line 17 def backup_folder @backup_folder end |
#base_path ⇒ Object
Returns the value of attribute base_path.
17 18 19 |
# File 'lib/leipreachan.rb', line 17 def base_path @base_path end |
#db_config ⇒ Object
Returns the value of attribute db_config.
17 18 19 |
# File 'lib/leipreachan.rb', line 17 def db_config @db_config end |
#directory ⇒ Object
Returns the value of attribute directory.
17 18 19 |
# File 'lib/leipreachan.rb', line 17 def directory @directory end |
#file_for_restore ⇒ Object
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_days ⇒ Object
Returns the value of attribute max_days.
17 18 19 |
# File 'lib/leipreachan.rb', line 17 def max_days @max_days end |
#target_date ⇒ Object
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 |
#list ⇒ Object
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 |