cap_db_dump 1.2.0 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/cap_db_dump.rb +109 -0
  2. metadata +2 -2
  3. data/recipes/cap_db_dump.rb +0 -107
@@ -0,0 +1,109 @@
1
+ Capistrano::Configuration.instance(:must_exist).load do
2
+ namespace :database do
3
+ # a list of tables for which only the schema, but no data should be dumped.
4
+ set :schema_only_tables, []
5
+ set :dump_root_path, "/tmp"
6
+ set :formatted_time, Time.now.utc.strftime("%Y-%m-%d-%H:%M:%S")
7
+
8
+ module CapDbDumpHelpers
9
+ def dump_path
10
+ "#{dump_root_path}/#{database_name}_dump_#{formatted_time}.sql"
11
+ end
12
+
13
+ def give_description(desc_string)
14
+ puts " ** #{desc_string}"
15
+ end
16
+
17
+ def database_name
18
+ database_yml_in_env["database"]
19
+ end
20
+
21
+ def database_username
22
+ database_yml_in_env["username"]
23
+ end
24
+
25
+ def database_host
26
+ database_yml_in_env["host"] || "localhost"
27
+ end
28
+
29
+ def database_password
30
+ database_yml_in_env["password"]
31
+ end
32
+
33
+ def database_yml_in_env
34
+ database_yml[rails_env]
35
+ end
36
+
37
+ def database_yml
38
+ @database_yml ||= read_db_yml
39
+ end
40
+
41
+ def tasks_matching_for_db_dump
42
+ { :only => { :db_dump => true } }
43
+ end
44
+ end
45
+
46
+ extend CapDbDumpHelpers
47
+
48
+ task :read_db_yml, tasks_matching_for_db_dump do
49
+ @database_yml ||= begin
50
+ yaml = capture("cat #{shared_path}/config/database.yml")
51
+ YAML.load(yaml)
52
+ end
53
+ end
54
+
55
+ def password_field
56
+ database_password && database_password.length > 0 ? "-p#{database_password}" : ""
57
+ end
58
+
59
+ task :create_dump, tasks_matching_for_db_dump do
60
+ ignored_tables = schema_only_tables.map { |table_name|
61
+ "--ignore-table=#{database_name}.#{table_name}"
62
+ }
63
+
64
+ ignored_tables = ignored_tables.join(" ")
65
+
66
+ command = "mysqldump -u #{database_username} -h #{database_host} #{password_field} -Q "
67
+ command << "--add-drop-table -O add-locks=FALSE --lock-tables=FALSE --single-transaction "
68
+ command << "#{ignored_tables} #{database_name} > #{dump_path}"
69
+
70
+ give_description "About to dump production DB"
71
+
72
+ run command
73
+ dump_schema_tables if schema_only_tables.any?
74
+ end
75
+
76
+ task :dump_schema_tables, tasks_matching_for_db_dump do
77
+ if schema_only_tables.any?
78
+ table_names = schema_only_tables.join(" ")
79
+
80
+ command = "mysqldump -u #{database_username} -h #{database_host} #{password_field} "
81
+ command << "-Q --add-drop-table --single-transaction --no-data #{database_name} #{table_names} >> #{dump_path}"
82
+
83
+ give_description "Dumping schema for tables: #{schema_only_tables.join(", ")}"
84
+ run command
85
+ end
86
+ end
87
+
88
+ desc "Create a dump of the production database"
89
+ task :dump, tasks_matching_for_db_dump do
90
+ create_dump
91
+
92
+ cmd = "gzip -9 #{dump_path}"
93
+
94
+ give_description "Gzip'ing the file"
95
+ run cmd
96
+ end
97
+
98
+ desc "Make a production dump, transfer it to this machine"
99
+ task :dump_and_transfer, tasks_matching_for_db_dump do
100
+ dump
101
+ transfer
102
+ end
103
+
104
+ task :transfer, tasks_matching_for_db_dump do
105
+ give_description "Grabbing the dump"
106
+ download("#{dump_path}.gz", ".", :via => :scp)
107
+ end
108
+ end
109
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cap_db_dump
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -18,7 +18,7 @@ executables: []
18
18
  extensions: []
