SQL_Dump 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/sqldump.rb +69 -67
- metadata +3 -3
data/lib/sqldump.rb
CHANGED
@@ -1,70 +1,72 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'mysql'
|
3
|
-
|
4
|
-
|
5
|
-
|
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
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
3
|
+
class SQLDUMP
|
4
|
+
def sqldump(user, pass, db)
|
5
|
+
time = Time.new
|
6
|
+
date = "#{time.year}-#{time.month}-#{time.day}"
|
7
|
+
begin
|
8
|
+
my = Mysql.new('localhost', user, pass, db)
|
9
|
+
res = my.query("show tables")
|
10
|
+
tables = []
|
11
|
+
num_tables = res.num_rows
|
12
|
+
num_tables.times do
|
13
|
+
tables.push(res.fetch_row)
|
14
|
+
end
|
15
|
+
tables.each do |tbl|
|
16
|
+
res = my.query("show create table #{tbl}")
|
17
|
+
create = ''
|
18
|
+
res.each_hash do |x|
|
19
|
+
create = "#{x['Create Table']};"
|
20
|
+
end
|
21
|
+
File.open("#{db}_#{date}.sql", "a") do |file|
|
22
|
+
file.puts(create)
|
23
|
+
end
|
24
|
+
res = my.query("select * from #{tbl}")
|
25
|
+
num_rows = res.num_rows
|
26
|
+
content = []
|
27
|
+
num_rows.times do
|
28
|
+
content.push(res.fetch_row.join("_____"))
|
29
|
+
end
|
30
|
+
res = my.query("select * from #{tbl}")
|
31
|
+
num_rows = res.num_rows
|
32
|
+
content = []
|
33
|
+
num_rows.times do
|
34
|
+
content.push(res.fetch_row.join("_____"))
|
35
|
+
end
|
36
|
+
content.each do |cont|
|
37
|
+
res = my.query("explain #{tbl}")
|
38
|
+
num_cats = res.num_rows
|
39
|
+
y = 1
|
40
|
+
cats = []
|
41
|
+
File.open("#{db}_#{date}.sql", "a") do |file|
|
42
|
+
file.print("insert into #{tbl}(")
|
43
|
+
end
|
44
|
+
res.each_hash do |x|
|
45
|
+
File.open("#{db}_#{date}.sql", "a") do |file|
|
46
|
+
file.print("#{x['Field']}") if y == num_cats
|
47
|
+
file.print("#{x['Field']}, ") if y != num_cats
|
48
|
+
end
|
49
|
+
y += 1
|
50
|
+
end
|
51
|
+
File.open("#{db}_#{date}.sql", "a") do |file|
|
52
|
+
file.print(") values(")
|
53
|
+
end
|
54
|
+
cont = cont.gsub(/'/, "\\\\'")
|
55
|
+
cont = cont.gsub(/_____/, "', '")
|
56
|
+
File.open("#{db}_#{date}.sql", "a") do |file|
|
57
|
+
file.puts("'#{cont}');")
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
rescue Mysql::Error => e
|
62
|
+
puts "MySQL dump of #{db} failed!"
|
63
|
+
puts "#{e.errno}: #{e.error}"
|
64
|
+
puts "Exiting for safety"
|
65
|
+
exit
|
66
|
+
ensure
|
67
|
+
my.close
|
68
|
+
end
|
69
|
+
cpuser = Dir.pwd.split('/')[2]
|
70
|
+
#FileUtils.chown_R "#{cpuser}", "#{cpuser}", "#{db}_#{date}.sql"
|
71
|
+
end
|
70
72
|
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: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 1.0.
|
9
|
+
- 2
|
10
|
+
version: 1.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Jason Colyer
|