Class: Backup
- Inherits:
-
Object
- Object
- Backup
- Defined in:
- lib/backup/backup.rb
Direct Known Subclasses
Constant Summary collapse
- DATE_FORMAT =
'%Y-%m-%d-%H-%M'
Instance Method Summary collapse
-
#backup_command ⇒ Object
Main pipe, which generates data for backup.
- #backup_type ⇒ Object
- #backup_type_filename ⇒ Object
- #clean_files(prefix = 'local', dry_run = true) ⇒ Object
- #compress? ⇒ Boolean
- #debug? ⇒ Boolean
- #encrypt? ⇒ Boolean
- #encrypt_compress_pipe_command ⇒ Object
- #ensure_path ⇒ Object
- #filename ⇒ Object
- #filename_extension(base = 'tar') ⇒ Object
- #filepath ⇒ Object
- #get_datetime_from_filename(filename) ⇒ Object
-
#initialize(compress: true, encrypt: nil) ⇒ Backup
constructor
A new instance of Backup.
-
#minimum_backup_size ⇒ Object
Change it in child class.
- #output_command ⇒ Object
- #path ⇒ Object
- #print_info ⇒ Object
- #run ⇒ Object
-
#verify_backup ⇒ Object
It worth to redefine that method to better check backup integrity Return true if everything ok.
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_command ⇒ Object
Main pipe, which generates data for backup
52 53 54 |
# File 'lib/backup/backup.rb', line 52 def backup_command "echo Just text to backup" end |
#backup_type ⇒ Object
27 28 29 |
# File 'lib/backup/backup.rb', line 27 def backup_type self.class.to_s.gsub('Backup', '').downcase end |
#backup_type_filename ⇒ Object
31 32 33 |
# File 'lib/backup/backup.rb', line 31 def backup_type_filename backup_type end |
#clean_files(prefix = 'local', dry_run = true) ⇒ Object
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 150 151 152 153 |
# File 'lib/backup/backup.rb', line 108 def clean_files(prefix = 'local', dry_run = true) puts(dry_run ? 'Performing dry run' : '!!!! Cleaning backups !!!!') 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 result = {} # true - delete file, false - do not delete 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
15 16 17 |
# File 'lib/backup/backup.rb', line 15 def compress? @compress end |
#debug? ⇒ Boolean
23 24 25 |
# File 'lib/backup/backup.rb', line 23 def debug? ENV['DEBUG'] == 'true' end |
#encrypt? ⇒ Boolean
19 20 21 |
# File 'lib/backup/backup.rb', line 19 def encrypt? @encrypt end |
#encrypt_compress_pipe_command ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/backup/backup.rb', line 56 def encrypt_compress_pipe_command if encrypt? "| gpg2 --encrypt #{"--compress-algo=bzip2" if compress?} --recipient=#{ENV['GPGKEY']}" else "| bzip2" if compress? end end |
#ensure_path ⇒ Object
76 77 78 |
# File 'lib/backup/backup.rb', line 76 def ensure_path `mkdir -p #{path}` end |
#filename ⇒ Object
35 36 37 |
# File 'lib/backup/backup.rb', line 35 def filename "#{backup_type_filename}-#{DateTime.now.strftime(DATE_FORMAT)}.#{filename_extension}" end |
#filename_extension(base = 'tar') ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/backup/backup.rb', line 39 def filename_extension(base = 'tar') if encrypt? "#{base}.gpg" else if compress? "#{base}.bz2" else base end end end |
#filepath ⇒ Object
72 73 74 |
# File 'lib/backup/backup.rb', line 72 def filepath "#{path}/#{filename}" end |
#get_datetime_from_filename(filename) ⇒ Object
80 81 82 83 |
# File 'lib/backup/backup.rb', line 80 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_size ⇒ Object
Change it in child class
98 99 100 |
# File 'lib/backup/backup.rb', line 98 def minimum_backup_size 50 end |
#output_command ⇒ Object
64 65 66 |
# File 'lib/backup/backup.rb', line 64 def output_command "> #{filepath}" end |
#path ⇒ Object
68 69 70 |
# File 'lib/backup/backup.rb', line 68 def path "#{Techinform::BACKUPS_LOCAL_PREFIX}/#{backup_type}" end |
#print_info ⇒ Object
85 86 87 |
# File 'lib/backup/backup.rb', line 85 def print_info puts "Run backup to #{filepath}..." end |
#run ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/backup/backup.rb', line 89 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_backup ⇒ Object
It worth to redefine that method to better check backup integrity Return true if everything ok
104 105 106 |
# File 'lib/backup/backup.rb', line 104 def verify_backup File.size(filepath) > minimum_backup_size end |