SQL_Dump 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/sqldump.rb +66 -54
  2. 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
- 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
- puts create
21
- res = my.query("select * from #{tbl}")
22
- num_rows = res.num_rows
23
- content = []
24
- num_rows.times do
25
- content.push(res.fetch_row.join("_____"))
26
- end
27
- res = my.query("select * from #{tbl}")
28
- num_rows = res.num_rows
29
- content = []
30
- num_rows.times do
31
- content.push(res.fetch_row.join("_____"))
32
- end
33
- content.each do |cont|
34
- res = my.query("explain #{tbl}")
35
- num_cats = res.num_rows
36
- y = 1
37
- cats = []
38
- print "insert into #{tbl}("
39
- res.each_hash do |x|
40
- print "#{x['Field']}" if y == num_cats
41
- print "#{x['Field']}, " if y != num_cats
42
- y += 1
43
- end
44
- print ") values("
45
- cont = cont.gsub(/'/, "\\\\'")
46
- cont = cont.gsub(/_____/, "', '")
47
- puts "'#{cont}');"
48
- end
49
- end
50
- rescue Mysql::Error => e
51
- puts "MySQL dump of #{db} failed!"
52
- puts "#{e.errno}: #{e.error}"
53
- puts "Exiting for safety"
54
- exit
55
- ensure
56
- my.close
57
- end
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: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 0
10
- version: 1.0.0
9
+ - 1
10
+ version: 1.0.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jason Colyer