dump 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. data/.autotest +13 -0
  2. data/.gitignore +12 -0
  3. data/LICENSE.txt +20 -0
  4. data/README.markdown +250 -0
  5. data/dump.gemspec +22 -0
  6. data/lib/dump.rb +3 -0
  7. data/lib/dump/capistrano.rb +1 -0
  8. data/lib/dump/railtie.rb +8 -0
  9. data/lib/dump_rake.rb +85 -0
  10. data/lib/dump_rake/archive_tar_minitar_fix.rb +8 -0
  11. data/lib/dump_rake/assets.rb +22 -0
  12. data/lib/dump_rake/continious_timeout.rb +38 -0
  13. data/lib/dump_rake/dump.rb +175 -0
  14. data/lib/dump_rake/dump_reader.rb +289 -0
  15. data/lib/dump_rake/dump_writer.rb +119 -0
  16. data/lib/dump_rake/env.rb +139 -0
  17. data/lib/dump_rake/env/filter.rb +26 -0
  18. data/lib/dump_rake/rails_root.rb +12 -0
  19. data/lib/dump_rake/table_manipulation.rb +131 -0
  20. data/lib/generators/assets_config/assets_config_generator.rb +16 -0
  21. data/lib/generators/assets_config/templates/assets +8 -0
  22. data/lib/tasks/assets.rake +17 -0
  23. data/lib/tasks/dump.rake +27 -0
  24. data/recipes/dump.rb +343 -0
  25. data/script/update_readme +21 -0
  26. data/spec/.gitignore +1 -0
  27. data/spec/.tmignore +1 -0
  28. data/spec/cycle_spec.rb +229 -0
  29. data/spec/db/database.example.yml +19 -0
  30. data/spec/db/schema.rb +7 -0
  31. data/spec/dummy-3.1.3/.gitignore +15 -0
  32. data/spec/dummy-3.1.3/.rspec +1 -0
  33. data/spec/dummy-3.1.3/Gemfile +23 -0
  34. data/spec/dummy-3.1.3/Gemfile.lock +159 -0
  35. data/spec/dummy-3.1.3/README +261 -0
  36. data/spec/dummy-3.1.3/Rakefile +7 -0
  37. data/spec/dummy-3.1.3/app/assets/images/rails.png +0 -0
  38. data/spec/dummy-3.1.3/app/assets/javascripts/application.js +9 -0
  39. data/spec/dummy-3.1.3/app/assets/stylesheets/application.css +7 -0
  40. data/spec/dummy-3.1.3/app/controllers/application_controller.rb +3 -0
  41. data/spec/dummy-3.1.3/app/helpers/application_helper.rb +2 -0
  42. data/spec/dummy-3.1.3/app/mailers/.gitkeep +0 -0
  43. data/spec/dummy-3.1.3/app/models/.gitkeep +0 -0
  44. data/spec/dummy-3.1.3/app/views/layouts/application.html.erb +14 -0
  45. data/spec/dummy-3.1.3/config.ru +4 -0
  46. data/spec/dummy-3.1.3/config/application.rb +54 -0
  47. data/spec/dummy-3.1.3/config/boot.rb +6 -0
  48. data/spec/dummy-3.1.3/config/database.yml +25 -0
  49. data/spec/dummy-3.1.3/config/environment.rb +5 -0
  50. data/spec/dummy-3.1.3/config/environments/development.rb +30 -0
  51. data/spec/dummy-3.1.3/config/environments/production.rb +60 -0
  52. data/spec/dummy-3.1.3/config/environments/test.rb +39 -0
  53. data/spec/dummy-3.1.3/config/initializers/backtrace_silencers.rb +7 -0
  54. data/spec/dummy-3.1.3/config/initializers/inflections.rb +10 -0
  55. data/spec/dummy-3.1.3/config/initializers/mime_types.rb +5 -0
  56. data/spec/dummy-3.1.3/config/initializers/secret_token.rb +7 -0
  57. data/spec/dummy-3.1.3/config/initializers/session_store.rb +8 -0
  58. data/spec/dummy-3.1.3/config/initializers/wrap_parameters.rb +14 -0
  59. data/spec/dummy-3.1.3/config/locales/en.yml +5 -0
  60. data/spec/dummy-3.1.3/config/routes.rb +58 -0
  61. data/spec/dummy-3.1.3/db/seeds.rb +7 -0
  62. data/spec/dummy-3.1.3/doc/README_FOR_APP +2 -0
  63. data/spec/dummy-3.1.3/lib/assets/.gitkeep +0 -0
  64. data/spec/dummy-3.1.3/lib/tasks/.gitkeep +0 -0
  65. data/spec/dummy-3.1.3/log/.gitkeep +0 -0
  66. data/spec/dummy-3.1.3/public/404.html +26 -0
  67. data/spec/dummy-3.1.3/public/422.html +26 -0
  68. data/spec/dummy-3.1.3/public/500.html +26 -0
  69. data/spec/dummy-3.1.3/public/favicon.ico +0 -0
  70. data/spec/dummy-3.1.3/public/index.html +241 -0
  71. data/spec/dummy-3.1.3/public/robots.txt +5 -0
  72. data/spec/dummy-3.1.3/script/rails +6 -0
  73. data/spec/dummy-3.1.3/spec/spec_helper.rb +32 -0
  74. data/spec/dummy-3.1.3/vendor/assets/stylesheets/.gitkeep +0 -0
  75. data/spec/dummy-3.1.3/vendor/plugins/.gitkeep +0 -0
  76. data/spec/lib/dump_rake/dump_reader_spec.rb +638 -0
  77. data/spec/lib/dump_rake/dump_spec.rb +291 -0
  78. data/spec/lib/dump_rake/dump_writer_spec.rb +328 -0
  79. data/spec/lib/dump_rake/env/filter_spec.rb +56 -0
  80. data/spec/lib/dump_rake/env_spec.rb +139 -0
  81. data/spec/lib/dump_rake/rails_root_spec.rb +45 -0
  82. data/spec/lib/dump_rake/table_manipulation_spec.rb +256 -0
  83. data/spec/lib/dump_rake_spec.rb +326 -0
  84. data/spec/recipes/dump_spec.rb +553 -0
  85. data/spec/spec.opts +4 -0
  86. data/spec/spec_helper.rb +34 -0
  87. data/spec/tasks/assets_spec.rb +92 -0
  88. data/spec/tasks/dump_spec.rb +107 -0
  89. metadata +272 -0
