Class: Backup
- Inherits:
-
Object
- Object
- Backup
- Defined in:
- lib/backup/backup.rb
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
- #clean_files ⇒ 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.
- #output_command ⇒ Object
- #path ⇒ Object
- #print_info ⇒ Object
- #run ⇒ Object
Constructor Details
#initialize(compress: true, encrypt: nil) ⇒ Backup
Returns a new instance of Backup.
7 8 9 10 11 12 |
# File 'lib/backup/backup.rb', line 7 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
47 48 49 |
# File 'lib/backup/backup.rb', line 47 def backup_command "echo Just text to backup" end |
#backup_type ⇒ Object
26 27 28 |
# File 'lib/backup/backup.rb', line 26 def backup_type self.class.to_s.gsub('Backup', '').downcase end |
#clean_files ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/backup/backup.rb', line 92 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) # 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 puts "Candidates to removal: #{result.select{|file, delete| delete }.keys.inspect}" end |
#compress? ⇒ Boolean
14 15 16 |
# File 'lib/backup/backup.rb', line 14 def compress? @compress end |
#debug? ⇒ Boolean
22 23 24 |
# File 'lib/backup/backup.rb', line 22 def debug? ENV['DEBUG'] == 'true' end |
#encrypt? ⇒ Boolean
18 19 20 |
# File 'lib/backup/backup.rb', line 18 def encrypt? @encrypt end |
#encrypt_compress_pipe_command ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/backup/backup.rb', line 51 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
71 72 73 |
# File 'lib/backup/backup.rb', line 71 def ensure_path `mkdir -p #{path}` end |
#filename ⇒ Object
30 31 32 |
# File 'lib/backup/backup.rb', line 30 def filename "#{backup_type}-#{DateTime.now.strftime(DATE_FORMAT)}.#{filename_extension}" end |
#filename_extension(base = 'tar') ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/backup/backup.rb', line 34 def filename_extension(base = 'tar') if encrypt? "#{base}.gpg" else if compress? "#{base}.bz2" else base end end end |
#filepath ⇒ Object
67 68 69 |
# File 'lib/backup/backup.rb', line 67 def filepath "#{path}/#{filename}" end |
#get_datetime_from_filename(filename) ⇒ Object
75 76 77 |
# File 'lib/backup/backup.rb', line 75 def get_datetime_from_filename filename DateTime.strptime(filename.split('-')[2], DATE_FORMAT) end |
#output_command ⇒ Object
59 60 61 |
# File 'lib/backup/backup.rb', line 59 def output_command "> #{filepath}" end |
#path ⇒ Object
63 64 65 |
# File 'lib/backup/backup.rb', line 63 def path "#{Techinform::BACKUPS_LOCAL_PREFIX}/#{backup_type}" end |
#print_info ⇒ Object
79 80 81 |
# File 'lib/backup/backup.rb', line 79 def print_info puts "Run backup to #{filepath}..." end |
#run ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/backup/backup.rb', line 83 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 |