Class: Irgat::Backup

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

Overview

create the class, that extends the Module Irgat::Irgat class

Instance Attribute Summary

Attributes inherited from Irgat

#action, #commands_launched, #config, #config_module, #folders, #init_time, #log_process, #programs

Instance Method Summary collapse

Methods inherited from Irgat

#help, #load_config, #process

Methods included from Dependencies

#check_dependencies

Methods included from Execs

#execute_command, #exit_with_error

Methods included from Mail

#send_email

Methods included from Log

#log, #output_log, #report_to_email, #report_to_log

Methods included from Help

#show_help

Constructor Details

#initialize(values = {}) ⇒ Backup

initialize the Class… make default operations



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/modules/backup.rb', line 20

def initialize(values = {})
  @action = values[:action]
  # Create and irgat process
  irgat_process = Irgat.new()
  # Get the instance variables
  @config = irgat_process.config
  # load options for subsystem
  @config_module = self.load_config
  @init_time = Time.now
  # build the programs needed
  need_programs=["dar", "dar_manager", "gzip","tar"]
  # in function of options
  need_programs << "mysql" << "mysqldump" if @config_module[:dump_mysql]
  need_programs << "ssh" << "scp" if @config_module[:distribute_method]
  # check all the needed things
  check_dependencies(:check_programs       => need_programs,
                     :check_mounted_folder => (@config_module[:mounted_folder] ? @config_module[:working_path] : false),
                     :check_folders        => [ @config_module[:working_path],
                                                @config_module[:folders_tree] ],
                     :check_egid           => @config_module[:run_as_user]
                      )
end

Instance Method Details

#default(args = {}) ⇒ Object



43
44
45
# File 'lib/modules/backup.rb', line 43

def default(args =  {})
  exit_with_error("\n ** [ ERROR ] No default action for Backup Module. Avaible methods: \n    list, search, restore, launch", { :exit_level => 7 })
end

#launch(args = []) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/modules/backup.rb', line 99

def launch(args =  [])
  log("point", "Init Irgat Backup Launch")

  if @config_module[:dump_mysql]
    log("point","Doing MySQL database Dumping")
    do_mysql_dumps
  end

  # use the specific backup program
  self.send("do_backup_with_" + @config_module[:backup_program])

  # finish process || return STRING
  return "[IRGAT] [BACKUP] [OK] Backup Launch in #{ @config[:server_name] } server"
  
end

#list(args = []) ⇒ Object



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

def list(args =  [])
#      log("text", list_catalog[:output])
#      execute_command("su - \n","kk")
end

#restore(args = []) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/modules/backup.rb', line 78

def restore(args = [])
  backup_number = args[0] || nil
  restore_file  = args[1] || nil
  if !backup_number or !restore_file
    exit_with_error(" ** [ ERROR] Not Backup or restore file passed. Syntax is $ irgat backup restore <number of backup> <file to restore>", { :exit_level => 7 })
  else
    # identify backup file
    if ( !backup_data = get_backup_data(backup_number))
      exit_with_error("  ** [ ERROR ] Backup '#{ backup_number }' not found in catalog", { :exit_level => 3 })
    else
      # try to restore
      restore_command = "#{ @programs[:dar] } -x #{ backup_data[:file] } -R #{ @folders[:restore] } -K '#{ @config_module[:dar]["decrypt_passwd"] }' -v -g '#{ restore_file }'"
      result = execute_command(
          restore_command,
          "Restoring file",
          { :fatal_error => true })
      log("text"," Files were succesfull restored", 1)
    end
  end
end

#search(args = []) ⇒ Object



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
# File 'lib/modules/backup.rb', line 52

def search(args = [])
  if args.include?('-c')
    args.delete('-c')
    result = search_complete(args.first)
    if !result.empty?
      text =  "\n * The pattern match in: \n\n"
      text << "#{ result }"
      text << "\n * Restore the file with $ irgat backup restore <number backup> <file name>\n   File will be restored in #{ @folders[:restore] }/<file name> }"
      log("text", text, 1)
    else
      exit_with_error(" ** [ ERROR ] not pattern found.\n Exit", { :exit_level => 3} )
    end
  else
    result = search_file(args.first)
    if result[:status] == 0
      text =  "\n * The file exist in these backups \n\n"
      text << "#{ result[:output] }"
      text << "\n * Restore the file with $ irgat backup restore <number backup> <file>\n   File will be restored in #{ @folders[:restore] }/<file> }"
      log("text", text, 1)

    else
      exit_with_error("  ** [ ERROR ] not file found.\n Use $ irgat backup search <file> with -c option for a complete search", { :exit_level => 3} )
    end
  end
end