2015-11-15 72 views
0

我想:如何將本地數據庫轉儲推入Docker容器?

ssh [email protected] "docker exec -ti mydockercontainerid su postgres -c 'psql mydatabasename'" < mydump.sql 

,但僅執行

ssh [email protected] "docker exec -ti mydockercontainerid su postgres -c 'psql mydatabasename'" 

...並省略< mydump.sql一部分。

psql --list和同事一樣工作。

+2

的[開始在碼頭工人Postgres的容器中,然後用數據加載它(可能的複製http://stackoverflow.com/questions/29600369/starting- a-postgres-container-in-docker-and-then-loading-it-with-data) –

+0

不是,我想通過shell來完成,而不是通過容器。 – shredding

回答

0

這裏的Makefile我最終使用:

# Convenience cli shortcuts 

LOCAL_DOCKER_IP = 192.168.99.100 
STAGING_IP = DOCKER_NODE_IP_HERE 
RELEASE_IP = IP_FROM_THE_SERVER_WITH_THE_ORIGINAL_DB 

DOCKER_DB_ID = $(shell ssh [email protected]$$STAGING_IP "docker ps | grep database" | awk '{print $$1}') 


download_staging_db: 
    psql -h $(LOCAL_DOCKER_IP) -p 5432 -U postgres -c 'drop database mydb' 
    psql -h $(LOCAL_DOCKER_IP) -p 5432 -U postgres -c 'create database mydb' 
    ssh [email protected]$(STAGING_IP) "docker exec $(DOCKER_DB_ID) su postgres -c 'pg_dump mydb'" | psql -h $(LOCAL_DOCKER_IP) -p 5432 -U postgres mydb 

upload_release_db_to_staging_db: 
    ssh [email protected]$(RELEASE_IP) "cd/&& su postgres -c 'pg_dump my db'" > temp.sql 
    scp temp.sql [email protected]$(STAGING_IP):/temp.sql 
    ssh [email protected]$(STAGING_IP) "docker cp /temp.sql $(DOCKER_DB_ID):/temp.sql" 
    ssh [email protected]$(STAGING_IP) "docker exec $(DOCKER_DB_ID) su postgres -c 'drop database mydb'" 
    ssh [email protected]$(STAGING_IP) "docker exec $(DOCKER_DB_ID) su postgres -c 'create database mydb'" 
    ssh [email protected]$(STAGING_IP) "docker exec $(DOCKER_DB_ID) su postgres -c 'psql mydb < /temp.sql'" 
    rm temp.sql 
相關問題