Class: Muck::Backup

Inherits:
Object
  • Object
show all
Includes:
Logging, Utils
Defined in:
lib/muck/backup.rb

Instance Method Summary collapse

Methods included from Utils

#blue, #green, #pink, #red, #yellow

Methods included from Logging

#log, #logger

Constructor Details

#initialize(database) ⇒ Backup

Returns a new instance of Backup.



11
12
13
14
# File 'lib/muck/backup.rb', line 11

def initialize(database)
  @database = database
  @time = Time.now
end

Instance Method Details

#compressObject



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/muck/backup.rb', line 70

def compress
  if File.exist?(export_path)
    if system("gzip #{export_path}")
      @export_path = @export_path + ".gz"
      logger.info "Compressed #{blue export_path} with gzip"
    else
      logger.warn "Couldn't compress #{export_path} with gzip"
    end
  else
    raise Error, "Couldn't compress backup because it doesn't exist at #{export_path}"
  end
end

#export_pathObject



16
17
18
# File 'lib/muck/backup.rb', line 16

def export_path
  @export_path ||= File.join(@database.export_path, "master", @time.strftime("%Y-%m-%d-%H-%M-%S.sql"))
end

#runObject



20
21
22
23
24
25
26
# File 'lib/muck/backup.rb', line 20

def run
  logger.info "Backing up #{blue @database.name} from #{blue @database.server.hostname}"
  take_backup
  compress
  store_in_manifest
  tidy_masters
end

#store_in_manifestObject



60
61
62
63
64
65
66
67
68
# File 'lib/muck/backup.rb', line 60

def store_in_manifest
  if File.exist?(export_path)
    details = {:timestamp => @time.to_i, :path => export_path, :size => File.size(export_path)}
    @database.manifest[:backups] << details
    @database.save_manifest
  else
    raise Error, "Couldn't store backup in manifest because it doesn't exist at #{export_path}"
  end
end

#take_backupObject



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
53
54
55
56
57
58
# File 'lib/muck/backup.rb', line 28

def take_backup
  logger.info "Connecting to #{blue @database.server.ssh_username}@#{blue @database.server.hostname}:#{blue @database.server.ssh_port}"
  FileUtils.mkdir_p(File.dirname(self.export_path))
  file = File.open(export_path, 'w')
  ssh_session = @database.server.create_ssh_session
  channel = ssh_session.open_channel do |channel|
    logger.debug "Running: #{@database.dump_command}"
    channel.exec(@database.dump_command) do |channel, success|
      raise Error, "Could not execute dump command" unless success
      channel.on_data do |c, data|
        file.write(data)
      end

      channel.on_extended_data do |c, _, data|
        logger.debug red(data.gsub(/[\r\n]/, ''))
      end

      channel.on_request("exit-status") do |_, data|
        exit_code = data.read_long
        if exit_code != 0
          logger.debug "Exit status was #{exit_code}"
          raise Error, "mysqldump returned an error when executing."
        end
      end
    end
  end
  channel.wait
  ssh_session.close
  file.close
  logger.info "Successfully backed up to #{green export_path}"
end

#tidy_mastersObject



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

def tidy_masters
  files = Dir[File.join(@database.export_path, 'master', '*')].sort.reverse.drop(@database.server.masters_to_keep)
  unless files.empty?
    logger.info "Tidying master backup files. Keeping #{@database.server.masters_to_keep} back."
    files.each do |file|
      if system("rm #{file}")
        @database.manifest[:backups].delete_if { |b| b[:path] == file }
        logger.info "-> Removed #{green file}"
      else
        logger.error red("-> Couldn't remove unwanted master file at #{file}")
      end
    end
  end
ensure
  @database.save_manifest
end