首页 > 解决方案 > Dump PostgreSQL Database from Docker through SSH

问题描述

I am renting a remote server. On that server, I am running a PostgreSQL database inside a Docker instance. I would like to back up the database directly to my localhost, without having to make a copy on the remote server.

I know I can dump the database from the Docker instance to a local file (sqlbkp.bak) from the remote server using:

docker exec <container_name> pg_dump <schema_name> > /tmp/sqlbkp.bak

Then I could transfer that remote backup to my localhost using Rync from my localhost, like:

rsync -a user@remoteserver:/tmp/sqlbkp.bak /home/sqlbkp.bak 

However, using these two steps, I require extra disk space on my remote server for the backup (at least as large as the database). Then, immediately after transferring the backup, I can free the remote space. I find this inconvenient and it seems unnecessary. Is there a way to combine these steps into one, such that I could run pg_dump over SSH (against a database inside a Docker instance)?

标签: postgresqldockersshrsync

解决方案


您可以使用 ssh 在远程执行任何命令,然后该命令的 std 输出通过 ssh 通道返回成为 ssh 的 std 输出,并且可以重定向到本地计算机上的文件。

ssh user@dockerhost 'docker exec <container_name> pg_dump <schema_name>' > /home/sqlbkp.bak

推荐阅读