SQL_Dump 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/sqldump.rb +66 -54
- metadata +3 -3
data/lib/sqldump.rb
CHANGED
@@ -1,58 +1,70 @@
|
|
1
|
-
#!/usr/bin/ruby
|
2
|
-
|
3
1
|
require 'rubygems'
|
4
2
|
require 'mysql'
|
5
3
|
def sqldump(user, pass, db)
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
4
|
+
time = Time.new
|
5
|
+
date = "#{time.year}-#{time.month}-#{time.day}"
|
6
|
+
begin
|
7
|
+
my = Mysql.new('localhost', user, pass, db)
|
8
|
+
res = my.query("show tables")
|
9
|
+
tables = []
|
10
|
+
num_tables = res.num_rows
|
11
|
+
num_tables.times do
|
12
|
+
tables.push(res.fetch_row)
|
13
|
+
end
|
14
|
+
tables.each do |tbl|
|
15
|
+
res = my.query("show create table #{tbl}")
|
16
|
+
create = ''
|
17
|
+
res.each_hash do |x|
|
18
|
+
create = "#{x['Create Table']};"
|
19
|
+
end
|
20
|
+
File.open("#{db}_#{date}.sql", "a") do |file|
|
21
|
+
file.puts(create)
|
22
|
+
end
|
23
|
+
res = my.query("select * from #{tbl}")
|
24
|
+
num_rows = res.num_rows
|
25
|
+
content = []
|
26
|
+
num_rows.times do
|
27
|
+
content.push(res.fetch_row.join("_____"))
|
28
|
+
end
|
29
|
+
res = my.query("select * from #{tbl}")
|
30
|
+
num_rows = res.num_rows
|
31
|
+
content = []
|
32
|
+
num_rows.times do
|
33
|
+
content.push(res.fetch_row.join("_____"))
|
34
|
+
end
|
35
|
+
content.each do |cont|
|
36
|
+
res = my.query("explain #{tbl}")
|
37
|
+
num_cats = res.num_rows
|
38
|
+
y = 1
|
39
|
+
cats = []
|
40
|
+
File.open("#{db}_#{date}.sql", "a") do |file|
|
41
|
+
file.print("insert into #{tbl}(")
|
42
|
+
end
|
43
|
+
res.each_hash do |x|
|
44
|
+
File.open("#{db}_#{date}.sql", "a") do |file|
|
45
|
+
file.print("#{x['Field']}") if y == num_cats
|
46
|
+
file.print("#{x['Field']}, ") if y != num_cats
|
47
|
+
end
|
48
|
+
y += 1
|
49
|
+
end
|
50
|
+
File.open("#{db}_#{date}.sql", "a") do |file|
|
51
|
+
file.print(") values(")
|
52
|
+
end
|
53
|
+
cont = cont.gsub(/'/, "\\\\'")
|
54
|
+
cont = cont.gsub(/_____/, "', '")
|
55
|
+
File.open("#{db}_#{date}.sql", "a") do |file|
|
56
|
+
file.puts("'#{cont}');")
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
rescue Mysql::Error => e
|
61
|
+
puts "MySQL dump of #{db} failed!"
|
62
|
+
puts "#{e.errno}: #{e.error}"
|
63
|
+
puts "Exiting for safety"
|
64
|
+
exit
|
65
|
+
ensure
|
66
|
+
my.close
|
67
|
+
end
|
68
|
+
cpuser = Dir.pwd.split('/')[2]
|
69
|
+
#FileUtils.chown_R "#{cpuser}", "#{cpuser}", "#{db}_#{date}.sql"
|
58
70
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: SQL_Dump
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 21
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 1.0.
|
9
|
+
- 1
|
10
|
+
version: 1.0.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Jason Colyer
|