centos

centos postgresql备份与恢复方法

小樊
37
2025-02-27 04:01:21
栏目: 云计算

在CentOS上备份和恢复PostgreSQL数据库主要有两种方法:逻辑备份和物理备份。以下是详细的步骤和命令示例。

逻辑备份

逻辑备份使用pg_dumppg_dumpall命令,适用于备份单个数据库或整个实例。

使用pg_dump备份

pg_dump -h hostname -U username -p port -d database -F t -f output_file

使用pg_dumpall备份

pg_dumpall -h hostname -U username -p port -f output_file

恢复数据

使用pg_restore命令恢复数据。

pg_restore -h hostname -U username -p port -d database input_file

物理备份

物理备份通过复制数据库的物理文件(数据文件和WAL日志)来实现。

使用pg_basebackup进行物理备份

pg_basebackup -h hostname -U username -p port -d database -F t -f output_file

恢复数据

pg_basebackup -h hostname -U username -p port -d database -R

使用Barman进行物理备份和恢复

Barman是一个用于PostgreSQL的备份和恢复工具,支持物理备份和时间点恢复。

安装Barman

wget https://download.2ndquadrant.com/barman/barman-2.18.0.linux-x64.tar.gz
tar xzf barman-2.18.0.linux-x64.tar.gz
cd barman
./configure
make
sudo make install

配置Barman

编辑/etc/barman.conf文件,配置备份目标目录和其他选项。

[global]
    backup_dir = /var/lib/barman/backups
    log_level = INFO
    log_file = /var/log/barman/barman.log

创建备份

barman create mydb /path/to/backup/directory

恢复数据库

barman restore mydb /path/to/backup/directory
```。

以上就是在CentOS上备份和恢复PostgreSQL数据库的方法。根据实际需求选择合适的备份方式,并确保在执行备份和恢复操作时具有适当的权限。

0
看了该问题的人还看了