Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Q
qa-deploy-utils
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
QA
qa-deploy-utils
Commits
00197a78
Commit
00197a78
authored
Feb 03, 2017
by
daidekun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
db
parent
1e45a674
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
118 additions
and
0 deletions
+118
-0
mysql-dump-tables.rb
db-utils/mysql-dump-tables.rb
+118
-0
No files found.
db-utils/mysql-dump-tables.rb
0 → 100755
View file @
00197a78
#!/usr/bin/env ruby
require
'optparse'
tables
=
[]
ignore
=
[]
use_database
=
nil
dumpfile
=
""
cmds
=
OptionParser
.
new
do
|
opts
|
opts
.
banner
=
"Usage: mysql-dump-tables.rb [options] [FILE]"
opts
.
on
(
"-s"
,
"Read from stdin"
)
do
dumpfile
=
$stdin
end
opts
.
on
(
"-t"
,
'--tables TABLES'
,
Array
,
"Extract only these tables"
)
do
|
t
|
tables
=
t
end
opts
.
on
(
"-i"
,
'--ignore-tables TABLES'
,
Array
,
"Ignore these tables"
)
do
|
i
|
ignore
=
i
end
opts
.
on
(
"-u"
,
'--use-database NAME'
,
String
,
"Assume NAME as database name"
)
do
|
n
|
use_database
=
n
end
opts
.
on_tail
(
"-h"
,
"--help"
)
do
puts
opts
end
end
.
parse!
if
dumpfile
==
""
dumpfile
=
ARGV
.
shift
if
not
dumpfile
puts
"Nothing to do"
exit
end
end
STDOUT
.
sync
=
true
class
Numeric
def
bytes_to_human
units
=
%w{B KB MB GB TB}
e
=
self
>
0
?
(
Math
.
log
(
self
)
/
Math
.
log
(
1024
)).
floor
:
0
s
=
"%.3f"
%
(
to_f
/
1024
**
e
)
s
.
sub
(
/\.?0*$/
,
units
[
e
])
end
end
if
File
.
exist?
(
dumpfile
)
if
dumpfile
==
$stdin
d
=
$stdin
else
d
=
File
.
new
(
dumpfile
,
"r:binary"
)
end
outfile
=
nil
table
=
nil
db
=
use_database
linecount
=
tablecount
=
starttime
=
0
while
(
line
=
d
.
gets
)
# Detect table changes
if
line
=~
/^-- Table structure for table .(.+)./
or
line
=~
/^-- Dumping data for table .(.+)./
or
line
=~
/^# Dump of table.(.+)/
is_new_table
=
table
!=
$1
table
=
$1
# previous file should be closed
if
is_new_table
outfile
.
close
if
outfile
and
!
outfile
.
closed?
puts
(
"
\n\n
Found a new table:
#{
table
}
"
)
if
(
tables
!=
[]
and
not
tables
.
include?
(
table
))
puts
"`
#{
table
}
` not in list, ignoring"
table
=
nil
elsif
(
ignore
!=
[]
and
ignore
.
include?
(
table
))
puts
"`
#{
table
}
` will be ignored"
table
=
nil
else
starttime
=
Time
.
now
linecount
=
0
tablecount
+=
1
path
=
(
db
.
to_s
==
""
?
""
:
"
#{
db
}
/"
)
+
"tables"
;
Dir
.
mkdir
(
path
)
unless
File
.
exists?
(
path
)
outfile
=
File
.
new
(
"
#{
path
}
/
#{
table
}
.sql"
,
"w"
)
outfile
.
syswrite
(
"USE `
#{
db
}
`;
\n\n
"
)
end
end
elsif
line
=~
/^-- Current Database: .(.+)./
db
=
$1
table
=
nil
outfile
.
close
if
outfile
and
!
outfile
.
closed?
Dir
.
mkdir
(
db
)
Dir
.
mkdir
(
"
#{
db
}
/tables"
)
outfile
=
File
.
new
(
"
#{
db
}
/create.sql"
,
"w"
)
puts
(
"
\n\n
Found a new db:
#{
db
}
"
)
elsif
line
=~
/^-- Position to start replication or point-in-time recovery from/
db
=
nil
table
=
nil
outfile
.
close
if
outfile
and
!
outfile
.
closed?
outfile
=
File
.
new
(
"1replication.sql"
,
"w"
)
puts
(
"
\n\n
Found replication data"
)
end
# Write line to outfile
if
outfile
and
!
outfile
.
closed?
outfile
.
syswrite
(
line
)
linecount
+=
1
elapsed
=
Time
.
now
.
to_i
-
starttime
.
to_i
+
1
print
(
" writing line:
#{
linecount
}
#{
outfile
.
stat
.
size
.
bytes_to_human
}
in
#{
elapsed
}
seconds
#{
(
outfile
.
stat
.
size
/
elapsed
).
bytes_to_human
}
/sec
\r
"
)
end
end
end
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment