Last active
September 18, 2015 11:06
-
-
Save stevelacey/bb6c628dd37a221157ad to your computer and use it in GitHub Desktop.
capistrano postgres db push pull
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config valid only for Capistrano 3.1 | |
lock '3.1.0' | |
namespace :db do | |
desc "Push database" | |
task :push do | |
run_locally do | |
app_server = roles(:app).first | |
db_server = 'db.server.example.com' | |
user = 'simpleweb' | |
raise "No pushing your dev db over production" if env == :production | |
local = YAML.load(IO.read("config/database.yml"))["development"] | |
remote = YAML.load(capture("ssh -C #{user}@#{app_server} 'cat #{shared_path}/config/database.yml'"))[fetch(:rails_env).to_s] | |
execute " | |
pg_dump | |
--host localhost | |
--username #{local["username"]} | |
--create | |
--format c | |
--no-owner | |
#{local["database"]} | |
| | |
ssh -C #{user}@#{db_server} ' | |
PGPASSWORD=#{remote["password"]} | |
pg_restore | |
--host localhost | |
--username #{remote["username"]} | |
--dbname #{remote["database"]} | |
--no-owner | |
' | |
" | |
.gsub(/\n/, '') | |
.squeeze(' ') | |
end | |
end | |
desc "Pull database" | |
task :pull do | |
run_locally do | |
app_server = roles(:app).first | |
db_server = 'db.server.example.com' | |
user = 'simpleweb' | |
local = YAML.load(IO.read("config/database.yml"))["development"] | |
remote = YAML.load(capture("ssh -C #{user}@#{app_server} 'cat #{shared_path}/config/database.yml'"))[fetch(:rails_env).to_s] | |
execute " | |
ssh -C #{user}@#{db_server} ' | |
PGPASSWORD=#{remote["password"]} | |
pg_dump | |
--host localhost | |
--username #{remote["username"]} | |
--create | |
--format c | |
--no-owner | |
#{remote["database"]} | |
' | |
| | |
pg_restore | |
--username #{local["username"]} | |
--dbname #{local["database"]} | |
--clean | |
--no-owner | |
" | |
.gsub(/\n/, '') | |
.squeeze(' ') | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment