Class: Dumptruck::Truck
- Inherits:
-
Object
- Object
- Dumptruck::Truck
- Defined in:
- lib/dumptruck/truck.rb
Instance Attribute Summary collapse
-
#filename ⇒ Object
filename, default is ‘output`, the filename will have a time/date string appended to it.
-
#host ⇒ Object
host, hostname where you are connecting too, defaults too localhost.
-
#mysqldump_bin ⇒ Object
mysqldump needs to be installed locally, you can pass in the location if Ruby doesn’t find it.
-
#output_dir ⇒ Object
output_dir is where you’d like the output file to live after its run.
-
#password ⇒ Object
password for host.
-
#port ⇒ Object
port, other than 3306.
-
#username ⇒ Object
username for the host.
Instance Method Summary collapse
- #command ⇒ Object
- #credentials ⇒ Object
- #dump ⇒ Object
- #gzip ⇒ Object
-
#initialize(params) ⇒ Truck
constructor
A new instance of Truck.
- #load(input) ⇒ Object
- #loads ⇒ Object
- #output ⇒ Object
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
#filename ⇒ Object
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 |
#host ⇒ Object
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_bin ⇒ Object
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_dir ⇒ Object
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 |
#password ⇒ Object
password for host
23 24 25 |
# File 'lib/dumptruck/truck.rb', line 23 def password @password end |
#port ⇒ Object
port, other than 3306
14 15 16 |
# File 'lib/dumptruck/truck.rb', line 14 def port @port end |
#username ⇒ Object
username for the host
20 21 22 |
# File 'lib/dumptruck/truck.rb', line 20 def username @username end |
Instance Method Details
#command ⇒ Object
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.} #{l.ignored_tables}#{l.database}" ) mysqldump.command end end |
#credentials ⇒ Object
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 |
#dump ⇒ Object
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.} #{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.(filename)}"} end |
#gzip ⇒ Object
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 |
#loads ⇒ Object
70 71 72 |
# File 'lib/dumptruck/truck.rb', line 70 def loads @loads.map end |
#output ⇒ Object
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 |