Skip to content

Commit

Permalink
Backup database tables in a pre defined order.
Browse files Browse the repository at this point in the history
Signed-off-by: Yorick Peterse <yorickpeterse@gmail.com>
  • Loading branch information
Yorick Peterse committed Jan 2, 2012
1 parent bcd6485 commit 795b1c3
Showing 1 changed file with 38 additions and 11 deletions.
49 changes: 38 additions & 11 deletions task/backup.rake
@@ -1,23 +1,50 @@
namespace :db do
table_order = [
:settings,
:user_statuses,
:users,
:user_groups,
:user_groups_users,
:permissions,
:sections,
:section_entry_statuses,
:section_entries,
:comment_statuses,
:comments,
:category_groups,
:categories,
:category_groups_sections,
:custom_field_methods,
:custom_field_types,
:custom_field_groups,
:custom_fields,
:custom_field_values,
:custom_field_groups_sections,
:menus,
:menu_items,
]

desc 'Creates a database independent backup'
task :export do
data = {}

Zen.database.tables.each do |table|
handle = File.open(
File.expand_path('../../tmp/%s' % table, __FILE__),
'w'
)

handle.write(Marshal.dump([table, Zen.database[table].all]))
handle.close
table_order.each do |table|
data[table] = Zen.database[table].all
end

handle = File.open(File.expand_path('../../tmp/backup', __FILE__), 'w')

handle.write(Marshal.dump(data))
handle.close
end

desc 'Imports a database backup'
task :import, :backup do |task, args|
table, rows = Marshal.load(File.open(args[:backup], 'r').read)
data = Marshal.load(File.open(args[:backup], 'r').read)

Zen.database[table].delete
Zen.database[table].insert_multiple(rows)
table_order.each do |table|
Zen.database[table].delete
Zen.database[table].insert_multiple(data[table])
end
end
end

0 comments on commit 795b1c3

Please sign in to comment.