Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add backup/restore_latest task #87

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ Available tasks

db:local:sync || db:pull # Synchronize your local database using remote database data
db:remote:sync || db:push # Synchronize your remote database using local database data
db:remote:backup # Dumps database to db folder after that we can take it from there

Example
=======
Expand Down
2 changes: 1 addition & 1 deletion lib/capistrano-db-tasks/compressors/bzip2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def decompress(from, to = nil)
"-c --stdout > #{to}"
end

"bunzip2 -f #{from} #{to}"
"bunzip2 -k -f #{from} #{to}"
end

end
Expand Down
12 changes: 12 additions & 0 deletions lib/capistrano-db-tasks/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ def initialize(cap_instance)

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

Expand Down Expand Up @@ -229,5 +230,16 @@ def local_to_remote(instance)
remote_db.load(local_db.output_file, instance.fetch(:db_local_clean))
File.unlink(local_db.output_file) if instance.fetch(:db_local_clean)
end

def backup(instance)
remote_db = Database::Remote.new(instance)
remote_db.dump
remote_db.download
end

def restore_latest(instance)
remote_db = Database::Remote.new(instance)
remote_db.load("#{instance.current_path}/db/latest.sql.#{remote_db.compressor.file_extension}", true)
end
end
end
14 changes: 14 additions & 0 deletions lib/capistrano-db-tasks/dbtasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@
end
end
end

desc 'Dumps database to db folder after that we can take it from there'
task :backup do
on roles(:db) do
Database.backup(self)
end
end

desc 'Restores the latest database dump from the remote folder (pairs with db:remote:backup)'
task :restore_latest do
on roles(:db) do
Database.restore_latest(self)
end
end
end

namespace :local do
Expand Down