Skip to content

Commit b8beb1c

Browse files
committed
Add db:remote:restore_latest task
1 parent 1450d5f commit b8beb1c

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

lib/capistrano-db-tasks/compressors/bzip2.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def decompress(from, to = nil)
3232
"-c --stdout > #{to}"
3333
end
3434

35-
"bunzip2 -f #{from} #{to}"
35+
"bunzip2 -k -f #{from} #{to}"
3636
end
3737

3838
end

lib/capistrano-db-tasks/database.rb

+6
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ def initialize(cap_instance)
112112

113113
def dump
114114
@cap.execute "cd #{@cap.current_path} && #{dump_cmd} | #{compressor.compress('-', output_file)}"
115+
@cap.execute("ln -fs #{@cap.current_path}/#{output_file} #{@cap.current_path}/db/latest.sql.#{compressor.file_extension}")
115116
self
116117
end
117118

@@ -212,6 +213,11 @@ def backup(instance)
212213
remote_db = Database::Remote.new(instance)
213214
remote_db.dump
214215
end
216+
217+
def restore_latest(instance)
218+
remote_db = Database::Remote.new(instance)
219+
remote_db.load("#{instance.current_path}/db/latest.sql.#{remote_db.compressor.file_extension}", true)
220+
end
215221
end
216222

217223
end

lib/capistrano-db-tasks/dbtasks.rb

+7
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@
3737
Database.backup(self)
3838
end
3939
end
40+
41+
desc 'Restores the latest database dump from the remote folder (pairs with db:remote:backup)'
42+
task :restore_latest do
43+
on roles(:db) do
44+
Database.restore_latest(self)
45+
end
46+
end
4047
end
4148

4249
namespace :local do

0 commit comments

Comments
 (0)