mysql_dumper 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +45 -0
- data/Rakefile +1 -0
- data/bin/mysql_dumper +94 -0
- data/lib/mysql_dumper/version.rb +3 -0
- data/lib/mysql_dumper.rb +31 -0
- data/mysql_dumper.gemspec +24 -0
- data/spec/mysql_dumper_spec.rb +89 -0
- metadata +99 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2f9f14de100bde7364e2ccf399e9bf84338dba40
|
4
|
+
data.tar.gz: 5238aa528a8f86ba2bf5b8f0a1986bb63558e01c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2b710df22ed383236b7f599744bf88d9c291667cbf577d83e8a90f6280fead5e6e6bc8f1b094c3f61fdf7191407ca5015f2aab3e4529470801c03e5daa1f8d84
|
7
|
+
data.tar.gz: 437a8c5437bb08e442090aec0dd300218020cb0a809c61ba1f12ce28058ce7e102bfab74b85173eedd8a45cc9a0546787797f99a6e1efd9dd5694dc2b2f425b7
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Yang-Hsing Lin
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# MysqlDumper
|
2
|
+
|
3
|
+
MysqlDumper is a ruby wrapper of `mysqldump`.
|
4
|
+
It provides both ruby and command line interface.
|
5
|
+
|
6
|
+
## Usage
|
7
|
+
|
8
|
+
### Ruby interface:
|
9
|
+
|
10
|
+
require 'mysql_dumper'
|
11
|
+
config = {
|
12
|
+
"database" => "the_db_name",
|
13
|
+
"username" => "db_user_name"
|
14
|
+
"password" => "xxxx"
|
15
|
+
}
|
16
|
+
|
17
|
+
dumper = MysqlDumper.new config
|
18
|
+
|
19
|
+
# dump whole db, including stored procedure
|
20
|
+
dumper.dump_to("path/to/db.sql")
|
21
|
+
|
22
|
+
# dump only schema and certain tables
|
23
|
+
dumper.dump_schema_to("path/to/db.sql", { :preserve => ["table1", "table2"] })
|
24
|
+
|
25
|
+
# load a sql file to a database
|
26
|
+
dumper.load_from("path/to/db.sql")
|
27
|
+
|
28
|
+
### command-line interface: (more expressive than native mysqldump interface)
|
29
|
+
|
30
|
+
1. dump whole db:
|
31
|
+
`$ mysql_dumper dump DBNAME --to path/to/db.sql -u USERNAME [-p PASSWORD]`
|
32
|
+
|
33
|
+
2. dump schema only:
|
34
|
+
`$ mysql_dumper dump_schema DBNAME --to path/to/schema.sql -u USERNAME [--perserve table1,table2...] [-p PASSWORD]`
|
35
|
+
|
36
|
+
3. load from schema:
|
37
|
+
`$ mysql_dumper load DBNAME --from path/to/db.sql -u USERNAME [-p PASSWORD]`
|
38
|
+
|
39
|
+
## Contributing
|
40
|
+
|
41
|
+
1. Fork it
|
42
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
43
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
44
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
45
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/mysql_dumper
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
#! /usr/bin/env ruby
|
2
|
+
# command line interface
|
3
|
+
# usage:
|
4
|
+
# 1. dump whole db:
|
5
|
+
# $ mysql_dumper dump DBNAME --to SQL_FILE_PATH -u USERNAME
|
6
|
+
#
|
7
|
+
# 2. dump schema:
|
8
|
+
# $ mysql_dumper dump_schema DBNAME --to SQL_FILE_PATH -u USERNAME [--perserve t1,t2...]
|
9
|
+
#
|
10
|
+
# 3. load schema to db:
|
11
|
+
# $ mysql_dumper load DBNAME --from SQL_FILE_PATH -u USERNAME
|
12
|
+
|
13
|
+
require 'optparse'
|
14
|
+
require "mysql_dumper"
|
15
|
+
|
16
|
+
class ArgumentSet
|
17
|
+
attr_accessor :config, :action, :file, :preserve, :username, :error
|
18
|
+
VALID_ACTIONS = [ "dump", "dump_schema", "load" ]
|
19
|
+
def initialize
|
20
|
+
@config = {}
|
21
|
+
parser.parse! ARGV
|
22
|
+
@action = ARGV[0]
|
23
|
+
@config["database"] = ARGV[1]
|
24
|
+
@error = ""
|
25
|
+
end
|
26
|
+
|
27
|
+
def parser
|
28
|
+
@parser ||= OptionParser.new do |opts|
|
29
|
+
opts.banner = "Usage: mysql_dumper ACTION [options]"
|
30
|
+
opts.on("-u", "--username USERNAME", "username to connect") do |username|
|
31
|
+
@config["username"] = username
|
32
|
+
end
|
33
|
+
opts.on("-f", "--to FILENAME", "file path of sql") do |filename|
|
34
|
+
@file = filename
|
35
|
+
end
|
36
|
+
opts.on("-f", "--from FILENAME", "file path of sql") do |filename|
|
37
|
+
@file = filename
|
38
|
+
end
|
39
|
+
opts.on("-p", "--password PASSWORD", "password of db") do |pwd|
|
40
|
+
@config["password"] = pwd
|
41
|
+
end
|
42
|
+
opts.on("-t", "--preserve DBNAME1,DBNAME2...", Array, "tables to preserve") do |tables|
|
43
|
+
@preserve = tables
|
44
|
+
end
|
45
|
+
opts.on("-h", "--help") do
|
46
|
+
puts opts
|
47
|
+
exit
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def valid?
|
53
|
+
ensure_exists @config["username"], "username"
|
54
|
+
ensure_exists @config["database"], "database"
|
55
|
+
ensure_exists @file, "sql_file"
|
56
|
+
validate_action
|
57
|
+
|
58
|
+
@error == ""
|
59
|
+
end
|
60
|
+
|
61
|
+
def ensure_exists param, attr_name
|
62
|
+
if ! param
|
63
|
+
@error += "missing argument #{attr_name}\n"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def validate_action
|
68
|
+
if ! VALID_ACTIONS.include? @action
|
69
|
+
@error += "invalid action: #{@action}"
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def show_help
|
74
|
+
puts parser
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
arguments = ArgumentSet.new
|
80
|
+
|
81
|
+
if arguments.valid?
|
82
|
+
dumper = MysqlDumper.new(arguments.config)
|
83
|
+
case arguments.action
|
84
|
+
when "dump"
|
85
|
+
dumper.dump_to(arguments.file)
|
86
|
+
when "dump_schema"
|
87
|
+
dumper.dump_schema_to(arguments.file, { :preserve => arguments.preserve })
|
88
|
+
when "load"
|
89
|
+
dumper.load_from(arguments.file)
|
90
|
+
end
|
91
|
+
else
|
92
|
+
puts arguments.error
|
93
|
+
arguments.show_help
|
94
|
+
end
|
data/lib/mysql_dumper.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require "mysql_dumper/version"
|
2
|
+
|
3
|
+
class MysqlDumper
|
4
|
+
class InitFailedException < Exception; end;
|
5
|
+
def initialize config
|
6
|
+
@password = config["password"]
|
7
|
+
@username = config["username"]
|
8
|
+
@database = config["database"]
|
9
|
+
raise InitFailedException, "username is required to init a dumper" unless @username
|
10
|
+
raise InitFailedException, "database is required to init a dumper" unless @database
|
11
|
+
end
|
12
|
+
|
13
|
+
def dump_schema_to path, options = {}
|
14
|
+
preserved_tables = options[:preserve] || []
|
15
|
+
table_string = preserved_tables.join(" ")
|
16
|
+
|
17
|
+
system "mysqldump -u #{@username} -p#{@password} -R -d --skip-comments #{@database} > #{path}"
|
18
|
+
if ! table_string.strip.empty?
|
19
|
+
system "mysqldump -u #{@username} -p#{@password} --skip-comments #{@database} #{table_string} >> #{path}"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def dump_to path
|
24
|
+
system "mysqldump -u #{@username} -p#{@password} -R --skip-comments #{@database} > #{path}"
|
25
|
+
end
|
26
|
+
|
27
|
+
def load_from path
|
28
|
+
system "cat #{path} | mysql -u #{@username} -p#{@password} #{@database}"
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'mysql_dumper/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "mysql_dumper"
|
8
|
+
spec.version = MysqlDumper::VERSION
|
9
|
+
spec.authors = ["Yang-Hsing Lin"]
|
10
|
+
spec.email = ["yanghsing.lin@gmail.com"]
|
11
|
+
spec.description = %q{mysqldump wrapper}
|
12
|
+
spec.summary = %q{provides both command line and ruby interface}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rspec"
|
23
|
+
spec.add_development_dependency "rake"
|
24
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require "mysql_dumper"
|
2
|
+
|
3
|
+
describe MysqlDumper do
|
4
|
+
let(:database) { "db_name" }
|
5
|
+
let(:username) { "the_username" }
|
6
|
+
let(:password) { "the_pwd" }
|
7
|
+
before(:each) do
|
8
|
+
@config = {
|
9
|
+
"database" => database,
|
10
|
+
"username" => username,
|
11
|
+
"password" => password
|
12
|
+
}
|
13
|
+
end
|
14
|
+
|
15
|
+
it "takes a config to init" do
|
16
|
+
dumper = MysqlDumper.new @config
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.ensure_in_config attr_name
|
20
|
+
it "raises MysqlDumper::InitFailedException if no #{attr_name}" do
|
21
|
+
@config.delete attr_name
|
22
|
+
|
23
|
+
expect {
|
24
|
+
MysqlDumper.new @config
|
25
|
+
}.to raise_error(MysqlDumper::InitFailedException,
|
26
|
+
"#{attr_name} is required to init a dumper")
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
ensure_in_config "username"
|
31
|
+
ensure_in_config "database"
|
32
|
+
|
33
|
+
context "dumping methods" do
|
34
|
+
let(:file_path) { "/tmp/dummy.sql" }
|
35
|
+
before(:each) do
|
36
|
+
@dumper = MysqlDumper.new @config
|
37
|
+
@dumper.stub(:system)
|
38
|
+
end
|
39
|
+
describe "#dump_schema_to(destination_file[, options])" do
|
40
|
+
it "dump schema to destination_file" do
|
41
|
+
executed = false
|
42
|
+
@dumper.stub(:system) do |command|
|
43
|
+
sql = <<-SQL
|
44
|
+
mysqldump -u #{username} -p#{password} -R -d --skip-comments #{database} > #{file_path}
|
45
|
+
SQL
|
46
|
+
command.should == sql.strip
|
47
|
+
executed = true
|
48
|
+
end
|
49
|
+
|
50
|
+
@dumper.dump_schema_to(file_path)
|
51
|
+
executed.should be_true
|
52
|
+
end
|
53
|
+
|
54
|
+
it "preserves tables data if specified" do
|
55
|
+
table1 = "table1"
|
56
|
+
table2 = "table2"
|
57
|
+
sql_schema_only =
|
58
|
+
"mysqldump -u #{username} -p#{password} -R -d --skip-comments #{database} > #{file_path}"
|
59
|
+
sql_with_tables =
|
60
|
+
"mysqldump -u #{username} -p#{password} --skip-comments #{database} #{table1} #{table2} >> #{file_path}"
|
61
|
+
|
62
|
+
@dumper.should_receive(:system).with(sql_schema_only)
|
63
|
+
@dumper.should_receive(:system).with(sql_with_tables)
|
64
|
+
|
65
|
+
@dumper.dump_schema_to(file_path, { :preserve => [ table1, table2 ] })
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe "#dump_to(destination_file)" do
|
70
|
+
it "dumps whole db to destination_file" do
|
71
|
+
@dumper.should_receive(:system).
|
72
|
+
with("mysqldump -u #{username} -p#{password} -R --skip-comments #{database} > #{file_path}")
|
73
|
+
@dumper.dump_to(file_path)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
describe "#load_from(sql_file)" do
|
78
|
+
it "loads sql_file to specific db" do
|
79
|
+
@dumper.should_receive(:system).
|
80
|
+
with("cat #{file_path} | mysql -u #{username} -p#{password} #{database}")
|
81
|
+
@dumper.load_from(file_path)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
end
|
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mysql_dumper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yang-Hsing Lin
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-09-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: mysqldump wrapper
|
56
|
+
email:
|
57
|
+
- yanghsing.lin@gmail.com
|
58
|
+
executables:
|
59
|
+
- mysql_dumper
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- .gitignore
|
64
|
+
- .rspec
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/mysql_dumper
|
70
|
+
- lib/mysql_dumper.rb
|
71
|
+
- lib/mysql_dumper/version.rb
|
72
|
+
- mysql_dumper.gemspec
|
73
|
+
- spec/mysql_dumper_spec.rb
|
74
|
+
homepage: ''
|
75
|
+
licenses:
|
76
|
+
- MIT
|
77
|
+
metadata: {}
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - '>='
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 2.0.7
|
95
|
+
signing_key:
|
96
|
+
specification_version: 4
|
97
|
+
summary: provides both command line and ruby interface
|
98
|
+
test_files:
|
99
|
+
- spec/mysql_dumper_spec.rb
|