rails-backup-migrate 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in rails-backup-migrate.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,284 @@
1
+ THIS LICENSE MUST ACCOMPANY THIS APPLICATION AT ALL TIMES.
2
+
3
+ License follows:
4
+
5
+ GNU GENERAL PUBLIC LICENSE
6
+ Version 2, June 1991
7
+
8
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc.
9
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
10
+ Everyone is permitted to copy and distribute verbatim copies
11
+ of this license document, but changing it is not allowed.
12
+
13
+ Preamble
14
+
15
+ The licenses for most software are designed to take away your
16
+ freedom to share and change it. By contrast, the GNU General Public
17
+ License is intended to guarantee your freedom to share and change free
18
+ software--to make sure the software is free for all its users. This
19
+ General Public License applies to most of the Free Software
20
+ Foundation's software and to any other program whose authors commit to
21
+ using it. (Some other Free Software Foundation software is covered by
22
+ the GNU Lesser General Public License instead.) You can apply it to
23
+ your programs, too.
24
+
25
+ When we speak of free software, we are referring to freedom, not
26
+ price. Our General Public Licenses are designed to make sure that you
27
+ have the freedom to distribute copies of free software (and charge for
28
+ this service if you wish), that you receive source code or can get it
29
+ if you want it, that you can change the software or use pieces of it
30
+ in new free programs; and that you know you can do these things.
31
+
32
+ To protect your rights, we need to make restrictions that forbid
33
+ anyone to deny you these rights or to ask you to surrender the rights.
34
+ These restrictions translate to certain responsibilities for you if you
35
+ distribute copies of the software, or if you modify it.
36
+
37
+ For example, if you distribute copies of such a program, whether
38
+ gratis or for a fee, you must give the recipients all the rights that
39
+ you have. You must make sure that they, too, receive or can get the
40
+ source code. And you must show them these terms so they know their
41
+ rights.
42
+
43
+ We protect your rights with two steps: (1) copyright the software, and
44
+ (2) offer you this license which gives you legal permission to copy,
45
+ distribute and/or modify the software.
46
+
47
+ Also, for each author's protection and ours, we want to make certain
48
+ that everyone understands that there is no warranty for this free
49
+ software. If the software is modified by someone else and passed on, we
50
+ want its recipients to know that what they have is not the original, so
51
+ that any problems introduced by others will not reflect on the original
52
+ authors' reputations.
53
+
54
+ Finally, any free program is threatened constantly by software
55
+ patents. We wish to avoid the danger that redistributors of a free
56
+ program will individually obtain patent licenses, in effect making the
57
+ program proprietary. To prevent this, we have made it clear that any
58
+ patent must be licensed for everyone's free use or not licensed at all.
59
+
60
+ The precise terms and conditions for copying, distribution and
61
+ modification follow.
62
+
63
+ GNU GENERAL PUBLIC LICENSE
64
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
65
+
66
+ 0. This License applies to any program or other work which contains
67
+ a notice placed by the copyright holder saying it may be distributed
68
+ under the terms of this General Public License. The "Program", below,
69
+ refers to any such program or work, and a "work based on the Program"
70
+ means either the Program or any derivative work under copyright law:
71
+ that is to say, a work containing the Program or a portion of it,
72
+ either verbatim or with modifications and/or translated into another
73
+ language. (Hereinafter, translation is included without limitation in
74
+ the term "modification".) Each licensee is addressed as "you".
75
+
76
+ Activities other than copying, distribution and modification are not
77
+ covered by this License; they are outside its scope. The act of
78
+ running the Program is not restricted, and the output from the Program
79
+ is covered only if its contents constitute a work based on the
80
+ Program (independent of having been made by running the Program).
81
+ Whether that is true depends on what the Program does.
82
+
83
+ 1. You may copy and distribute verbatim copies of the Program's
84
+ source code as you receive it, in any medium, provided that you
85
+ conspicuously and appropriately publish on each copy an appropriate
86
+ copyright notice and disclaimer of warranty; keep intact all the
87
+ notices that refer to this License and to the absence of any warranty;
88
+ and give any other recipients of the Program a copy of this License
89
+ along with the Program.
90
+
91
+ You may charge a fee for the physical act of transferring a copy, and
92
+ you may at your option offer warranty protection in exchange for a fee.
93
+
94
+ 2. You may modify your copy or copies of the Program or any portion
95
+ of it, thus forming a work based on the Program, and copy and
96
+ distribute such modifications or work under the terms of Section 1
97
+ above, provided that you also meet all of these conditions:
98
+
99
+ a) You must cause the modified files to carry prominent notices
100
+ stating that you changed the files and the date of any change.
101
+
102
+ b) You must cause any work that you distribute or publish, that in
103
+ whole or in part contains or is derived from the Program or any
104
+ part thereof, to be licensed as a whole at no charge to all third
105
+ parties under the terms of this License.
106
+
107
+ c) If the modified program normally reads commands interactively
108
+ when run, you must cause it, when started running for such
109
+ interactive use in the most ordinary way, to print or display an
110
+ announcement including an appropriate copyright notice and a
111
+ notice that there is no warranty (or else, saying that you provide
112
+ a warranty) and that users may redistribute the program under
113
+ these conditions, and telling the user how to view a copy of this
114
+ License. (Exception: if the Program itself is interactive but
115
+ does not normally print such an announcement, your work based on
116
+ the Program is not required to print an announcement.)
117
+
118
+ These requirements apply to the modified work as a whole. If
119
+ identifiable sections of that work are not derived from the Program,
120
+ and can be reasonably considered independent and separate works in
121
+ themselves, then this License, and its terms, do not apply to those
122
+ sections when you distribute them as separate works. But when you
123
+ distribute the same sections as part of a whole which is a work based
124
+ on the Program, the distribution of the whole must be on the terms of
125
+ this License, whose permissions for other licensees extend to the
126
+ entire whole, and thus to each and every part regardless of who wrote it.
127
+
128
+ Thus, it is not the intent of this section to claim rights or contest
129
+ your rights to work written entirely by you; rather, the intent is to
130
+ exercise the right to control the distribution of derivative or
131
+ collective works based on the Program.
132
+
133
+ In addition, mere aggregation of another work not based on the Program
134
+ with the Program (or with a work based on the Program) on a volume of
135
+ a storage or distribution medium does not bring the other work under
136
+ the scope of this License.
137
+
138
+ 3. You may copy and distribute the Program (or a work based on it,
139
+ under Section 2) in object code or executable form under the terms of
140
+ Sections 1 and 2 above provided that you also do one of the following:
141
+
142
+ a) Accompany it with the complete corresponding machine-readable
143
+ source code, which must be distributed under the terms of Sections
144
+ 1 and 2 above on a medium customarily used for software interchange; or,
145
+
146
+ b) Accompany it with a written offer, valid for at least three
147
+ years, to give any third party, for a charge no more than your
148
+ cost of physically performing source distribution, a complete
149
+ machine-readable copy of the corresponding source code, to be
150
+ distributed under the terms of Sections 1 and 2 above on a medium
151
+ customarily used for software interchange; or,
152
+
153
+ c) Accompany it with the information you received as to the offer
154
+ to distribute corresponding source code. (This alternative is
155
+ allowed only for noncommercial distribution and only if you
156
+ received the program in object code or executable form with such
157
+ an offer, in accord with Subsection b above.)
158
+
159
+ The source code for a work means the preferred form of the work for
160
+ making modifications to it. For an executable work, complete source
161
+ code means all the source code for all modules it contains, plus any
162
+ associated interface definition files, plus the scripts used to
163
+ control compilation and installation of the executable. However, as a
164
+ special exception, the source code distributed need not include
165
+ anything that is normally distributed (in either source or binary
166
+ form) with the major components (compiler, kernel, and so on) of the
167
+ operating system on which the executable runs, unless that component
168
+ itself accompanies the executable.
169
+
170
+ If distribution of executable or object code is made by offering
171
+ access to copy from a designated place, then offering equivalent
172
+ access to copy the source code from the same place counts as
173
+ distribution of the source code, even though third parties are not
174
+ compelled to copy the source along with the object code.
175
+
176
+ 4. You may not copy, modify, sublicense, or distribute the Program
177
+ except as expressly provided under this License. Any attempt
178
+ otherwise to copy, modify, sublicense or distribute the Program is
179
+ void, and will automatically terminate your rights under this License.
180
+ However, parties who have received copies, or rights, from you under
181
+ this License will not have their licenses terminated so long as such
182
+ parties remain in full compliance.
183
+
184
+ 5. You are not required to accept this License, since you have not
185
+ signed it. However, nothing else grants you permission to modify or
186
+ distribute the Program or its derivative works. These actions are
187
+ prohibited by law if you do not accept this License. Therefore, by
188
+ modifying or distributing the Program (or any work based on the
189
+ Program), you indicate your acceptance of this License to do so, and
190
+ all its terms and conditions for copying, distributing or modifying
191
+ the Program or works based on it.
192
+
193
+ 6. Each time you redistribute the Program (or any work based on the
194
+ Program), the recipient automatically receives a license from the
195
+ original licensor to copy, distribute or modify the Program subject to
196
+ these terms and conditions. You may not impose any further
197
+ restrictions on the recipients' exercise of the rights granted herein.
198
+ You are not responsible for enforcing compliance by third parties to
199
+ this License.
200
+
201
+ 7. If, as a consequence of a court judgment or allegation of patent
202
+ infringement or for any other reason (not limited to patent issues),
203
+ conditions are imposed on you (whether by court order, agreement or
204
+ otherwise) that contradict the conditions of this License, they do not
205
+ excuse you from the conditions of this License. If you cannot
206
+ distribute so as to satisfy simultaneously your obligations under this
207
+ License and any other pertinent obligations, then as a consequence you
208
+ may not distribute the Program at all. For example, if a patent
209
+ license would not permit royalty-free redistribution of the Program by
210
+ all those who receive copies directly or indirectly through you, then
211
+ the only way you could satisfy both it and this License would be to
212
+ refrain entirely from distribution of the Program.
213
+
214
+ If any portion of this section is held invalid or unenforceable under
215
+ any particular circumstance, the balance of the section is intended to
216
+ apply and the section as a whole is intended to apply in other
217
+ circumstances.
218
+
219
+ It is not the purpose of this section to induce you to infringe any
220
+ patents or other property right claims or to contest validity of any
221
+ such claims; this section has the sole purpose of protecting the
222
+ integrity of the free software distribution system, which is
223
+ implemented by public license practices. Many people have made
224
+ generous contributions to the wide range of software distributed
225
+ through that system in reliance on consistent application of that
226
+ system; it is up to the author/donor to decide if he or she is willing
227
+ to distribute software through any other system and a licensee cannot
228
+ impose that choice.
229
+
230
+ This section is intended to make thoroughly clear what is believed to
231
+ be a consequence of the rest of this License.
232
+
233
+ 8. If the distribution and/or use of the Program is restricted in
234
+ certain countries either by patents or by copyrighted interfaces, the
235
+ original copyright holder who places the Program under this License
236
+ may add an explicit geographical distribution limitation excluding
237
+ those countries, so that distribution is permitted only in or among
238
+ countries not thus excluded. In such case, this License incorporates
239
+ the limitation as if written in the body of this License.
240
+
241
+ 9. The Free Software Foundation may publish revised and/or new versions
242
+ of the General Public License from time to time. Such new versions will
243
+ be similar in spirit to the present version, but may differ in detail to
244
+ address new problems or concerns.
245
+
246
+ Each version is given a distinguishing version number. If the Program
247
+ specifies a version number of this License which applies to it and "any
248
+ later version", you have the option of following the terms and conditions
249
+ either of that version or of any later version published by the Free
250
+ Software Foundation. If the Program does not specify a version number of
251
+ this License, you may choose any version ever published by the Free Software
252
+ Foundation.
253
+
254
+ 10. If you wish to incorporate parts of the Program into other free
255
+ programs whose distribution conditions are different, write to the author
256
+ to ask for permission. For software which is copyrighted by the Free
257
+ Software Foundation, write to the Free Software Foundation; we sometimes
258
+ make exceptions for this. Our decision will be guided by the two goals
259
+ of preserving the free status of all derivatives of our free software and
260
+ of promoting the sharing and reuse of software generally.
261
+
262
+ NO WARRANTY
263
+
264
+ 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
265
+ FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
266
+ OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
267
+ PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
268
+ OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
269
+ MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
270
+ TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
271
+ PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
272
+ REPAIR OR CORRECTION.
273
+
274
+ 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
275
+ WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
276
+ REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
277
+ INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
278
+ OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
279
+ TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
280
+ YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
281
+ PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
282
+ POSSIBILITY OF SUCH DAMAGES.
283
+
284
+ END OF TERMS AND CONDITIONS
data/README.textile ADDED
@@ -0,0 +1,42 @@
1
+ h1. Rails Backup Migrate
2
+
3
+ A gem that provides a rake task for backing up your database and uploaded files to an archive and restoring them from an archive.
4
+
5
+ You can archive schema, schema+data, files, or schema+data+files. Each archive action has a restore counterpart. The data from the database is backed up to a .yml file for each table that is database independent.
6
+
7
+ So you can use Rails Backup Migrate to get data out of one kind of database and easily load it into another kind.
8
+
9
+ h2. Quick Start
10
+
11
+ Installing the gem:
12
+
13
+ <pre><code> $ gem install rails-backup-migrate</code></pre>
14
+
15
+ Add the following line to your rails application's Rakefile
16
+ <pre><code> require 'rails-backup-migrate'</code></pre>
17
+
18
+ To backup everything in a rails instance:
19
+ <pre><code> $ rake site:backup</code></pre>
20
+
21
+ Everything is saved in an archive file, named by default 'site-backup.tgz'. Copy/move this to another rails instance
22
+ <pre><code> $ rake site:restore</code></pre>
23
+
24
+ h2. More info
25
+
26
+ The backup file can be specified with a command line argument in rake's (wierd) format:
27
+
28
+ <pre></code> $ rake site:backup[/path/to/file.tgz]</code></pre>
29
+
30
+ h2. Compatibility
31
+
32
+ Tested initially on 'redmine' using rails 2.3.11 and some simpler tests with rails 3. Has been tested with MySQL and Sqlite3 databases. Further testing with postgres to be done.
33
+
34
+ h2. Contributors
35
+
36
+ Matt Connolly - main author of gem
37
+ Tobias Lütke - wrote a `"backup.rake":http://blog.leetsoft.com/2006/5/29/easy-migration-between-databases` file that inspired the database backup portion.
38
+
39
+ h2. License
40
+
41
+ GPL(General Public License) - See LICENSE
42
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,136 @@
1
+ #
2
+ # @file lib/rails-backup-migrate.rb
3
+ #
4
+ # @author Matt Connolly
5
+ # @copyright 2011 Matt Connolly
6
+ #
7
+ # This file defines the core of the backup functionality as singleton methods in a Module which
8
+ # is used by the rake task.
9
+ #
10
+ # When required by a Rakefile, this file also loads the rake tasks in lib/tasks/rails-backup-migrate.rake
11
+ #
12
+
13
+ require "rails-backup-migrate/version"
14
+ require 'tmpdir'
15
+ require 'fileutils'
16
+
17
+
18
+ module RailsBackupMigrate
19
+ @archive_file = nil
20
+ @files_to_archive = []
21
+
22
+ if ENV['verbose'] || ENV['VERBOSE']
23
+ VERBOSE = true
24
+ else
25
+ VERBOSE = false
26
+ end
27
+
28
+ # singleton methods for Module `RailsBackupMigrate`
29
+ class << self
30
+ attr_accessor :backup_file
31
+
32
+ # list the tables we should backup, excluding ones we can ignore
33
+ def interesting_tables
34
+ ActiveRecord::Base.connection.tables.sort.reject do |tbl|
35
+ ['schema_migrations', 'sessions', 'public_exceptions'].include?(tbl)
36
+ end
37
+ end
38
+
39
+ # add a path to be archived. Expected path to be relative to RAILS_ROOT. This is where the archive
40
+ # will be created so that uploaded files (the 'files' dir) can be reference in place without needing to be copied.
41
+ def add_to_archive path
42
+ # check it's relative to RAILS_ROOT
43
+ raise "File '#{path}' does not exist" unless File::exist? path
44
+
45
+ if File::expand_path(path).start_with? RAILS_ROOT
46
+ # remove RAILS_ROOT from absolute path
47
+ relative = File::expand_path(path).sub(RAILS_ROOT,'')
48
+ # remove leading slash
49
+ relative.gsub! /^#{File::SEPARATOR}/,''
50
+ # add to list
51
+ @files_to_archive << relative
52
+ end
53
+ end
54
+
55
+ def files_to_archive
56
+ @files_to_archive
57
+ end
58
+
59
+ # get a temp directory to be used for the backup
60
+ # the value is cached so it can be reused throughout the process
61
+ def temp_dir
62
+ unless defined? @temp_dir
63
+ @temp_dir = Dir.mktmpdir
64
+ end
65
+ @temp_dir
66
+ end
67
+
68
+
69
+ # delete any working files
70
+ def clean_up
71
+ puts "cleaning up." if VERBOSE
72
+ FileUtils.rmtree temp_dir
73
+ @temp_dir = nil
74
+ @files_to_delete_on_cleanup ||= []
75
+ @files_to_delete_on_cleanup.each do |f|
76
+ unless File::directory? f
77
+ FileUtils.rm f
78
+ else
79
+ FileUtils.rm_r f
80
+ end
81
+ end
82
+ @files_to_delete_on_cleanup = []
83
+ end
84
+
85
+ # create the archive .tgz file in the requested location
86
+ def create_archive backup_file
87
+ puts "creating archive..." if VERBOSE
88
+ absolute = File::expand_path backup_file
89
+ Dir::chdir RAILS_ROOT
90
+ `tar -czf #{absolute} #{files_to_archive.join ' '}`
91
+ end
92
+
93
+
94
+ # save the required database tables to .yml files in a folder 'yml' and add them to the backup
95
+ def save_db_to_yml
96
+ FileUtils.chdir RAILS_ROOT
97
+ FileUtils.mkdir_p 'db/backup'
98
+ FileUtils.chdir 'db/backup'
99
+
100
+ @files_to_delete_on_cleanup ||= []
101
+
102
+ interesting_tables.each do |tbl|
103
+ puts "Writing #{tbl}..." if VERBOSE
104
+ File.open("#{tbl}.yml", 'w+') { |f| YAML.dump ActiveRecord::Base.connection.select_all("SELECT * FROM #{tbl}"), f }
105
+ @files_to_delete_on_cleanup << File::expand_path("#{tbl}.yml")
106
+ end
107
+
108
+ # simply add the whole yml folder to the archive
109
+ FileUtils.chdir RAILS_ROOT
110
+ add_to_archive 'db/backup'
111
+ end
112
+
113
+
114
+ def restore_db_from_yml
115
+ FileUtils.chdir temp_dir + '/db/backup'
116
+
117
+ interesting_tables.each do |tbl|
118
+
119
+ ActiveRecord::Base.transaction do
120
+
121
+ puts "Loading #{tbl}..." if VERBOSE
122
+ YAML.load_file("#{tbl}.yml").each do |fixture|
123
+ ActiveRecord::Base.connection.execute "INSERT INTO #{tbl} (#{fixture.keys.join(",")}) VALUES (#{fixture.values.collect { |value| ActiveRecord::Base.connection.quote(value) }.join(",")})", 'Fixture Insert'
124
+ end
125
+ end
126
+ end
127
+ end
128
+ end
129
+ end
130
+
131
+ if defined?(Rake)
132
+ rake_files = Dir["#{File.dirname(__FILE__)}/tasks/*.rake"]
133
+ rake_files.each { |file| load file }
134
+ end
135
+
136
+
@@ -0,0 +1,3 @@
1
+ module RailsBackupMigrate
2
+ VERSION = "0.0.5"
3
+ end
@@ -0,0 +1,123 @@
1
+ #
2
+ # @file lib/tasks/rails-backup-migrate.rake
3
+ #
4
+ # @author Matt Connolly
5
+ # @copyright 2011 Matt Connolly
6
+ #
7
+ # this file defines the rake tasks for backing up a rails application (site) including
8
+ # optionally, the database and the files, or both.
9
+ #
10
+
11
+ require 'tmpdir'
12
+
13
+ namespace :site do
14
+ namespace :backup do
15
+
16
+ # private task: save the db/schema.rb file, calls 'db:schema:dump' as a dependency
17
+ task :_save_db_schema, [:backup_file] => [:environment, 'db:schema:dump'] do |t, args|
18
+ puts "adding db/schema.rb to archive list." if RailsBackupMigrate::VERBOSE
19
+ Dir::chdir RAILS_ROOT
20
+ RailsBackupMigrate.add_to_archive 'db/schema.rb'
21
+ end
22
+
23
+ # private task: save the database tables to yml files
24
+ task :_save_db_to_yml, [:backup_file] => [:environment] do |t, args|
25
+ RailsBackupMigrate.save_db_to_yml
26
+ end
27
+
28
+ # private task: finalise the archive, used as a dependency after :_save_db_to_yml and/or :_save_db_schema
29
+ task :_finish_archive, [:backup_file] => [] do |t, args|
30
+ args.with_defaults(:backup_file => "site-backup.tgz")
31
+ RailsBackupMigrate.create_archive args.backup_file
32
+ RailsBackupMigrate.clean_up
33
+ end
34
+
35
+ # private task: add the `files` directory to the archive
36
+ task :_add_files_directory_to_archive, [:backup_file] do |t, args|
37
+ puts "adding 'files' dir to archive list." if RailsBackupMigrate::VERBOSE
38
+ RailsBackupMigrate.add_to_archive "files"
39
+ end
40
+
41
+ desc "Dump schema to a backup file. Default backup = 'site-backup.tgz'."
42
+ task :schema, [:backup_file] => [:_save_db_schema, :_finish_archive] do |t, args|
43
+ end
44
+
45
+ desc "Dump schema and entire db in YML files to a backup file. Default backup = 'site-backup.tgz'"
46
+ task :db, [:backup_file] => [:_save_db_schema, :_save_db_to_yml, :_finish_archive] do |t, args|
47
+ end
48
+
49
+ desc "Archive all files in the `files` directory into a backup file. Default backup = 'site-backup.tgz'"
50
+ task :files, [:backup_file] => [:_add_files_directory_to_archive, :_finish_archive] do |t,args|
51
+ end
52
+
53
+ desc "Backup everything: schema, database to yml, and all files in 'files' directory. Default backup file is 'site-backup.tgz'"
54
+ task :all, [:backup_file] => [:_save_db_schema, :_save_db_to_yml, :_add_files_directory_to_archive, :_finish_archive] do |t, args|
55
+ end
56
+
57
+ end
58
+
59
+
60
+ namespace :restore do
61
+
62
+ # private task: set the backup file to the parameter passed to rake, or the default. Saves the absolute path for later.
63
+ task :_set_backup_file, [:backup_file] => [:environment] do |t, args|
64
+ args.with_defaults(:backup_file => "site-backup.tgz")
65
+
66
+ abort "File does not exist" unless
67
+ File.exist? args.backup_file
68
+
69
+ RailsBackupMigrate.backup_file = File::expand_path args.backup_file
70
+ end
71
+
72
+ # private task: restore db/schema.rb, expected to be a dependency before 'db:schema:load'
73
+ task :_restore_db_schema, [:backup_file] => [:_set_backup_file] do |t, args|[]
74
+ puts "restoring db/schema.rb from archive." if RailsBackupMigrate::VERBOSE
75
+ Dir::chdir RAILS_ROOT
76
+ # extract the schema.rb file in place
77
+ options = RailsBackupMigrate::VERBOSE ? '-xvzf' : '-xzf'
78
+ `tar #{options} #{RailsBackupMigrate.backup_file} db/schema.rb`
79
+ end
80
+
81
+ # private task: restore the database tables from yml files
82
+ task :_restore_db_from_yml, [:backup_file] => [:_restore_db_schema, 'db:schema:load'] do |t, args|
83
+ Dir::chdir RailsBackupMigrate.temp_dir
84
+ # extract the yml files
85
+ options = RailsBackupMigrate::VERBOSE ? '-xvzf' : '-xzf'
86
+ `tar #{options} #{RailsBackupMigrate.backup_file} 'db/backup/*.yml'`
87
+ RailsBackupMigrate.restore_db_from_yml
88
+ end
89
+
90
+ # private task: restore 'files' directory. Should we delete the contents of it?? not for now...
91
+ task :_restore_files_directory, [:backup_file] => [:_set_backup_file] do |t, args|
92
+ Dir::chdir RAILS_ROOT
93
+ # extract the 'files' directory in place
94
+ options = RailsBackupMigrate::VERBOSE ? '-xvzf' : '-xzf'
95
+ `tar #{options} #{RailsBackupMigrate.backup_file} files`
96
+ end
97
+
98
+ desc "Erase and reload db schema from backup file. Default backup file is 'site-backup.tgz'. Runs `rake db:schema:load`."
99
+ task :schema, [:backup_file] => [:_restore_db_schema, 'db:schema:load'] do |t, args|
100
+ RailsBackupMigrate.clean_up
101
+ end
102
+
103
+ desc "Erase and reload entire db schema and data from backup file. Runs `rake db:schema:load`."
104
+ task :db, [:backup_file] => [:_restore_db_from_yml] do |t, args|
105
+ RailsBackupMigrate.clean_up
106
+ end
107
+
108
+ desc "Erase and reload db schema and data from backup filem, and restore all files in the 'files' directory. Default backup file is 'site-backup.tgz'. Runs `rake db:schema:load`."
109
+ task :all, [:backup_file] => [:_set_backup_file, :_restore_db_from_yml, :_restore_files_directory] do |t,args|
110
+ RailsBackupMigrate.clean_up
111
+ end
112
+
113
+ end
114
+
115
+ desc "Backup everything: schema, database to yml, and all files in 'files' directory. Default backup file is 'site-backup.tgz'"
116
+ task :backup, [:backup_file] => 'backup:all' do
117
+ end
118
+
119
+ desc "Erase and reload entire db. Runs `rake db:schema:load`."
120
+ task :restore, [:backup_file] => 'restore:all' do
121
+ end
122
+
123
+ end
@@ -0,0 +1,40 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "rails-backup-migrate/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "rails-backup-migrate"
7
+ s.version = RailsBackupMigrate::VERSION
8
+ s.authors = ["Matt Connolly"]
9
+ s.email = ["matt@soundevolution.com.au"]
10
+ s.homepage = ""
11
+ s.summary = %q{Backup and restore a rails application including database data and files}
12
+ s.description = %q{Creates a directory db/backup in the rails app and creates / loads YML files from there.
13
+ After a backup, the db/backups directory is archived into a .tgz file and then deleted.
14
+ When restoring, the db/backup directory is extracted from the .tgz file.
15
+ All of the files in the 'files' directory are also backed up / restored.
16
+
17
+ The default archive file is "site-backup.tgz" but any other one can be passed as an argument to both db:backup:write
18
+ and db:backup:read, for example:
19
+
20
+ app1$ rake db:backup:write
21
+ app1$ cd ../app2
22
+ app2$ rake db:backup:read[../app1/site-backup.tgz]
23
+
24
+ The environment variable 'verbose' or 'VERBOSE' if defined will result in some verbose output.
25
+
26
+ To add the rake tasks to your Rails app, simply install the gem, and then add the following line to your 'Rakefile':
27
+
28
+ require 'rails-backup-migrate'
29
+
30
+ }
31
+
32
+ s.rubyforge_project = "rails-backup-migrate"
33
+
34
+ s.files = `git ls-files`.split("\n")
35
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
36
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
37
+ s.require_paths = ["lib"]
38
+
39
+ s.add_dependency "activerecord", ">= 2.3"
40
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-backup-migrate
3
+ version: !ruby/object:Gem::Version
4
+ hash: 21
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 5
10
+ version: 0.0.5
11
+ platform: ruby
12
+ authors:
13
+ - Matt Connolly
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-06-29 00:00:00 +10:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: activerecord
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 5
30
+ segments:
31
+ - 2
32
+ - 3
33
+ version: "2.3"
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ description: "Creates a directory db/backup in the rails app and creates / loads YML files from there. \n After a backup, the db/backups directory is archived into a .tgz file and then deleted.\n When restoring, the db/backup directory is extracted from the .tgz file.\n All of the files in the 'files' directory are also backed up / restored.\n \n The default archive file is \"site-backup.tgz\" but any other one can be passed as an argument to both db:backup:write\n and db:backup:read, for example:\n \n app1$ rake db:backup:write\n app1$ cd ../app2\n app2$ rake db:backup:read[../app1/site-backup.tgz]\n \n The environment variable 'verbose' or 'VERBOSE' if defined will result in some verbose output.\n \n To add the rake tasks to your Rails app, simply install the gem, and then add the following line to your 'Rakefile':\n \n require 'rails-backup-migrate'\n \n "
37
+ email:
38
+ - matt@soundevolution.com.au
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ files:
46
+ - .gitignore
47
+ - Gemfile
48
+ - LICENSE
49
+ - README.textile
50
+ - Rakefile
51
+ - lib/rails-backup-migrate.rb
52
+ - lib/rails-backup-migrate/version.rb
53
+ - lib/tasks/rails-backup-migrate.rake
54
+ - rails-backup-migrate.gemspec
55
+ has_rdoc: true
56
+ homepage: ""
57
+ licenses: []
58
+
59
+ post_install_message:
60
+ rdoc_options: []
61
+
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ hash: 3
70
+ segments:
71
+ - 0
72
+ version: "0"
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ hash: 3
79
+ segments:
80
+ - 0
81
+ version: "0"
82
+ requirements: []
83
+
84
+ rubyforge_project: rails-backup-migrate
85
+ rubygems_version: 1.6.2
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: Backup and restore a rails application including database data and files
89
+ test_files: []
90
+