developer tip

고정 된 PostgreSQL 데이터베이스 백업 / 복원

optionbox 2020. 8. 8. 12:27
반응형

고정 된 PostgreSQL 데이터베이스 백업 / 복원


Docker 웹 사이트에 설명 된대로 PostgreSQL 데이터베이스를 백업 / 복원하려고하는데 데이터가 복원되지 않습니다.

데이터베이스 이미지에서 사용하는 볼륨은 다음과 같습니다.

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 exec -t your-db-container pg_dumpall -c -U postgres > dump_`date +%d-%m-%Y"_"%H_%M_%S`.sql

데이터베이스 복원

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

좋아, 나는 이것을 알아 냈다. Postgresql은 일단 시작되면 / var / lib / postgresql 폴더의 변경 사항을 감지하지 못합니다. 적어도 내가 감지하려는 변경 사항은 아닙니다.

첫 번째 해결책은 postgres 서버를 직접 시작하는 대신 bash로 컨테이너를 시작하고 데이터를 복원 한 다음 서버를 수동으로 시작하는 것입니다.

두 번째 해결책은 데이터 컨테이너를 사용하는 것입니다. 전에는 요점을 이해하지 못했지만 지금은 이해합니다. 이 데이터 컨테이너를 사용하면 postgres 컨테이너를 시작하기 전에 데이터를 복원 할 수 있습니다. 따라서 postgres 서버가 시작되면 데이터가 이미 있습니다.


주어진 시간 내에 데이터베이스를 백업하는 potgres 백업 컨테이너를 사용할 수도 있다고 생각합니다.

  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 기반 )

내보낼 로컬 실행 데이터베이스 (도커에는 없지만 동일한 접근 방식이 작동 함) :

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'

I had this issue while trying to use a db_dump to restore a db. I normally use dbeaver to restore- however received a psql dump, so had to figure out a method to restore using the docker container.

The methodology recommended by Forth and edited by Soviut worked for me:

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

(since this was a single db dump and not multiple db's i included the name)

However, in order to get this to work, I had to also go into the virtualenv that the docker container and project were in. This eluded me for a bit before figuring it out- as I was receiving the following docker error.

read unix @->/var/run/docker.sock: read: connection reset by peer

This can be caused by the file /var/lib/docker/network/files/local-kv.db .I don't know the accuracy of this statement: but I believe I was seeing this as I do not user docker locally, so therefore did not have this file, which it was looking for, using Forth's answer.

I then navigated to correct directory (with the project) activated the virtualenv and then ran the accepted answer. Boom, worked like a top. Hope this helps someone else out there!


This is the command worked for me.

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

for example

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

Reference: Solution given by GMartinez-Sisti in this discussion. https://gist.github.com/gilyes/525cc0f471aafae18c3857c27519fc4b

참고URL : https://stackoverflow.com/questions/24718706/backup-restore-a-dockerized-postgresql-database

반응형