@@ -0,0 +1,13 @@
1
+ Autotest.add_hook :reset do |at|
2
+ at.clear_mappings
3
+ at.add_mapping(%r%^(([^/]+)/.*)\.(?:rb|rake)$%) { |filename, m|
4
+ if m[2] == 'spec'
5
+ filename
6
+ else
7
+ at.files_matching(%r%^spec\/#{Regexp.escape(m[1])}_spec.rb%)
8
+ end
9
+ }
10
+ at.add_mapping(%r%^spec/(spec_helper|shared/.*)\.rb$%) {
11
+ at.files_matching %r%^spec/.*_spec\.rb$%
12
+ }
13
+ end
@@ -0,0 +1,12 @@
1
+ /pkg/
2
+ /*.gem
3
+
4
+ /doc/
5
+ /rdoc/
6
+ /.yardoc/
7
+ /coverage/
8
+
9
+ Makefile
10
+ *.o
11
+ *.bundle
12
+ /tmp/
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008-2012 Ivan Kuchin
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,250 @@
1
+ # DumpRake
2
+
3
+ Rails app rake and capistrano tasks to create and restore dumps of database and assets.
4
+
5
+ Works with rails 2 and rails 3.
6
+
7
+ Requires at least ruby 1.8.7, tested with ruby 1.9.
8
+
9
+ ## Install
10
+
11
+ Put in Gemfile if you are using bundler:
12
+
13
+ gem 'dump'
14
+
15
+ Install as plugin for rails 3 (not recommended):
16
+
17
+ rails plugin install git://github.com/toy/dump.git
18
+
19
+ Install as plugin for rails 2:
20
+
21
+ script/plugin install git://github.com/toy/dump.git
22
+
23
+ ### Capistrano integration
24
+
25
+ To get capistrano tasks in rails 3, put in `config/deploy.rb`:
26
+
27
+ require 'dump/capistrano'
28
+
29
+ ### Assets config
30
+
31
+ `config/assets` holds paths of dirs you want to dump in file:
32
+
33
+ public/audios
34
+ public/flash
35
+ public/images/upload
36
+ public/videos
37
+
38
+ Generate in rails 3:
39
+
40
+ rails generate assets_config
41
+
42
+ Generate in rails 2:
43
+
44
+ script/generate assets_config
45
+
46
+ ## Capistrano integration
47
+
48
+ When using cap tasks, dump folder should be in persistent place and be linked to application folder, or you will lose all dumps every deploy. Default recipe creates link on after `deploy:update_code`.
49
+
50
+ You can use cap dump:* tasks to control dumps on remote server. Don't forget to deploy application to remote server before using dump:remote tasks.
51
+ Also you can set custom remote rake binary in your deploy.rb like:
52
+
53
+ set :rake, "/custom/rake"
54
+
55
+ ## Usage
56
+
57
+ # create dump
58
+ rake dump
59
+ rake dump:create
60
+
61
+ # list avaliable dumps
62
+ rake dump:versions
63
+
64
+ # restore dump
65
+ rake dump:restore
66
+
67
+ # delete old and unfinished dumps (all non tgz files will be deleted if they are not locked)
68
+ rake dump:cleanup
69
+
70
+ ### Environment variables
71
+
72
+ #### While creating dumps:
73
+
74
+ `DESC`, `DESCRIPTION` — free form description of dump
75
+
76
+ rake dump DESC='uploaded photos'
77
+
78
+ `TAGS`, `TAG` — comma separated list of tags
79
+
80
+ rake dump TAGS='photos,videos'
81
+
82
+ `ASSETS` — comma or colon separated list of paths or globs to dump
83
+
84
+ rake dump ASSETS='public/system:public/images/masks/*'
85
+ rake dump ASSETS='public/system,public/images/masks/*'
86
+
87
+ `TABLES` — comma separated list of tables to dump or if prefixed by "-" — to skip; by default only sessions table is skipped; schema_info and schema_migrations are always included if they are present
88
+
89
+ dump all tables except sessions:
90
+
91
+ rake dump
92
+
93
+ dump all tables:
94
+
95
+ rake dump TABLES='-'
96
+
97
+ dump only people, pages and photos tables:
98
+
99
+ rake dump TABLES='people,pages,photos'
100
+
101
+ dump all tables except people and pages:
102
+
103
+ rake dump TABLES='-people,pages'
104
+
105
+ #### While restoring dumps:
106
+
107
+ `LIKE`, `VER`, `VERSION` — filter dumps by full dump name
108
+
109
+ rake dump:versions LIKE='2009'
110
+ rake dump:restore LIKE='2009' # restores last dump matching 2009
111
+
112
+ `TAGS`, `TAG` — comma separated list of tags
113
+ without '+' or '-' — dump should have any of such tags
114
+ prefixed with '+' — dump should have every tag with prefix
115
+ prefixed with '-' — dump should not have any of tags with prefix
116
+
117
+ select dumps with tags photos or videos:
118
+
119
+ rake dump:restore TAGS='photos,videos'
120
+
121
+ select dumps with tags photos and videos:
122
+
123
+ rake dump:restore TAGS='+photos,+videos'
124
+
125
+ skip dumps with tags mirror and archive:
126
+
127
+ rake dump:restore TAGS='-mirror,-archive'
128
+
129
+ select dumps with tags photos or videos, with tag important and without mirror:
130
+
131
+ rake dump:restore TAGS='photos,videos,+important,-mirror'
132
+
133
+ `MIGRATE_DOWN` — don't run down for migrations not present in dump if you pass "0", "no" or "false"; pass "reset" to recreate (drop and create) db
134
+ by default all migrations which are not present in dump are ran down
135
+
136
+ don't run down for migrations:
137
+
138
+ rake dump:restore MIGRATE_DOWN=no
139
+
140
+ reset database:
141
+
142
+ rake dump:restore MIGRATE_DOWN=reset
143
+
144
+ `RESTORE_SCHEMA` — don't read/change schema if you pass "0", "no" or "false" (useful to just restore data for table; note that schema info tables are also not restored)
145
+
146
+ don't restore schema:
147
+
148
+ rake dump:restore RESTORE_SCHEMA=no
149
+
150
+ `RESTORE_TABLES` — works as TABLES, but for restoring
151
+
152
+ restores only people, pages and photos tables:
153
+
154
+ rake dump RESTORE_TABLES='people,pages,photos'
155
+
156
+ restores all tables except people and pages:
157
+
158
+ rake dump TABLES='-people,pages'
159
+
160
+ `RESTORE_ASSETS` — works as ASSETS, but for restoring
161
+
162
+ rake dump RESTORE_ASSETS='public/system/a,public/system/b'
163
+ rake dump RESTORE_ASSETS='public/system/a:public/images/masks/*/new*'
164
+
165
+ #### For listing dumps:
166
+
167
+ `LIKE`, `VER`, `VERSION` and `TAG`, `TAGS` as for restoring
168
+
169
+ `SUMMARY` — output info about dump: "1", "true" or "yes" for basic info, "2" or "schema" to display schema as well
170
+
171
+ rake dump:versions SUMMARY=1
172
+ rake dump:versions SUMMARY=full # output schema too
173
+
174
+ #### For cleanup:
175
+
176
+ `LIKE`, `VER`, `VERSION` and `TAG`, `TAGS` as for restoring
177
+
178
+ `LEAVE` — number of dumps to leave
179
+
180
+ rake dump:cleanup LEAVE=10
181
+ rake dump:cleanup LEAVE=none
182
+
183
+ ### cap tasks
184
+
185
+ For all cap commands environment variables are same as for rake tasks
186
+
187
+ `TRANSFER_VIA` — transfer method (rsync, sftp or scp)
188
+ By default transferring task will try to transfer using rsync if it is present, else it will try to use sftp and scp
189
+
190
+ force transfer using scp:
191
+
192
+ cap dump:remote:download TRANSFER_VIA=scp
193
+ cap dump:mirror:down TRANSFER_VIA=scp
194
+
195
+ `BACKUP`, `AUTOBACKUP`, `AUTO_BACKUP` — no autobackup if you pass "0", "no" or "false"
196
+ by default backup is created before mirroring
197
+
198
+ don't create backup:
199
+
200
+ cap dump:mirror:down BACKUP=0
201
+ cap dump:mirror:down AUTOBACKUP=no
202
+ cap dump:mirror:down AUTO_BACKUP=false
203
+
204
+ #### Basic cap tasks are same as rake tasks
205
+
206
+ cap dump:local
207
+ cap dump:local:create
208
+ cap dump:local:restore
209
+ cap dump:local:versions
210
+ cap dump:local:cleanup
211
+
212
+ cap dump:remote
213
+ cap dump:remote:create
214
+ cap dump:remote:restore
215
+ cap dump:remote:versions
216
+ cap dump:remote:cleanup
217
+
218
+ #### Dump exchanging tasks
219
+
220
+ transfer selected dump to remote server:
221
+
222
+ cap dump:local:upload
223
+
224
+ transfer selected dump to local:
225
+
226
+ cap dump:remote:download
227
+
228
+ #### Mirroring tasks
229
+
230
+ mirror local to remote (create local dump, upload it to remote and restore it there):
231
+
232
+ cap dump:mirror:up
233
+
234
+ mirror remote to local (create remote dump, download it from remote and restore on local):
235
+
236
+ cap dump:mirror:down
237
+
238
+ #### Backuping tasks
239
+
240
+ backup remote on local (create remote dump and download it):
241
+
242
+ cap dump:backup:create
243
+
244
+ restore backup (upload dump and restore on remote):
245
+
246
+ cap dump:backup:restore
247
+
248
+ ## Copyright
249
+
250
+ Copyright (c) 2008-2012 Ivan Kuchin. See LICENSE.txt for details.
@@ -0,0 +1,22 @@
1
+ # encoding: UTF-8
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'dump'
5
+ s.version = '1.0.0'
6
+ s.summary = %q{Rails app rake and capistrano tasks to create and restore dumps of database and assets}
7
+ s.homepage = "http://github.com/toy/#{s.name}"
8
+ s.authors = ['Ivan Kuchin']
9
+ s.license = 'MIT'
10
+
11
+ s.rubyforge_project = s.name
12
+
13
+ s.files = `git ls-files`.split("\n")
14
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
16
+ s.require_paths = %w[lib]
17
+
18
+ s.add_dependency 'archive-tar-minitar', '= 0.5.2'
19
+ s.add_dependency 'progress', '~> 2.4.0'
20
+ s.add_development_dependency 'rspec'
21
+ s.add_development_dependency 'sqlite3'
22
+ end
@@ -0,0 +1,3 @@
1
+ module Dump
2
+ require 'dump/railtie' if defined?(Rails)
3
+ end
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), '../../recipes/dump.rb')
@@ -0,0 +1,8 @@
1
+ module Dump
2
+ class Railtie < Rails::Railtie
3
+ rake_tasks do
4
+ load 'tasks/assets.rake'
5
+ load 'tasks/dump.rake'
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,85 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'pathname'
4
+ require 'find'
5
+ require 'fileutils'
6
+ require 'zlib'
7
+ require 'tempfile'
8
+
9
+ require 'rake'
10
+ require 'archive/tar/minitar'
11
+ require 'dump_rake/archive_tar_minitar_fix'
12
+ require 'progress'
13
+
14
+ class DumpRake
15
+ class << self
16
+ def versions(options = {})
17
+ Dump.list(options).each do |dump|
18
+ puts DumpRake::Env[:show_size] || $stdout.tty? ? "#{dump.human_size.to_s.rjust(7)}\t#{dump}" : dump
19
+ begin
20
+ case options[:summary].to_s.downcase[0, 1]
21
+ when *%w[1 t y]
22
+ puts DumpReader.summary(dump.path)
23
+ puts
24
+ when *%w[2 s]
25
+ puts DumpReader.summary(dump.path, :schema => true)
26
+ puts
27
+ end
28
+ rescue => e
29
+ $stderr.puts "Error reading dump: #{e}"
30
+ $stderr.puts
31
+ end
32
+ end
33
+ end
34
+
35
+ def create(options = {})
36
+ dump = Dump.new(options.merge(:dir => File.join(DumpRake::RailsRoot, 'dump')))
37
+
38
+ DumpWriter.create(dump.tmp_path)
39
+
40
+ File.rename(dump.tmp_path, dump.tgz_path)
41
+ puts File.basename(dump.tgz_path)
42
+ end
43
+
44
+ def restore(options = {})
45
+ dump = Dump.list(options).last
46
+
47
+ if dump
48
+ DumpReader.restore(dump.path)
49
+ else
50
+ $stderr.puts "Avaliable versions:"
51
+ $stderr.puts Dump.list
52
+ end
53
+ end
54
+
55
+ def cleanup(options = {})
56
+ unless options[:leave].nil? || /^\d+$/ === options[:leave] || options[:leave].downcase == 'none'
57
+ raise 'LEAVE should be number or "none"'
58
+ end
59
+
60
+ to_delete = []
61
+
62
+ all_dumps = Dump.list(options.merge(:all => true))
63
+ to_delete.concat(all_dumps.select{ |dump| dump.ext != 'tgz' })
64
+
65
+ dumps = Dump.list(options)
66
+ leave = (options[:leave] || 5).to_i
67
+ to_delete.concat(dumps[0, dumps.length - leave]) if dumps.length > leave
68
+
69
+ to_delete.each do |dump|
70
+ dump.lock do
71
+ begin
72
+ dump.path.unlink
73
+ puts "Deleted #{dump.path}"
74
+ rescue => e
75
+ $stderr.puts "Can not delete #{dump.path} — #{e}"
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
82
+
83
+ %w[rails_root assets table_manipulation dump dump_reader dump_writer env].each do |file|
84
+ require "dump_rake/#{file}"
85
+ end
@@ -0,0 +1,8 @@
1
+ class Archive::Tar::Minitar::Reader::EntryStream
2
+ def getbyte
3
+ return nil if @read >= @size
4
+ ret = @io.getbyte
5
+ @read += 1 if ret
6
+ ret
7
+ end
8
+ end
@@ -0,0 +1,22 @@
1
+ # encoding: UTF-8
2
+
3
+ class DumpRake
4
+ module Assets
5
+ SPLITTER = /[:,]/
6
+
7
+ class << self
8
+ def assets
9
+ File.readlines(File.join(DumpRake::RailsRoot, 'config/assets')).map(&:strip).grep(/^[^#]/).join(':')
10
+ end
11
+
12
+ def glob_asset_children(asset, glob)
13
+ path = File.expand_path(asset, DumpRake::RailsRoot)
14
+ if path[0, DumpRake::RailsRoot.length] == DumpRake::RailsRoot # asset must be in rails root
15
+ Dir[File.join(path, glob)]
16
+ else
17
+ []
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end