我正在尝试备份/恢复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 

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


当前回答

我想为备份和恢复添加官方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.

其他回答

我认为您还可以使用postgres备份容器,它可以在给定的时间内备份您的数据库。

  pgbackups:
    container_name: Backup
    image: prodrigestivill/postgres-backup-local
    restart: always
    volumes:
      - ./backup:/backups
    links:
      - db:db
    depends_on:
      - db
    environment:
      - POSTGRES_HOST=db
      - POSTGRES_DB=${DB_NAME} 
      - POSTGRES_USER=${DB_USER}
      - POSTGRES_PASSWORD=${DB_PASSWORD}
      - POSTGRES_EXTRA_OPTS=-Z9 --schema=public --blobs
      - SCHEDULE=@every 0h30m00s
      - BACKUP_KEEP_DAYS=7
      - BACKUP_KEEP_WEEKS=4
      - BACKUP_KEEP_MONTHS=6
      - HEALTHCHECK_PORT=81

另一种方法(基于docker-postgresql-workflow)

本地运行的数据库(不是在docker中,但相同的方法可以工作)导出:

pg_dump -F c -h localhost mydb -U postgres export.dmp

要导入的容器数据库:

docker run -d -v /local/path/to/postgres:/var/lib/postgresql/data postgres #ex runs container as `CONTAINERNAME` #find via `docker ps`
docker run -it --link CONTAINERNAME:postgres  --volume $PWD/:/tmp/  postgres  bash -c 'exec pg_restore -h postgres -U postgres -d mydb -F c /tmp/sonar.dmp'

我在尝试使用db_dump恢复数据库时遇到了这个问题。我通常使用dbeaver来恢复-但是收到一个psql转储,所以必须想出一个方法来恢复使用docker容器。

Forth推荐并由soviet编辑的方法对我很有效:

猫your_dump。sql | docker exec -i your-db-container psql -U postgres -d dbname

(因为这是一个单一的db转储,而不是多个db,我包括了名称)

然而,为了让它工作,我还必须进入docker容器和项目所在的virtualenv。这回避了我一点之前弄清楚-因为我收到以下docker错误。

读取Unix @->/var/run/docker。Sock: read:由对等体重置连接

这可能是由文件/var/lib/docker/network/files/local-kv.db引起的。我不知道这句话的准确性:但我相信我看到了这一点,因为我不在本地使用docker,因此没有这个文件,它正在寻找,使用Forth的答案。

然后我导航到正确的目录(与项目)激活virtualenv,然后运行接受的答案。砰,像陀螺一样工作。希望这能帮助到其他人!

我想为备份和恢复添加官方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.

上面那个答案对我没用。我一直得到这个错误:

psql: error: FATAL:  Peer authentication failed for user "postgres"

为了让它工作,我必须为docker容器指定一个用户:

备份

docker exec -t --user postgres your-db-container pg_dumpall -c -U postgres > dump_`date +%d-%m-%Y"_"%H_%M_%S`.sql

恢复

cat your_dump.sql | docker exec -i --user postgres your-db-container psql -U postgres