fruworg.github.io/content/posts/postgres-simple-backup.md
2023-09-30 14:18:44 +03:00

27 lines
1017 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: Простой бэкап Postgres Pro
description: Используя pg_dump, pg_dumpall, pg_restore и cron
date: 2022-12-14T16:48:00+05:00
tags: [linux, postgres]
---
## Скрипт для бэкапа БД и глобальных объектов
В файл `/home/<username>/pg-backup.sh` необходимо добавить следующие строки:
```sh
#!/usr/bin/env bash
pg_dump -U <username> -h <pg-hostname> -Fc <db> --file=<db>-$(date '+%Y-%m-%d').dump
pg_dumpall -U <username> -h <pg-hostname> --globals --file=gb-$(date '+%Y-%m-%d').dump
```
## Файл cron с запуском скрипта (каждый день в 2:00)
Необходимо дописать в конец `crontab -e -u <username>` следующие строки:
```sh
0 2 * * * /usr/bin/env bash /home/<username>/pg-backup.sh
```
## Рестор файлов БД и глобальных объектов
```shell
pg_restore -C -d postgres <dbname>-<date>.dump
psql -U postgres < gb-<date>.dump
```