Class: Dumptruck::Truck

Inherits:
Object
  • Object
show all
Defined in:
lib/dumptruck/truck.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Truck

Returns a new instance of Truck.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/dumptruck/truck.rb', line 28

def initialize(params)
  defaults = {
    output_dir: "/tmp",
    filename: "output",
    port: 3306,
    host: "localhost"
  }.merge!(params)

  @username      = defaults[:username]
  @password      = defaults[:password]
  @host          = defaults[:host]
  @port          = defaults[:port]
  @filename      = defaults[:filename]
  @output_dir    = defaults[:output_dir]

  Cocaine::CommandLine.path = params[:mysqldump_bin]
end

Instance Attribute Details

#filenameObject

filename, default is ‘output`, the filename will have a time/date string appended to it.



17
18
19
# File 'lib/dumptruck/truck.rb', line 17

def filename
  @filename
end

#hostObject

host, hostname where you are connecting too, defaults too localhost



11
12
13
# File 'lib/dumptruck/truck.rb', line 11

def host
  @host
end

#mysqldump_binObject

mysqldump needs to be installed locally, you can pass in the location if Ruby doesn’t find it



8
9
10
# File 'lib/dumptruck/truck.rb', line 8

def mysqldump_bin
  @mysqldump_bin
end

#output_dirObject

output_dir is where you’d like the output file to live after its run



26
27
28
# File 'lib/dumptruck/truck.rb', line 26

def output_dir
  @output_dir
end

#passwordObject

password for host



23
24
25
# File 'lib/dumptruck/truck.rb', line 23

def password
  @password
end

#portObject

port, other than 3306



14
15
16
# File 'lib/dumptruck/truck.rb', line 14

def port
  @port
end

#usernameObject

username for the host



20
21
22
# File 'lib/dumptruck/truck.rb', line 20

def username
  @username
end

Instance Method Details

#commandObject



78
79
80
81
82
83
84
85
# File 'lib/dumptruck/truck.rb', line 78

def command
  self.loads.each_with_index do |l,i|
    mysqldump = Cocaine::CommandLine.new('mysqldump',
      "#{self.credentials} #{l.options} #{l.ignored_tables}#{l.database}"
    )
    mysqldump.command
  end
end

#credentialsObject



46
47
48
49
50
51
52
# File 'lib/dumptruck/truck.rb', line 46

def credentials
  creds = "-u #{@username} "
  creds += "--password=#{@password} " unless @password.nil?
  creds += "-h #{@host} "
  creds += "-P #{@port}" unless @port.nil?
  creds
end

#dumpObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/dumptruck/truck.rb', line 87

def dump
  raise Dumptruck::NothingLoaded, "You haven't loaded anything!" unless @loads
  filename = self.output

  time = Benchmark.realtime do
    self.loads.each_with_index do |l,i|
      cmd = Cocaine::CommandLine.new("mysqldump",
        "#{self.credentials} #{l.options} #{l.ignored_tables}#{l.database} #{self.gzip} #{filename}"
      )

      begin
          cmd.run
      rescue Cocaine::ExitStatusError => e
        e
      rescue Cocaine::CommandNotFoundError => e
        raise Dumptruck::MysqlDumpNotFound, "Could not find the `mysqldump` command."
      end
    end
  end
    {time: time, file: "#{File.expand_path(filename)}"}
end

#gzipObject



74
75
76
# File 'lib/dumptruck/truck.rb', line 74

def gzip
  "| gzip -c >>"
end

#load(input) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/dumptruck/truck.rb', line 58

def load( input )
  @loads = [] unless @loads
  if input.instance_of? Dumptruck::Load #created separate from the truck
    @loads << input
  else
    input = Dumptruck::Load.new(input)
    @loads << input
  end

  input
end

#loadsObject



70
71
72
# File 'lib/dumptruck/truck.rb', line 70

def loads
  @loads.map
end

#outputObject



54
55
56
# File 'lib/dumptruck/truck.rb', line 54

def output
  "#{@output_dir}/#{@filename}_#{Time.now.strftime("%Y-%m-%d-%H%M")}.sql.gz"
end