Class: DbBackup

Inherits:
Backup show all
Defined in:
lib/backup/db_backup.rb

Direct Known Subclasses

MysqlBackup, PostgreBackup

Constant Summary

Constants inherited from Backup

Backup::DATE_FORMAT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Backup

#backup_command, #backup_type, #compress?, #debug?, #encrypt?, #encrypt_compress_pipe_command, #filename_extension, #get_datetime_from_filename, #minimum_backup_size, #output_command, #print_info, #run, #verify_backup

Constructor Details

#initialize(user: nil, database: nil, password: nil, compress: true, host: nil, encrypt: nil) ⇒ DbBackup

Returns a new instance of DbBackup.



6
7
8
9
10
11
12
# File 'lib/backup/db_backup.rb', line 6

def initialize user: nil, database: nil, password: nil, compress: true, host: nil, encrypt: nil
  super compress: compress, encrypt: encrypt
  user = ENV['USER'] if user.nil?
  password = ENV['PASSWORD'] if password.nil?
  @password, @user, @database, @host = password, user, database, host
  ensure_path unless database.nil?
end

Instance Attribute Details

#databaseObject (readonly)

Returns the value of attribute database.



4
5
6
# File 'lib/backup/db_backup.rb', line 4

def database
  @database
end

#hostObject (readonly)

Returns the value of attribute host.



4
5
6
# File 'lib/backup/db_backup.rb', line 4

def host
  @host
end

#passwordObject (readonly)

Returns the value of attribute password.



4
5
6
# File 'lib/backup/db_backup.rb', line 4

def password
  @password
end

#userObject (readonly)

Returns the value of attribute user.



4
5
6
# File 'lib/backup/db_backup.rb', line 4

def user
  @user
end

Instance Method Details

#clean_filesObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/backup/db_backup.rb', line 34

def clean_files
  result = {} # true - delete file, false - do not delete
  months_taken = []
  days_taken = []
  files = Dir.entries(path) - ['.', '..']
  files.each do |file|
    datetime = get_datetime_from_filename(file)
    day_id = datetime.strftime('%Y-%m-%d')
    month_id = datetime.strftime('%Y-%m')
    if datetime < (DateTime.now << 2)     # 1 month ago
      if datetime.day == 1 && !(months_taken.include? month_id)
        result[file] = false
        months_taken << month_id
      else
        result[file] = true
      end
    elsif datetime < DateTime.now - 14 # 2 weeks ago
      if [1, 7, 14, 21].include?(datetime.day) && !(days_taken.include?(day_id))
        result[file] = false
        days_taken << day_id
      else
        result[file] = true
      end
    elsif days_taken.includ
    end
  end
end

#ensure_pathObject



30
31
32
# File 'lib/backup/db_backup.rb', line 30

def ensure_path
  `mkdir -p #{path}`
end

#filenameObject



14
15
16
# File 'lib/backup/db_backup.rb', line 14

def filename
  "#{backup_type}-#{database}-#{DateTime.now.strftime(DATE_FORMAT)}.#{filename_extension('sql')}"
end

#filepathObject



26
27
28
# File 'lib/backup/db_backup.rb', line 26

def filepath
  "#{path}/#{filename}"
end

#pathObject



18
19
20
# File 'lib/backup/db_backup.rb', line 18

def path
  "#{Techinform::BACKUPS_LOCAL_PREFIX}/#{backup_type}/#{database}"
end

#restore_pathObject



22
23
24
# File 'lib/backup/db_backup.rb', line 22

def restore_path
  "#{Techinform::BACKUPS_ALL_PREFIX}/backups/#{backup_type}/#{database}"
end