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

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


当前回答

备份数据库

生成sql:

docker exec -t your-db-container pg_dumpall -c -u your-db-user > dump_$(date +%Y-%m-%d_%H_%M_%S).sql

为了减少SQL的大小,你可以生成一个压缩:

docker exec -t your-db-container pg_dumpall -c -u your-db-user | gzip > ./dump_$(date +"%Y-%m-%d_%H_%M_%S").gz

恢复数据库

猫your_dump。sql | docker exec -i your-db-container psql -U your-db-user -d your-db-name

恢复已压缩的sql:

gunzip < your_dump.sql.gz | docker exec -i your-db-container psql -U your-db-user -d your-db-name

PD:这是对我有用的东西的汇编,以及我从这里和其他地方得到的东西。我开始做贡献,任何反馈将不胜感激。

其他回答

这是对我有用的命令。

cat your_dump.sql | sudo docker exec -i {docker-postgres-container} psql -U {user} -d {database_name}

例如

cat table_backup.sql | docker exec -i 03b366004090 psql -U postgres -d postgres

参考:GMartinez-Sisti在讨论中给出的解决方案。 https://gist.github.com/gilyes/525cc0f471aafae18c3857c27519fc4b

我认为您还可以使用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文档。这适用于卷中的所有类型的数据,而不仅仅是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.

在Docker卷上使用文件系统级备份

示例Docker Compose

version: "3.9"

services:
  db:
    container_name: pg_container
    image: platerecognizer/parkpow-postgres
    # restart: always
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    environment:
      POSTGRES_USER: admin
      POSTGRES_PASSWORD: admin
      POSTGRES_DB: admin

volumes:
  postgres_data:

备份Postgresql卷

docker run --rm \
   --user root \
   --volumes-from pg_container \
   -v /tmp/db-bkp:/backup \
   ubuntu tar cvf /backup/db.tar /var/lib/postgresql/data

然后将/tmp/db-bkp拷贝到第二台主机

恢复Postgresql卷

docker run --rm \
   --user root \
   --volumes-from pg_container \
   -v /tmp/db-bkp:/backup \
   ubuntu bash -c "cd /var && tar xvf /backup/db.tar --strip 1"

备份数据库

生成sql:

docker exec -t your-db-container pg_dumpall -c -u your-db-user > dump_$(date +%Y-%m-%d_%H_%M_%S).sql

为了减少SQL的大小,你可以生成一个压缩:

docker exec -t your-db-container pg_dumpall -c -u your-db-user | gzip > ./dump_$(date +"%Y-%m-%d_%H_%M_%S").gz

恢复数据库

猫your_dump。sql | docker exec -i your-db-container psql -U your-db-user -d your-db-name

恢复已压缩的sql:

gunzip < your_dump.sql.gz | docker exec -i your-db-container psql -U your-db-user -d your-db-name

PD:这是对我有用的东西的汇编,以及我从这里和其他地方得到的东西。我开始做贡献,任何反馈将不胜感激。