Class: BackupUtility::File

Inherits:
Base
  • Object
show all
Defined in:
lib/backup_utility/file.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#backup, #cleanup_backups, #cleanup_dir, #cleanup_shared, #custom?, #log, #log_and_time, #send_dir_to_s3, #send_file_to_s3, #shared_dst, #store_backup

Constructor Details

#initialize(backup_info) ⇒ File

Returns a new instance of File.



54
55
56
# File 'lib/backup_utility/file.rb', line 54

def initialize(backup_info)
  super(backup_info)
end

Class Method Details

.build_settings(type) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/backup_utility/file.rb', line 26

def self.build_settings(type)
  age = (ENV['age'] || 5).to_i
  if type == :custom
    src_dir = ENV['custom_dir']
    label = ENV['custom_label']
    if src_dir.nil? || label.nil?
      puts "missing custom dir or label"
      return nil
    end
    type = label.to_sym
  else
    type = type.to_sym
    map = {:logs => '/var/log/nginx'
    }
    if map.has_key?(type)
      src_dir = map[type]
    else
      # check if the type exists on disk
      src_dir = "/home/twist/stats_prod/shared/log/#{type}_dumps"
    end      
    if src_dir.blank? || !File.exists?(src_dir)
      puts "could not find directory to backup for #{type} (src dir : #{src_dir})"
      return nil
    end
  end
  {:type => type, :src_dir => src_dir, :age => age.days.ago, :date_format => '%Y-%m-%d'}
end

.init(type) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/backup_utility/file.rb', line 3

def self.init(type)
  save = ENV['save'] != 'false'
  use_code = false
  # don't send to shared, since we're going do it on "cleanup"
  if ::Rails.env == 'production'
    send_to_s3 = true
  else
    send_to_s3 = false
  end
  use_code = send_to_s3
  settings = build_settings(type)
  return [nil, nil] if settings.nil?
  db_info = {:save => save, 
    :send_to_s3 => send_to_s3,
    :send_to_shared => false,
    :use_code => use_code,
    :shared_dir => "/mnt/shared_storage2/backups/#{type}",
    :backup_dir => "/home/twist/backups/#{type}",
    :clean_shared_age => ENV['clean_shared_age']
  }
  [BackupUtility::File.new(db_info), settings]
end

Instance Method Details

#find_dir(settings) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/backup_utility/file.rb', line 70

def find_dir(settings)
  src_dir = settings[:src_dir]
  return nil if src_dir.blank? || !File.exists?(src_dir)
  age = settings[:age]
  to_backup = nil
  Dir.entries(src_dir).each do |name|
    next if name[0] == ?.
    dir = File.join(src_dir, name)
    next if !File.directory?(dir)
    mtime = File.mtime(dir)
    if mtime < age
      to_backup = dir
      puts "found #{dir} to backup"
      break
    end
  end
  to_backup
end

#get_paths(settings = {}, append = nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/backup_utility/file.rb', line 58

def get_paths(settings = {}, append = nil)
  to_backup = find_dir(settings)
  return [nil, nil, nil] if to_backup.nil?
  date = File.basename(to_backup)
  type = settings[:type]
  base_name = "stats_#{type}_#{date}.tar.gz"
  backup_file = File.join(@backup_dir, base_name)
  working_dir = File.join(@backup_dir, date)
  settings[:to_backup] = to_backup
  [nil, working_dir, backup_file]
end

#perform_dump(settings, working_dir) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/backup_utility/file.rb', line 89

def perform_dump(settings, working_dir)
  return false if working_dir.blank?
  # move everything in the to_backup dir to the working_dir, and then remove it
  to_backup = settings[:to_backup]
  return false if to_backup.blank?
  `mv #{to_backup} #{@backup_dir}`
  return false if File.exists?(to_backup)
  true
end