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 task for pulling down remote db without loading locally #94

Open
wants to merge 2 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 || db:backup # Create a database backup using remote database data

Example
=======
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 @@ -215,6 +215,18 @@ 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 remote_backup(instance)
remote_db = Database::Remote.new(instance)
remote_db.dump.download

file = remote_db.output_file
unzip_file = File.join(File.dirname(file), File.basename(file, '.bz2'))
backup_dir = instance.fetch(:db_backup_dir)

instance.logger.info("Unzipping database backup and storing in #{backup_dir}")
system("bunzip2 -f #{file} && mkdir -p #{backup_dir} && mv #{unzip_file} #{backup_dir}")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use compressor.decompress(file) like here.

end
end

end
12 changes: 12 additions & 0 deletions lib/capistrano-db-tasks/dbtasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
set :skip_data_sync_confirm, (ENV['SKIP_DATA_SYNC_CONFIRM'].to_s.downcase == 'true')
set :disallow_pushing, false unless fetch(:disallow_pushing)
set :compressor, :gzip unless fetch(:compressor)
set :db_backup_dir, 'db/backups' unless fetch(:db_backup_dir)

namespace :capistrano_db_tasks do
task :check_can_push do
Expand All @@ -30,6 +31,14 @@
end
end
end

desc 'Create a database backup using remote database data'
task :backup do
on roles(:db) do
Database.remote_backup(instance)
end
end
end
end

namespace :local do
Expand All @@ -47,6 +56,9 @@
desc 'Synchronize your local database using remote database data'
task :pull => "db:local:sync"

desc 'Create a database backup using remote database data'
task :backup => "db:remote:backup"

desc 'Synchronize your remote database using local database data'
task :push => "db:remote:sync"
end
Expand Down