diff --git a/README.markdown b/README.markdown index 0b11a54..29ae58b 100644 --- a/README.markdown +++ b/README.markdown @@ -64,6 +64,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 ======= diff --git a/lib/capistrano-db-tasks/database.rb b/lib/capistrano-db-tasks/database.rb index 3c89fec..75f8a60 100644 --- a/lib/capistrano-db-tasks/database.rb +++ b/lib/capistrano-db-tasks/database.rb @@ -40,7 +40,7 @@ def current_time end def output_file - @output_file ||= "db/#{database}_#{current_time}.sql.bz2" + @output_file ||= "#{@cap.fetch(:db_backup_dir)}/#{database}_#{current_time}.sql.bz2" end def pgpass @@ -53,7 +53,7 @@ def dump_cmd if mysql? "mysqldump #{credentials} #{database} --lock-tables=false" elsif postgresql? - "#{pgpass} pg_dump --no-acl --no-owner #{credentials} #{database}" + "#{pgpass} pg_dump --no-acl --no-owner -Fc #{credentials} #{database}" end end @@ -62,7 +62,7 @@ def import_cmd(file) "mysql #{credentials} -D #{database} < #{file}" elsif postgresql? terminate_connection_sql = "SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = '#{database}' AND pid <> pg_backend_pid();" - "#{pgpass} psql -c \"#{terminate_connection_sql};\" #{credentials}; #{pgpass} dropdb #{credentials} #{database}; #{pgpass} createdb #{credentials} #{database}; #{pgpass} psql #{credentials} -d #{database} < #{file}" + "#{pgpass} psql -c \"#{terminate_connection_sql};\" #{credentials}; #{pgpass} dropdb #{credentials} #{database}; #{pgpass} createdb #{credentials} #{database}; #{pgpass} pg_restore --verbose --clean --no-acl --no-owner #{credentials} -d #{database} #{file}" end end @@ -172,6 +172,11 @@ 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 + end end end diff --git a/lib/capistrano-db-tasks/dbtasks.rb b/lib/capistrano-db-tasks/dbtasks.rb index 9eaf263..5d0c9af 100644 --- a/lib/capistrano-db-tasks/dbtasks.rb +++ b/lib/capistrano-db-tasks/dbtasks.rb @@ -8,6 +8,7 @@ set :assets_dir, 'system' unless fetch(:assets_dir) set :local_assets_dir, 'public' unless fetch(:local_assets_dir) set :skip_data_sync_confirm, (ENV['SKIP_DATA_SYNC_CONFIRM'].to_s.downcase == 'true') +set :db_backup_dir, "db" unless fetch(:db_backup_dir) namespace :db do namespace :remote do @@ -19,6 +20,13 @@ 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 end namespace :local do