Class: BigBackup::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/big_backup/base.rb

Direct Known Subclasses

Uploader::Ftp

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



4
5
6
# File 'lib/big_backup/base.rb', line 4

def initialize
  @current_time = Time.now.strftime("%Y.%m.%d")
end

Instance Method Details

#archive_databasesObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/big_backup/base.rb', line 48

def archive_databases
  puts "\nArchive Backups".bright.color(:cyan)
  self.backup_config['databases'].each do |type, config|
    cmd_line = BigBackup::Archive.new(config).build_cmd(@current_time) + " > /dev/null 2>&1"
    unless cmd_line.blank?
      start_message(cmd_line)
      exec_cmd(cmd_line)
    end
  end
end

#archive_directoriesObject



74
75
76
77
78
79
80
81
# File 'lib/big_backup/base.rb', line 74

def archive_directories
  puts "\nCreate directories backups".bright.color(:cyan)
  cmd_line = BigBackup::Archive.new(self.backup_config['directories']).build_cmd + " > /dev/null 2>&1"
  unless cmd_line.blank?
    start_message(cmd_line)
    exec_cmd(cmd_line)
  end
end

#backup_configObject



29
30
31
32
33
34
35
# File 'lib/big_backup/base.rb', line 29

def backup_config
  if File.exists?(BigBackup.config_path)
    @backup_config ||= YAML.load_file(BigBackup.config_path)
  else
    @backup_config ||= YAML.load_file("#{File.dirname(__FILE__)}/../../config/bigbackup.yml")
  end
end

#backup_databasesObject



37
38
39
40
41
42
43
44
45
46
# File 'lib/big_backup/base.rb', line 37

def backup_databases
  puts "\nCreate Backups".bright.color(:cyan)
  self.backup_config['databases'].each do |type, config|
    cmd_line = BigBackup::Database::Base.build(type, config).build_cmd(@current_time)
    unless cmd_line.blank?
      start_message(cmd_line)
      exec_cmd(cmd_line)
    end
  end
end

#error_message(message) ⇒ Object



16
17
18
# File 'lib/big_backup/base.rb', line 16

def error_message(message)
  puts "ERROR".color(:red).bright+" -> #{message}"
end

#exec_cmd(line) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/big_backup/base.rb', line 20

def exec_cmd(line)
  system(line)
  if $?.exitstatus > 0
    error_message(line)
  else
    ok_message(line)
  end
end

#ok_message(message) ⇒ Object



12
13
14
# File 'lib/big_backup/base.rb', line 12

def ok_message(message)
  puts "OK".color(:green).bright+" -> #{message}"
end

#startObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/big_backup/base.rb', line 96

def start
  FileUtils.rm_rf "/tmp/bigbackup"
  Dir.mkdir "/tmp/bigbackup"
  if File.directory?("/tmp/bigbackup")
    puts "\nStart BigBackup \n".color(:green).bright
    if self.backup_config.include?('databases')
      self.backup_databases
      self.archive_databases
      self.upload_databases
    end
    if self.backup_config.include?('directories')
      self.archive_directories
      self.upload_directories
    end
    puts "\nStop BigBackup \n".color(:green).bright
  end
end

#start_message(message) ⇒ Object



8
9
10
# File 'lib/big_backup/base.rb', line 8

def start_message(message)
  puts "\nStart".bright+" -> #{message}"
end

#upload_databasesObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/big_backup/base.rb', line 59

def upload_databases
  if self.backup_config.include?('backup_server')
    puts "\nUpload Backups".bright.color(:cyan)
    self.backup_config['databases'].each do |type, config|
      start_message("upload #{type} backups")
      begin
        BigBackup::Uploader::Base.build(:ftp, config).upload(type, @current_time)
        ok_message("database backups uploaded")
      rescue Errno::ECONNREFUSED
        error_message("Can't connect to FTP Server")
      end
    end
  end
end

#upload_directoriesObject



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/big_backup/base.rb', line 83

def upload_directories
  if self.backup_config.include?('backup_server')
    puts "\nUpload directories Backups".bright.color(:cyan)
    start_message("upload directorie backups")
    begin
      BigBackup::Uploader::Base.build(:ftp, self.backup_config['directories']).upload("directories_backup", @current_time)
      ok_message("directories uploaded")
    rescue Errno::ECONNREFUSED
      error_message("Can't connect to FTP Server")
    end
  end
end