Class: GatgetDarBackup

Inherits:
Gat::Base show all
Defined in:
lib/gatgets/dar_backup/dar_backup.rb

Direct Known Subclasses

LauncherForGatgetDarBackup

Instance Attribute Summary

Attributes inherited from Gat::Base

#arguments, #config, #debug, #flags, #logger, #name, #onfail_error, #operation, #path, #times, #variables, #verbose

Instance Method Summary collapse

Methods inherited from Gat::Base

#execute, #get_dependence_value, #get_dependence_variable, #get_element_config, #get_flag, #initialize, #set_dependence_variable, #set_flag

Methods included from Gat::Email

#create_and_deliver_email, #new_email, #send_email

Methods included from Gat::Launcher

included

Methods included from Gat::Help

#gatget_help, #returning

Methods included from Gat::Debug

#get_debug_levels, #print_debug_msg

Methods included from Gat::Checks

#check_enough_space, #check_false, #check_file_exists, #check_file_not_exists, #check_i_am_running, #check_true, #exit_if_i_am_running, #need_arguments

Constructor Details

This class inherits a constructor from Gat::Base

Instance Method Details

#check_enough_space_for_backupObject

We need to have enough space to do backup



109
110
111
112
113
114
115
116
117
118
# File 'lib/gatgets/dar_backup/dar_backup.rb', line 109

def check_enough_space_for_backup

  last_backup_size = get_dependence_variable("last_backup_size")
  backup_folder    = get_dependence_value("folders", "backups_folder_data_local")
  thresold         = get_dependence_value("static", "space_thresold").to_i * 1024 * 1024 # to Bytes
  # check it
  if not check_enough_space({'where' => backup_folder, 'space_needed' => last_backup_size}) and not check_enough_space({'where' => backup_folder, 'space_needed' => thresold})
    raise Gat::GatgetProcessException.new("System doesnt have enough space", "check_enough_space_for_backup")
  end
end

#exists_backup_fileObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/gatgets/dar_backup/dar_backup.rb', line 83

def exists_backup_file
  get_system_backups
  backup_folder   = get_dependence_value("folders", "backups_folder_data_local")
  backup_dar_name = get_dependence_value("arguments", "backup_dar_name")
  backups_with_dar_name = get_dependence_variable("system_backups").select { |b| b['dar_name'] == backup_dar_name }
  if backups_with_dar_name.empty?
    self.logger.log("direct", "action_result", "\n ERROR: Backup dar name #{ backup_dar_name } not found\n\n Type $> #{ $0 } list_backups to see avalaibles backups\n\n")
    set_flag("exists_backup_file", false)
  else
    backup = backups_with_dar_name.first
    set_dependence_variable("backup_to_show_content", "#{ backup['folder'] }/#{ backup['dar_name']}")
    set_flag("exists_backup_file", true)
  end

end

#get_all_backupsObject

get all the system backups



34
35
36
37
# File 'lib/gatgets/dar_backup/dar_backup.rb', line 34

def get_all_backups
  backups_path = get_dependence_value("folders", "backups_folder_data_local")
  set_dependence_variable("system_backups", SystemBackup.find(:all, backups_path))
end

#launch_backup_rescueObject

failed action launch_backup_rescue callback Dar, when the backup is so big, usually end with a 2 status, that means some files changed during backup operations. That is normal while is working over logs, mysql, etc… So, we have to discriminate what the non zero exit status means.



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/gatgets/dar_backup/dar_backup.rb', line 167

def launch_backup_rescue
  # get the backup action
  launch_backup = Gat::Action::Base.get_current_action

  # Exit level 4 but with callback to delete backup
  if launch_backup.exit_level == 4 and launch_backup.output.include?('No space left on device, you have the opportunity to make room now')
    # disk is full, mark bad backup flag that will delete current backup
    self.logger.log('trace', 'onfail_launch_backup_rescue', 'Disk is full, delete current backup')

    delete_action = Gat::Action::ShellCommand.new('delete_bad_backup', get_element_config('actions', 'delete_bad_backup'), self.operation)
    delete_action.execute

    if delete_action.exit_level == 0
      launch_backup.onfail_message = "Dar backup collapse disk, but backup was deleted"
    else
      launch_backup.onfail_message = "Dar backup collapse disk and backup WASNT DELETED."
    end

    # mark action to abort
    launch_backup.onfail = 'abort'

  # 4 exit level always means problems
  elsif launch_backup.exit_level == 4 or launch_backup.exit_level == 2
    # mark action to abort
    launch_backup.onfail = 'abort'
  end
end

#list_backups_dataObject

list_backups_data is empty. SystemBackup.find(:all) is launched at required helper get_all_backups



105
106
# File 'lib/gatgets/dar_backup/dar_backup.rb', line 105

def list_backups_data
end

#onfailObject

send rescue email will send a email for notification errors at do_backup operation



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/gatgets/dar_backup/dar_backup.rb', line 196

def onfail

  unless self.config['email_config']
    raise Gat::GatgetConfigException.new("Default email config is empty at gatget.config['email_config']", "dar_backup:onfail")
  end

  email_subject = "[GAT] [#{ self.class.name }] [#{ self.operation.name.camelize }] [FAILED]"


  email_body    =  "Gatget #{ self.class.name } was stopped by raising of #{ self.onfail_error.class } at #{ Time.now }\n\n"
  email_body    << "#{ onfail_error.exit_error }"
  email_body    << "\n\n Errors log:  \n\n #{self.logger.entries.select{ |e| e.global_type == 'error' }.map{ |e| e.inspect }.join("\n") }\n\n"
  email_body    << "\n\n Warning log: \n\n #{self.logger.entries.select{ |e| e.global_type == 'warning' }.map{ |e| e.inspect }.join("\n") }\n\n"
  email_body    << "\n\n Whole log:   \n\n #{self.logger.entries.map{ |e| e.inspect }.join("\n") }"

  self.create_and_deliver_email(self.config['email_config']['send_to'], { 'subject' => email_subject, 'body' => email_body }, self.config['email_config'])

end


215
216
217
218
219
220
221
222
# File 'lib/gatgets/dar_backup/dar_backup.rb', line 215

def print_status
  if check_i_am_running
    ret = "running"
  else
    ret = "not running"
  end
  Gat::Action::Base.get_current_action.output = ret
end

#search_in_backupsObject



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
154
155
156
157
158
159
160
# File 'lib/gatgets/dar_backup/dar_backup.rb', line 120

def search_in_backups

  backup_folder = get_dependence_value("folders", "backups_folder_data_local")
  all_backups   = get_dependence_variable("system_backups")
  search_what   = get_dependence_value("arguments", "search_pattern")

  something_found = false

  all_backups.each do |backup|

    # do we have already the dumped content??
    unless backup.dump
      self.logger.log("warning", "process_action", "Backup #{ backup.name } doesnt have a valid dump content. Dumping content")
      backup_path = "#{ backup_folder }/#{ backup.folder }/#{ backup.name }"
      set_dependence_variable("backup_path", backup_path)

      dumping_action = Gat::Action::ShellCommand.new('dump_backup_content_to_file', get_element_config('actions', 'dump_backup_content_to_file'), self.operation)

      begin
        dumping_action.execute
      rescue => error
        raise Gat::GatgetProcessException.new("Unable to dump content for backup #{ backup_path}", "search_in_backups")
      end
    end

    # now, search in backup the patern
    results_for_backup = backup.search_pattern(search_what)

    if results_for_backup.blank?
      self.logger.log("trace", "process_action", "Backup #{ backup.name } doesnt contain pattern #{ search_what }")
    else
      something_found = true
      self.logger.log("direct", "process_action", "\n\nBackup #{ backup.name } contain pattern '#{ search_what }'\n")
      self.logger.log("direct", "process_action", "#{ results_for_backup.gsub(/^\[/, " » [") }")
    end
  end

  unless something_found
    self.logger.log("direct", "process_action", "\n Sorry. Pattern #{ search_what } not found in stored backups.\n Try to simplify search, for example, seach 'bin' instead of 'bin/ls'\n\n")
  end
end

#set_do_backup_variablesObject

We are going to set the backups variables. Which ones must be deleted and the name of the current backup



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/gatgets/dar_backup/dar_backup.rb', line 40

def set_do_backup_variables

  backups_path        = get_dependence_value("folders", "backups_folder_data_local")
  last_complet_backup = SystemBackup.find(:last, backups_path, { :current => true, :type => 'complet'})
  if not last_complet_backup

    # * perform a complet backup
    # * set the minimum space required at 1 kb
    # * mark backups to delete, if nothing to delete, system will get it
    # * create new dir and link it to current through new_link_current flag

    set_variables_to_complete_backup
    set_dependence_variable("last_backup_size", 1)
    set_backups_to_delete
    set_flag("delete_old_backups", true)
    set_flag("new_complete_folder", true)

  # if we have a complet backup but today is complet
  elsif get_dependence_value("static", "complet_backup_days").include?(Date.today.wday)

    # * perform a complet backup
    # * get the last size of complet backup
    # * identify backups to delete and mark delete flag
    # * mark delete current link
    # * create new dir and link it to current through new_link_current flag

    set_variables_to_complete_backup
    set_dependence_variable("last_backup_size", get_last_backup_size('complet'))
    set_backups_to_delete
    set_flag("delete_old_backups", true)
    set_flag("delete_current_link", true)
    set_flag("new_complete_folder", true)

  # else, we must do a diff backup, do
  else
    # * perform diff backup
    # * dont care about delete or new dirs... etc..
    # * get the last size of diff backup
    set_variables_to_diff_backup
    set_dependence_variable("last_backup_size", get_last_backup_size('diff'))
  end
end

#set_flag_mysql_dumpsObject

Gatget Helpers ##



29
30
31
# File 'lib/gatgets/dar_backup/dar_backup.rb', line 29

def set_flag_mysql_dumps
  set_flag("active_mysql_dumps", get_dependence_value("static", "mysql_dumps"))
end