cap_db_dump 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/recipes/cap_db_dump.rb +107 -0
  2. metadata +46 -0
@@ -0,0 +1,107 @@
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
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cap_db_dump
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Scott Taylor
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-06 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Capistrano tasks for dumping your mysql database + transfering to your
15
+ local machine
16
+ email: scott@railsnewbie.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - recipes/cap_db_dump.rb
22
+ homepage: https://github.com/smtlaissezfaire/cap_db_dump
23
+ licenses: []
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 1.8.24
43
+ signing_key:
44
+ specification_version: 3
45
+ summary: cap_db_dump
46
+ test_files: []