class Dumptruck::Truck
Attributes
filename[RW]
filename, default is ‘output`, the filename will have a time/date string appended to it.
host[RW]
host, hostname where you are connecting too, defaults too localhost
mysqldump_bin[RW]
mysqldump needs to be installed locally, you can pass in the location if Ruby doesn’t find it
output_dir[RW]
output_dir
is where you’d like the output file to live after its run
password[RW]
password for host
port[RW]
port, other than 3306
username[RW]
username for the host
Public Class Methods
new(params)
click to toggle source
# 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
Public Instance Methods
command()
click to toggle source
# 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
credentials()
click to toggle source
# 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
dump()
click to toggle source
# 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 file = File.open(filename) {time: time, file: file} end
gzip()
click to toggle source
# File lib/dumptruck/truck.rb, line 74 def gzip "| gzip -c >>" end
load( input )
click to toggle source
# 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
loads()
click to toggle source
# File lib/dumptruck/truck.rb, line 70 def loads @loads.map end
output()
click to toggle source
# File lib/dumptruck/truck.rb, line 54 def output "#{@output_dir}/#{@filename}_#{Time.now.strftime("%Y-%m-%d-%H%M")}.sql.gz" end