我正在尝试备份/恢复PostgreSQL数据库,正如Docker网站上所解释的那样,但数据没有恢复。

数据库映像使用的卷是:

VOLUME  ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql"]

CMD为:

CMD ["/usr/lib/postgresql/9.3/bin/postgres", "-D", "/var/lib/postgresql/9.3/main", "-c", "config_file=/etc/postgresql/9.3/main/postgresql.conf"]

我用下面的命令创建DB容器:

docker run -it --name "$DB_CONTAINER_NAME" -d "$DB_IMAGE_NAME"

然后我连接另一个容器手动插入一些数据:

docker run -it --rm --link "$DB_CONTAINER_NAME":db "$DB_IMAGE_NAME" sh -c 'exec bash'
psql -d test -h $DB_PORT_5432_TCP_ADDR
# insert some data in the db
<CTRL-D>
<CTRL-D>

然后创建tar存档:

$ sudo docker run --volumes-from "$DB_CONTAINER_NAME" --rm -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar /etc/postgresql /var/log/postgresql /var/lib/postgresql

现在我删除用于db的容器,并创建另一个同名的容器,并尝试恢复之前插入的数据:

$ sudo docker run --volumes-from "$DB_CONTAINER_NAME" --rm -v $(pwd):/backup ubuntu tar xvf /backup/backup.tar 

但是表是空的,为什么数据没有正确恢复?


当前回答

猫db。Dump | docker exec…这种方法并不适用于我的转储(~2Gb)。它花了几个小时,最后出现内存不足的错误。

相反,我cp'ed dump到容器和pg_restore'ed它从内部。

假设容器id为CONTAINER_ID,数据库名称为DB_NAME:

# copy dump into container
docker cp local/path/to/db.dump CONTAINER_ID:/db.dump

# shell into container
docker exec -it CONTAINER_ID bash

# restore it from within
pg_restore -U postgres -d DB_NAME --no-owner -1 /db.dump

其他回答

docker-compose用户的解决方案:

At First run the docker-compose file by any on of following commands: $ docker-compose -f loca.yml up OR docker-compose -f loca.yml up -d For taking backup: $ docker-compose -f local.yml exec postgres backup To see list of backups inside container: $ docker-compose -f local.yml exec postgres backups Open another terminal and run following command: $ docker ps Look for the CONTAINER ID of postgres image and copy the ID. Let's assume the CONTAINER ID is: ba78c0f9bcee Now to bring that backup into your local file system, run the following command: $ docker cp ba78c0f9bcee:/backups ./local_backupfolder

希望这能帮助到像我一样迷路的人。

注意:这个解决方案的全部细节可以在这里找到。

另一种方法是从主机上运行pg_restore(当然,如果您在主机上设置了postgres)命令。

假设在docker-compose文件中有postgres服务的端口映射“5436:5432”。有了这个端口映射,就可以通过主机的端口5436访问容器的postgres(运行在端口5432上)

pg_restore -h localhost -p 5436 -U <POSTGRES_USER> -d <POSTGRES_DB>  /Path/to/the/.psql/file/in/your/host_machine 

这样,您就不必深入容器的终端或将转储文件复制到容器中。

我想为备份和恢复添加官方docker文档。这适用于卷中的所有类型的数据,而不仅仅是postegres。

Backup a container Create a new container named dbstore: $ docker run -v /dbdata --name dbstore ubuntu /bin/bash Then in the next command, we: Launch a new container and mount the volume from the dbstore container Mount a local host directory as /backup Pass a command that tars the contents of the dbdata volume to a backup.tar file inside our /backup directory. $ docker run --rm --volumes-from dbstore -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar /dbdata When the command completes and the container stops, we are left with a backup of our dbdata volume. Restore container from backup With the backup just created, you can restore it to the same container, or another that you made elsewhere. For example, create a new container named dbstore2: $ docker run -v /dbdata --name dbstore2 ubuntu /bin/bash Then un-tar the backup file in the new container`s data volume: $ docker run --rm --volumes-from dbstore2 -v $(pwd):/backup ubuntu bash -c "cd /dbdata && tar xvf /backup/backup.tar --strip 1" You can use the techniques above to automate backup, migration and restore testing using your preferred tools.

猫db。Dump | docker exec…这种方法并不适用于我的转储(~2Gb)。它花了几个小时,最后出现内存不足的错误。

相反,我cp'ed dump到容器和pg_restore'ed它从内部。

假设容器id为CONTAINER_ID,数据库名称为DB_NAME:

# copy dump into container
docker cp local/path/to/db.dump CONTAINER_ID:/db.dump

# shell into container
docker exec -it CONTAINER_ID bash

# restore it from within
pg_restore -U postgres -d DB_NAME --no-owner -1 /db.dump

Dksnap (https://github.com/kelda/dksnap)自动运行pg_dumpall并通过/docker-entrypoint-initdb.d加载转储。

它向您显示正在运行的容器列表,您可以选择要备份的容器。生成的工件是一个常规的Docker映像,因此您可以Docker运行它,或者通过将它推送到Docker注册表来共享它。

(免责声明:我是项目的维护者)