Class: Backup

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

Direct Known Subclasses

DbBackup, EtcBackup

Constant Summary collapse

DATE_FORMAT =
'%Y-%m-%d-%H-%M'

Instance Method Summary collapse

Constructor Details

#initialize(compress: true, encrypt: nil) ⇒ Backup

Returns a new instance of Backup.



8
9
10
11
12
13
# File 'lib/backup/backup.rb', line 8

def initialize compress: true, encrypt: nil
  @compress = compress
  @encrypt = encrypt.nil? ? !ENV['GPGKEY'].nil? : encrypt
  raise "GPGKEY environment variable should be specified for encryption" if encrypt? && ENV['GPGKEY'].nil?
  ensure_path
end

Instance Method Details

#backup_commandObject

Main pipe, which generates data for backup



48
49
50
# File 'lib/backup/backup.rb', line 48

def backup_command
  "echo Just text to backup"
end

#backup_typeObject



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

def backup_type
  self.class.to_s.gsub('Backup', '').downcase
end

#clean_files(prefix = 'local', dry_run = true) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/backup/backup.rb', line 104

def clean_files(prefix = 'local', dry_run = true)
  puts(dry_run ? 'Performing dry run' : '!!!! Cleaning backups !!!!')
  result = {} # true - delete file, false - do not delete
  months_taken = []
  days_taken = []
  # Process each backup type
  Dir["#{Techinform::BACKUPS_PREFIX}/#{prefix}/*"].each do |type|
    puts "Type: #{type}"
    # Get all backup name
    Dir["#{type}/*"].each do |name|
      puts "Name: #{name}"
      # Get all files
      Dir["#{name}/*"].sort.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)     # 2 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
        end
      end
      result.each do |file, delete|
        puts "#{file} #{"*" if delete}"
      end
      unless dry_run
        return unless HighLine.new.agree "Delete #{result.select{|file, delete| delete}.keys.size} files - Are you sure? (yes/no)"
        # Actually delete files
        result.select{|file, delete| delete}.keys.each do |file|
          puts `rm #{file}`
        end
      end
    end
  end

end

#compress?Boolean

Returns:

  • (Boolean)


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

def compress?
  @compress
end

#debug?Boolean

Returns:

  • (Boolean)


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

def debug?
  ENV['DEBUG'] == 'true'
end

#encrypt?Boolean

Returns:

  • (Boolean)


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

def encrypt?
  @encrypt
end

#encrypt_compress_pipe_commandObject



52
53
54
55
56
57
58
# File 'lib/backup/backup.rb', line 52

def encrypt_compress_pipe_command
  if encrypt?
    "| gpg2 --encrypt #{"--compress-algo=bzip2" if compress?} --recipient=#{ENV['GPGKEY']}"
  else
    "| bzip2" if compress?
  end
end

#ensure_pathObject



72
73
74
# File 'lib/backup/backup.rb', line 72

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

#filenameObject



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

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

#filename_extension(base = 'tar') ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/backup/backup.rb', line 35

def filename_extension(base = 'tar')
  if encrypt?
    "#{base}.gpg"
  else
    if compress?
      "#{base}.bz2"
    else
      base
    end
  end
end

#filepathObject



68
69
70
# File 'lib/backup/backup.rb', line 68

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

#get_datetime_from_filename(filename) ⇒ Object



76
77
78
79
# File 'lib/backup/backup.rb', line 76

def get_datetime_from_filename filename
  puts 'Filename: ' + filename.split('.').first.split('-')[2..6].join('-')
  DateTime.strptime(filename.split('.').first.split('-')[2..6].join('-'), DATE_FORMAT)
end

#minimum_backup_sizeObject

Change it in child class



94
95
96
# File 'lib/backup/backup.rb', line 94

def minimum_backup_size
  50
end

#output_commandObject



60
61
62
# File 'lib/backup/backup.rb', line 60

def output_command
  "> #{filepath}"
end

#pathObject



64
65
66
# File 'lib/backup/backup.rb', line 64

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


81
82
83
# File 'lib/backup/backup.rb', line 81

def print_info
  puts "Run backup to #{filepath}..."
end

#runObject



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

def run
  print_info
  command = "#{backup_command} #{encrypt_compress_pipe_command} #{output_command}"
  puts "Command: #{command}" if debug?
  output = `#{command}`
  puts output unless output.empty?
end

#verify_backupObject

It worth to redefine that method to better check backup integrity Return true if everything ok



100
101
102
# File 'lib/backup/backup.rb', line 100

def verify_backup
  File.size(filepath) > minimum_backup_size
end