Class: Mys3ql::Conductor

Inherits:
Object
  • Object
show all
Defined in:
lib/mys3ql/conductor.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_file = nil) ⇒ Conductor

Returns a new instance of Conductor.



21
22
23
24
25
# File 'lib/mys3ql/conductor.rb', line 21

def initialize(config_file = nil)
  @config = Config.new(config_file)
  @mysql = Mysql.new @config
  @s3 = S3.new @config
end

Class Method Details

.run(args) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/mys3ql/conductor.rb', line 9

def self.run(args)
  conductor = Conductor.new args.fetch(:config, nil)
  conductor.debug = args.fetch(:debug, false)

  command = args.fetch(:command)
  if command == 'restore'
    conductor.restore args.fetch(:after, nil)
  else
    conductor.send command
  end
end

Instance Method Details

#debug=(val) ⇒ Object



83
84
85
# File 'lib/mys3ql/conductor.rb', line 83

def debug=(val)
  @config.debug = val
end

#fullObject

Dumps the database and uploads it to S3. Copies the uploaded file to the key :latest. Deletes binary logs from the file system. Deletes binary logs from S3.



31
32
33
34
35
36
# File 'lib/mys3ql/conductor.rb', line 31

def full
  @mysql.dump
  @s3.store @mysql.dump_file
  @mysql.delete_dump
  @s3.delete_bin_logs
end

#incrementalObject

Uploads mysql’s binary logs to S3. The binary logs are left on the file system. Log files already on S3 are not re-uploaded.



41
42
43
44
45
# File 'lib/mys3ql/conductor.rb', line 41

def incremental
  @mysql.each_bin_log do |log|
    @s3.store log, false
  end
end

#restore(after = nil) ⇒ Object

When ‘after’ is nil:

  • downloads the latest dump from S3 and loads it into the database;

  • downloads each binary log from S3 and loads it into the database.

When ‘after’ is given:

  • downloads each binary log following ‘after’ from S3 and loads it into the database.

Downloaded files are removed from the file system.



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/mys3ql/conductor.rb', line 57

def restore(after = nil)
  unless after
    # get latest dump
    with_temp_file do |file|
      @s3.retrieve :latest, file
      @mysql.restore file
    end
  end

  # apply bin logs
  begin
    tmpfiles = []
    @s3.each_bin_log(after) do |log|
      file = Tempfile.new 'mys3ql'
      tmpfiles << file
      @s3.retrieve log, file.path
    end
    @mysql.apply_bin_logs tmpfiles.map(&:path)
  ensure
    tmpfiles.each &:close!
  end

  # NOTE: not sure about this:
  # puts "You might want to flush mysql's logs..."
end