19
19
  extra_rdoc_files: []
20
20
  files:
21
- - recipes/cap_db_dump.rb
21
+ - lib/cap_db_dump.rb
22
22
  homepage: https://github.com/smtlaissezfaire/cap_db_dump
23
23
  licenses: []
24
24
  post_install_message:
@@ -1,107 +0,0 @@
1
- namespace :database do
2
- # a list of tables for which only the schema, but no data should be dumped.
3
- set :schema_only_tables, []
4
- set :dump_root_path, "/tmp"
5
- set :formatted_time, Time.now.utc.strftime("%Y-%m-%d-%H:%M:%S")
6
-
7
- module CapDbDumpHelpers
8
- def dump_path
9
- "#{dump_root_path}/#{database_name}_dump_#{formatted_time}.sql"
10
- end
11
-
12
- def give_description(desc_string)
13
- puts " ** #{desc_string}"
14
- end
15
-
16
- def database_name
17
- database_yml_in_env["database"]
18
- end
19
-
20
- def database_username
21
- database_yml_in_env["username"]
22
- end
23
-
24
- def database_host
25
- database_yml_in_env["host"] || "localhost"
26
- end
27
-
28
- def database_password
29
- database_yml_in_env["password"]
30
- end
31
-
32
- def database_yml_in_env
33
- database_yml[rails_env]
34
- end
35
-
36
- def database_yml
37
- @database_yml ||= read_db_yml
38
- end
39
-
40
- def tasks_matching_for_db_dump
41
- { :only => { :db_dump => true } }
42
- end
43
- end
44
-
45
- extend CapDbDumpHelpers
46
-
47
- task :read_db_yml, tasks_matching_for_db_dump do
48
- @database_yml ||= begin
49
- yaml = capture("cat #{shared_path}/config/database.yml")
50
- YAML.load(yaml)
51
- end
52
- end
53
-
54
- def password_field
55
- database_password && database_password.any? ? "-p#{database_password}" : ""
56
- end
57
-
58
- task :create_dump, tasks_matching_for_db_dump do
59
- ignored_tables = schema_only_tables.map { |table_name|
60
- "--ignore-table=#{database_name}.#{table_name}"
61
- }
62
-
63
- ignored_tables = ignored_tables.join(" ")
64
-
65
- command = "mysqldump -u #{database_username} -h #{database_host} #{password_field} -Q "
66
- command << "--add-drop-table -O add-locks=FALSE --lock-tables=FALSE --single-transaction "
67
- command << "#{ignored_tables} #{database_name} > #{dump_path}"
68
-
69
- give_description "About to dump production DB"
70
-
71
- run command
72
- dump_schema_tables if schema_only_tables.any?
73
- end
74
-
75
- task :dump_schema_tables, tasks_matching_for_db_dump do
76
- if schema_only_tables.any?
77
- table_names = schema_only_tables.join(" ")
78
-
79
- command = "mysqldump -u #{database_username} -h #{database_host} #{password_field} "
80
- command << "-Q --add-drop-table --single-transaction --no-data #{database_name} #{table_names} >> #{dump_path}"
81
-
82
- give_description "Dumping schema for tables: #{schema_only_tables.join(", ")}"
83
- run command
84
- end
85
- end
86
-
87
- desc "Create a dump of the production database"
88
- task :dump, tasks_matching_for_db_dump do
89
- create_dump
90
-
91
- cmd = "gzip -9 #{dump_path}"
92
-
93
- give_description "Gzip'ing the file"
94
- run cmd
95
- end
96
-
97
- desc "Make a production dump, transfer it to this machine"
98
- task :dump_and_transfer, tasks_matching_for_db_dump do
99
- dump
100
- transfer
101
- end
102
-
103
- task :transfer, tasks_matching_for_db_dump do
104
- give_description "Grabbing the dump"
105
- download("#{dump_path}.gz", ".", :via => :scp)
106
- end
107
- end