OpenGauss 数据库部署与备份恢复实战文档
OpenGauss 数据库部署与备份恢复实战文档
一、OpenGauss 概述
OpenGauss 是一款开源的关系型数据库管理系统,由华为公司贡献并开源,具备以下核心优势:
-
高性能
- 采用多线程架构,支持高并发事务处理。
- 内置 NUMA 亲和性优化、智能索引、并行查询等特性,TPC-C 基准测试表现优异。
- 支持鲲鹏、x86 等多种硬件平台,充分发挥多核性能。
-
高可用与高可靠
- 提供主备同步、异步流复制,支持自动故障切换。
- 支持物理备份、逻辑备份、PITR(时间点恢复)等多种恢复手段。
- 数据页完整性校验、WAL 日志保护,保障数据不丢失。
-
安全合规
- 提供全密态计算、透明加密、动态数据脱敏等安全特性。
- 支持三权分立、强制访问控制,满足等保 2.0 标准。
-
兼容性与易用性
- 高度兼容 PostgreSQL,迁移成本低。
- 提供完善的工具链(gsql、gs_dump、gs_restore、gs_basebackup 等)。
- 支持 SQL 标准 2016,并提供 Oracle 兼容模式。
-
生态开放
- 开源社区活跃,支持多种第三方插件,可集成 Prometheus、Grafana 等监控系统。
- 官方提供详细文档和社区支持,持续迭代。
二、部署环境准备
本部署基于以下环境信息(单节点):
- 操作系统:openEuler 22.03
- IP 地址:192.168.100.174
- 磁盘空间:根分区 90G 可用
- 内存:8 GiB
2.1 系统依赖安装
# 安装必要依赖包
dnf install -y bzip2 libaio-devel flex bison ncurses-devel glibc-devel patch readline-devel tar
2.2 创建操作系统用户和组
# 修改主机名
[root@localhost ~]# hostnamectl set-hostname opengauss-server
[root@localhost ~]# bash
Welcome to 6.6.0-132.0.0.111.oe2403sp3.x86_64
System information as of time: 2026年 03月 30日 星期一 17:43:48 CST
System load: 0.00
Memory used: 1.7%
Swap used: 0.0%
Usage On: 3%
IP address: 192.168.100.174
Users online: 2
# 查看版本号
[root@opengauss-server ~]# cat /etc/os-release
NAME="openEuler"
VERSION="22.03 (LTS-SP4)"
ID="openEuler"
VERSION_ID="22.03"
PRETTY_NAME="openEuler 22.03 (LTS-SP4)"
ANSI_COLOR="0;31"
[root@opengauss-server ~]#
# 创建数据库用户组
groupadd dbgrp
# 创建用户 omm(用于运行数据库)
useradd -g dbgrp -m -s /bin/bash omm
# 设置 omm 用户密码(根据实际情况设置)
echo "omm:OpenGauss@123" | chpasswd
# 赋予sudo权限(可选)
echo "omm ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers.d/omm
2.3 规划目录结构
# 创建数据库相关目录
[root@opengauss-server ~]# mkdir -p /data/software/openGauss /data/opengauss/{om,core,data/dn,log/opengauss}
[root@opengauss-server ~]# chown -R omm:dbgrp /data/opengauss
[root@opengauss-server ~]# chown -R omm:dbgrp /data/software/openGauss
[root@opengauss-server ~]#
[root@opengauss-server ~]# chmod 755 /data/opengauss
[root@opengauss-server ~]# tree -d /data/
/data/
├── opengauss
│ ├── core # 进程崩溃核心转储目录
│ ├── data
│ │ └── dn # 数据节点存储目录
│ ├── log
│ │ └── opengauss # 数据库运行日志目录
│ └── om # OM 运维管理模块目录
└── software
└── openGauss # 存放软件安装包
2.4 配置内核参数(优化性能)
编辑 /etc/sysctl.conf,追加以下内容:
# 内核参数优化
kernel.sem = 50100 64128000 50100 1280
net.ipv4.tcp_tw_reuse = 1
net.core.rmem_default = 262144
net.core.rmem_max = 4194304
net.core.wmem_default = 262144
net.core.wmem_max = 4194304
net.ipv4.tcp_rmem = 4096 8192 4194304
net.ipv4.tcp_wmem = 4096 8192 4194304
vm.swappiness = 0
vm.overcommit_memory = 0
生效配置:
sysctl -p
2.5 配置资源限制(用户进程数、文件数)
编辑 /etc/security/limits.conf,添加:
omm soft nproc 131072
omm hard nproc 131072
omm soft nofile 65536
omm hard nofile 65536
编辑 /etc/security/limits.d/20-nproc.conf(若存在)确保包含上述配置。
2.6 关闭防火墙(生产环境需按需开放端口)
systemctl stop firewalld
systemctl disable firewalld
或者开放数据库端口(默认5432):
firewall-cmd --permanent --add-port=5432/tcp
firewall-cmd --reload
2.7 关闭 SELinux(或配置策略)
setenforce 0
sed -i 's/^SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
三、安装 OpenGauss
3.1 下载安装包
访问 OpenGauss 官网 下载适用于 openEuler 24.03 的安装包。示例命令:
[root@opengauss-server ~]# cd /data/software/openGauss/
[root@opengauss-server openGauss]# wget https://opengauss.obs.cn-south-1.myhuaweicloud.com/6.0.3/openEuler22.03/x86/openGauss-All-6.0.3-openEuler22.03-x86_64.tar.gz
--2026-03-30 21:49:28-- https://opengauss.obs.cn-south-1.myhuaweicloud.com/6.0.3/openEuler22.03/x86/openGauss-All-6.0.3-openEuler22.03-x86_64.tar.gz
正在解析主机 opengauss.obs.cn-south-1.myhuaweicloud.com (opengauss.obs.cn-south-1.myhuaweicloud.com)... 139.9.23.166, 139.159.208.234, 121.37.63.47, ...
正在连接 opengauss.obs.cn-south-1.myhuaweicloud.com (opengauss.obs.cn-south-1.myhuaweicloud.com)|139.9.23.166|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:111514088 (106M) [application/gzip]
正在保存至: “openGauss-All-6.0.3-openEuler22.03-x86_64.tar.gz”
openGauss-All-6.0.3-openEuler22.0 100%[============================================================>] 106.35M 3.57MB/s 用时 28s
2026-03-30 21:49:56 (3.84 MB/s) - 已保存 “openGauss-All-6.0.3-openEuler22.03-x86_64.tar.gz” [111514088/111514088])
[root@opengauss-server openGauss]#
[root@opengauss-server openGauss]# ll
总用量 108904
-rw-r--r--. 1 root root 111514088 12月 29 15:24 openGauss-All-6.0.3-openEuler22.03-x86_64.tar.gz
[root@opengauss-server openGauss]#
# 查看架构命令
[root@opengauss-server ~]# arch
x86_64
[root@opengauss-server ~]#
注意:请根据实际版本和架构(x86_64或aarch64)选择。当前环境为 x86_64。
3.2 解压安装包
[root@opengauss-server openGauss]# pwd
/data/software/openGauss
[root@opengauss-server openGauss]# tar -zxvf openGauss-All-6.0.3-openEuler22.03-x86_64.tar.gz
openGauss-CM-6.0.3-openEuler22.03-x86_64.tar.gz
openGauss-OM-6.0.3-openEuler22.03-x86_64.tar.gz
openGauss-Server-6.0.3-openEuler22.03-x86_64.tar.bz2
openGauss-CM-6.0.3-openEuler22.03-x86_64.sha256
openGauss-OM-6.0.3-openEuler22.03-x86_64.sha256
openGauss-Server-6.0.3-openEuler22.03-x86_64.sha256
upgrade_sql.tar.gz
upgrade_sql.sha256
[root@opengauss-server openGauss]#
# 解压OM包(关键!会生成script目录)
[root@opengauss-server openGauss]# tar -xf openGauss-OM-6.0.3-openEuler22.03-x86_64.tar.gz
# 验证script目录存在
[root@opengauss-server openGauss]# ls -l script/gs_preinstall
-rwxr-xr-x. 1 root root 29380 12月 23 21:48 script/gs_preinstall
[root@opengauss-server openGauss]#
[root@opengauss-server openGauss]# ls -l script/gs_install
-rwxr-xr-x. 1 root root 12987 12月 23 21:48 script/gs_install
[root@opengauss-server openGauss]#
# 应看到 gs_preinstall, gs_install 等脚本
3.3 执行一体化安装脚本
OpenGauss 提供了一体化安装脚本 gs_install,先以 root 用户执行预安装,再以 omm 用户执行安装。
3.3.1 准备 XML 配置文件
创建 /home/omm/cluster_config.xml,内容如下(单机模式):
<?xml version="1.0" encoding="UTF-8"?>
<ROOT>
<!-- openGauss整体信息 -->
<CLUSTER>
<!-- 数据库名称 -->
<PARAM name="clusterName" value="dbCluster" />
<!-- 数据库节点名称(hostname) -->
<PARAM name="nodeNames" value="opengauss-server" />
<!-- 数据库安装目录-->
<PARAM name="gaussdbAppPath" value="/data/opengauss/app" />
<!-- 日志目录-->
<PARAM name="gaussdbLogPath" value="/data/opengauss/log/opengauss" />
<!-- 临时文件目录-->
<PARAM name="tmpMppdbPath" value="/home/omm/tmp" />
<!-- 数据库工具目录-->
<PARAM name="gaussdbToolPath" value="/data/opengauss/om" />
<!-- 数据库core文件目录-->
<PARAM name="corePath" value="/data/opengauss/core" />
<!-- 节点IP,与数据库节点名称列表一一对应 -->
<!-- 如果用ipv6 替换ipv4地址即可 如:<PARAM name="backIp1s" value="2407:xxxx:xxxx:xxxx:xxxx:xxxx:caa:2335"/> -->
<PARAM name="backIp1s" value="192.168.100.174"/>
</CLUSTER>
<!-- 每台服务器上的节点部署信息 -->
<DEVICELIST>
<!-- 节点1上的部署信息 -->
<DEVICE sn="10000001">
<!-- 节点1的主机名称 -->
<PARAM name="name" value="opengauss-server"/>
<!-- 节点1所在的AZ及AZ优先级 -->
<PARAM name="azName" value="AZ1"/>
<PARAM name="azPriority" value="1"/>
<!-- 节点1的IP,如果服务器只有一个网卡可用,将backIP1和sshIP1配置成同一个IP -->
<!-- 用ipv6安装部署时 换上ipv6地址即可,后面xml文件示例也是同样操作 -->
<PARAM name="backIp1" value="192.168.100.174"/>
<PARAM name="sshIp1" value="192.168.100.174"/>
<!--dbnode-->
<PARAM name="dataNum" value="1"/>
<!-- 端口 -->
<PARAM name="dataPortBase" value="5432"/>
<PARAM name="dataNode1" value="/data/opengauss/data/dn"/>
<PARAM name="dataNode1_syncNum" value="0"/>
</DEVICE>
</DEVICELIST>
</ROOT>
3.3.2 执行预安装(root)
[root@opengauss-server ~]# cd /data/software/openGauss/
[root@opengauss-server openGauss]# ./script/gs_preinstall -U omm -G dbgrp -X /home/omm/cluster_config.xml
Parsing the configuration file.
Successfully parsed the configuration file.
Installing the tools on the local node.
Successfully installed the tools on the local node.
Setting host ip env
Successfully set host ip env.
Are you sure you want to create the user[omm] (yes/no)? y # 输入yes
Preparing SSH service.
Successfully prepared SSH service.
Checking OS software.
Successfully check OS software.
Checking OS version.
Successfully checked OS version.
Checking cpu instructions.
Successfully checked cpu instructions.
Creating cluster's path.
Successfully created cluster's path.
Set and check OS parameter.
Setting OS parameters.
Successfully set OS parameters.
Warning: Installation environment contains some warning messages.
Please get more details by "/data/software/openGauss/script/gs_checkos -i A -h opengauss-server -X /home/omm/cluster_config.xml --detail".
Set and check OS parameter completed.
Preparing CRON service.
Successfully prepared CRON service.
Setting user environmental variables.
Successfully set user environmental variables.
Setting the dynamic link library.
Successfully set the dynamic link library.
Setting Core file
Successfully set core path.
Setting pssh path
Successfully set pssh path.
Setting Cgroup.
Successfully set Cgroup.
Set ARM Optimization.
No need to set ARM Optimization.
Fixing server package owner.
Setting finish flag.
Successfully set finish flag.
Preinstallation succeeded.
[root@opengauss-server openGauss]#
预安装脚本会创建必要目录、检查环境、配置系统服务等。
3.3.3 切换至 omm 用户并执行安装
[root@opengauss-server openGauss]# su - omm
上一次登录: 一 3月 30 22:02:17 CST 2026
Welcome to 5.10.0-216.0.0.115.oe2203sp4.x86_64
System information as of time: 2026年 03月 30日 星期一 22:03:20 CST
System load: 0.21
Memory used: 4.1%
Swap used: 0.0%
Usage On: 4%
IP address: 192.168.100.174
Users online: 3
To run a command as administrator(user "root"),use "sudo <command>".
[omm@opengauss-server ~]$ cd /data/software/openGauss/
[omm@opengauss-server openGauss]$ source ~/.bashrc
[omm@opengauss-server openGauss]$ ./script/gs_install -X /home/omm/cluster_config.xml --gsinit-parameter="--locale=en_US.UTF-8"
Parsing the configuration file.
Successfully checked gs_uninstall on every node.
Check preinstall on every node.
Successfully checked preinstall on every node.
Creating the backup directory.
Successfully created the backup directory.
begin deploy..
Installing the cluster.
begin prepare Install Cluster..
Checking the installation environment on all nodes.
begin install Cluster..
Installing applications on all nodes.
Successfully installed APP.
begin init Instance..
encrypt cipher and rand files for database.
Please enter password for database: # OpenGuass@123
Please repeat for database: # OpenGuass@123
begin to create CA cert files
The sslcert will be generated in /data/opengauss/app/share/sslcert/om
NO cm_server instance, no need to create CA for CM.
Non-dss_ssl_enable, no need to create CA for DSS
Cluster installation is completed.
Configuring.
Deleting instances from all nodes.
Successfully deleted instances from all nodes.
Checking node configuration on all nodes.
Initializing instances on all nodes.
Updating instance configuration on all nodes.
Check consistence of memCheck and coresCheck on database nodes.
Configuring pg_hba on all nodes.
Configuration is completed.
The cluster status is Normal.
Successfully started cluster.
Successfully installed application.
end deploy..
[omm@opengauss-server openGauss]$ echo $?
0
[omm@opengauss-server openGauss]$
安装过程中会提示设置数据库密码OpenGuass@123(至少包含大小写字母、数字、特殊字符,长度8位以上)。
3.4 检查安装结果
# 查看集群状态
[omm@opengauss-server openGauss]$ gs_om -t status --detail
[ Cluster State ]
cluster_state : Normal
redistributing : No
current_az : AZ_ALL
[ Datanode State ]
node node_ip port instance state
---------------------------------------------------------------------------------------------------
1 opengauss-server 192.168.100.174 5432 6001 /data/opengauss/data/dn P Primary Normal
[omm@opengauss-server openGauss]$
# 停止数据库集群
gs_om -t stop
# 启动数据库
gs_om -t start
# 检查恢复后的状态
[omm@opengauss-server openGauss]$ gs_om -t status
-----------------------------------------------------------------------
cluster_name : dbCluster
cluster_state : Normal
redistributing : No
-----------------------------------------------------------------------
[omm@opengauss-server openGauss]$
3.5 配置环境变量(可选)
在 /home/omm/.bashrc 末尾添加:
# Source default setting
[ -f /etc/bashrc ] && . /etc/bashrc
# User environment PATH
export PATH
export UNPACKPATH=/data/software/openGauss
export PGDATA=/data/opengauss/data/dn
export COREPATH=/data/opengauss/core
export PGDATABASE=postgres
export PGPORT=5432
export IP_TYPE=ipv4
export GPHOME=/data/opengauss/om
export PATH=$GPHOME/script/gspylib/pssh/bin:$GPHOME/script:$PATH
export LD_LIBRARY_PATH=$GPHOME/script/gspylib/clib:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=$GPHOME/lib:$LD_LIBRARY_PATH
export PYTHONPATH=$GPHOME/lib
export PATH=/home/omm/gauss_om/script:$PATH
export GAUSSHOME=/data/opengauss/app
export PATH=$GAUSSHOME/bin:$PATH
export LD_LIBRARY_PATH=$GAUSSHOME/lib:$LD_LIBRARY_PATH
export S3_CLIENT_CRT_FILE=$GAUSSHOME/lib/client.crt
export GAUSS_VERSION=6.0.3
export PGHOST=/home/omm/tmp
export GAUSSLOG=/data/opengauss/log/opengauss/omm
umask 077
export GAUSS_ENV=2
export GS_CLUSTER_NAME=dbCluster
然后执行 source ~/.bashrc 使其生效。
3.6 配置systemctl 来控制启停(可选)
步骤一:创建 systemd 服务文件
在 /etc/systemd/system/ 目录下,创建一个名为 opengauss.service 的文件,并填入以下内容。这里的内容已根据你的实际部署路径进行了适配。
# 1. 创建并编辑服务文件
sudo vim /etc/systemd/system/opengauss.service
服务文件内容:
[Unit]
Description=openGauss Database Server
# 确保在网络服务启动后再启动数据库,提高稳定性
After=network.target
# 可选:如果希望数据库在其他服务之后启动,可以添加,如 After=syslog.target
[Service]
# 后台运行模式,因为 gs_ctl 会 fork 出主进程
Type=forking
# 指定运行服务的用户和组,与你文档中的配置保持一致
User=omm
Group=dbgrp
# 设置环境变量,确保 gs_ctl 命令和相关库文件能被正确找到
Environment="GAUSSHOME=/data/opengauss/app"
Environment="PGDATA=/data/opengauss/data/dn"
Environment="LD_LIBRARY_PATH=/data/opengauss/app/lib"
# 启动命令:使用 gs_ctl 启动数据库
ExecStart=/data/opengauss/app/bin/gs_ctl start -D ${PGDATA}
# 停止命令:使用 gs_ctl 停止数据库
ExecStop=/data/opengauss/app/bin/gs_ctl stop -D ${PGDATA} -m fast
# 重启命令:使用 gs_ctl 重启数据库
ExecReload=/data/opengauss/app/bin/gs_ctl restart -D ${PGDATA}
# 进程意外退出时自动重启
Restart=on-failure
# 优雅停止的信号,openGauss 支持 SIGINT 进行快速关闭
KillMode=mixed
KillSignal=SIGINT
# 设置超时时间,避免因等待过久而被强制杀死
TimeoutSec=300
[Install]
# 表示在多用户模式下启动该服务
WantedBy=multi-user.target
请重点检查以下几项路径,确保与你的实际环境一致:
GAUSSHOME: 你的文档中是/data/opengauss/app。PGDATA: 你的文档中是/data/opengauss/data/dn。ExecStart/Stop: 指向$GAUSSHOME/bin/gs_ctl。
步骤二:启用并启动服务
-
重新加载 systemd 配置:通知 systemd 服务文件有新的变化。
sudo systemctl daemon-reload -
设置开机自启:这会在系统启动时自动拉起 openGauss 服务。
sudo systemctl enable opengauss -
立即启动服务:
sudo systemctl start opengauss
步骤三:验证配置结果
-
检查服务状态:确认服务是否正常运行。
sudo systemctl status opengauss你应该能看到类似
active (running)的状态输出。 -
验证数据库连接:切换到
omm用户,使用gsql尝试连接,确保数据库功能正常。su - omm gsql -d postgres -p 5432 -c "SELECT version();"
3.6 使用 crontab 的 @reboot 来自启
cron 服务在系统启动时会自动执行当前用户 crontab 中标记为 @reboot 的任务,不需要任何特权。
- 确认
crond服务已运行
虽然 omm 没有 sudo,但通常 crond 是系统默认启动的。你可以执行以下命令检查(如果提示无权限查看状态,直接跳过,因为绝大多数系统都默认开启):
ps -ef | grep crond | grep -v grep
若没有运行,需要请管理员启动。但生产环境通常都已运行。
- 以
omm用户编辑 crontab
su - omm
crontab -e
如果第一次使用,会提示选择编辑器(推荐 vi 或 nano)。
- 添加开机自启任务
在文件中添加一行(注意使用绝对路径):
@reboot /data/opengauss/app/bin/gs_ctl start -D /data/opengauss/data/dn
但为了确保环境变量(如 LD_LIBRARY_PATH)正确加载,更稳妥的做法是先写一个启动脚本,然后在 crontab 中调用该脚本。
- 3.1 创建启动脚本
mkdir -p /home/omm/bin
vi /home/omm/bin/start_opengauss.sh
内容如下:
#!/bin/bash
# 加载 omm 用户的环境变量(包含 GAUSSHOME, PGDATA, LD_LIBRARY_PATH 等)
source /home/omm/.bashrc
# 等待网络稳定后才启动
sleep 10
# 启动 openGauss 集群(使用 gs_om 统一管理,更安全)
/data/opengauss/om/script/gs_om -t start
# 或者直接启动数据节点(二选一,推荐 gs_om)
# /data/opengauss/app/bin/gs_ctl start -D /data/opengauss/data/dn
赋予执行权限:
chmod +x /home/omm/bin/start_opengauss.sh
- 3.2 在 crontab 中调用脚本
执行 crontab -e,添加:
@reboot /home/omm/bin/start_opengauss.sh > /home/omm/start_opengauss.log 2>&1
这样可以将启动日志输出到文件,方便排查问题。
- 保存并验证
-
查看 crontab 是否添加成功:
crontab -l -
重启测试(如果没有重启权限,可模拟测试):
# 手动执行脚本,验证能否正常启动数据库 /home/omm/bin/start_opengauss.sh gs_om -t status --detail
四、验证数据库运行
# 以 omm 用户登录数据库
[omm@opengauss-server openGauss]$ gsql -d postgres -p 5432
gsql ((openGauss 6.0.3 build 4e5c48e7) compiled at 2025-12-23 21:31:25 commit 0 last mr 8753 )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
# 执行简单查询
openGauss=# SELECT version();
version
--------------------------------------------------------------------------------------------------------------------------------------
---------------------
(openGauss 6.0.3 build 4e5c48e7) compiled at 2025-12-23 21:31:25 commit 0 last mr 8753 on x86_64-unknown-linux-gnu, compiled by g++
(GCC) 10.3.0, 64-bit
(1 row)
openGauss=#
openGauss=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+-------+----------+-------------+-------------+-------------------
postgres | omm | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | omm | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/omm +
| | | | | omm=CTc/omm
template1 | omm | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/omm +
| | | | | omm=CTc/omm
(3 rows)
openGauss=# \q
[omm@opengauss-server openGauss]$
# 查看端口
[omm@opengauss-server openGauss]$ ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 30000 192.168.100.174:5432 0.0.0.0:*
LISTEN 0 30000 127.0.0.1:5432 0.0.0.0:*
LISTEN 0 30000 192.168.100.174:5433 0.0.0.0:*
LISTEN 0 30000 127.0.0.1:5433 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 30000 [::1]:5432 [::]:*
LISTEN 0 30000 [::1]:5433 [::]:*
[omm@opengauss-server openGauss]$
五、数据库备份
OpenGauss 支持多种备份方式,下面分别演示逻辑备份(gs_dump)和物理备份(gs_basebackup)。
5.1 逻辑备份(以数据库 postgres 为例)
逻辑备份用于导出特定数据库或表结构及数据,适合迁移或归档。
# 导出整个数据库 postgres 为 SQL 脚本
gs_dump -U omm -p 5432 -f /opengauss/backup/postgres_backup.sql postgres
# 可选:压缩备份
gs_dump -U omm -p 5432 -F c -f /opengauss/backup/postgres_backup.dump postgres
参数说明:
-U:数据库用户名-p:端口-f:输出文件路径-F:格式(c 为自定义压缩格式,p 为纯文本)
5.2 物理备份(全量)
物理备份使用 gs_basebackup 工具,生成数据库文件的完整副本,可用于快速恢复。
# 创建备份目录
mkdir -p /opengauss/backup/phys_base
# 执行全量物理备份
gs_basebackup -U omm -p 5432 -D /opengauss/backup/phys_base -F p -X stream -P -v
参数说明:
-D:备份目标目录-F p:输出格式为 plain(目录格式)-X stream:备份期间包含 WAL 日志-P:显示进度-v:显示详细信息
六、数据库恢复
6.1 逻辑备份恢复
从 SQL 脚本恢复:
# 创建新数据库用于恢复(示例)
gsql -U omm -p 5432 -d postgres -c "CREATE DATABASE restored_db;"
# 恢复数据
gsql -U omm -p 5432 -d restored_db -f /opengauss/backup/postgres_backup.sql
如果备份文件为压缩格式(.dump):
gs_restore -U omm -p 5432 -d restored_db /opengauss/backup/postgres_backup.dump
6.2 物理备份恢复
物理恢复通常需要停止数据库服务,替换数据目录,然后重启。
# 停止数据库集群
gs_om -t stop
# 备份当前数据目录(可选)
mv /opengauss/data /opengauss/data_old
# 将物理备份复制到数据目录
cp -r /opengauss/backup/phys_base /opengauss/data
# 修改数据目录权限
chown -R omm:dbgrp /opengauss/data
# 启动数据库
gs_om -t start
# 检查恢复后的状态
gs_om -t status
注意:如果备份时使用了
-X stream,恢复后数据应已包含最新日志,无需额外操作。若需要恢复至特定时间点,可结合归档日志进行 PITR 恢复(本文不展开)。
七、备份恢复验证
为了确保备份和恢复操作的有效性,我们设计一个简单验证流程:
- 原始数据:在数据库中创建测试表和记录。
- 执行备份。
- 模拟数据损坏:删除或修改测试表。
- 执行恢复。
- 验证数据:检查恢复后数据是否与备份前一致。
7.1 逻辑备份验证步骤
# 以 omm 用户执行
gsql -U omm -p 5432 -d postgres
-- 创建测试表并插入数据
CREATE TABLE test_tbl (id INT, name VARCHAR(50));
INSERT INTO test_tbl VALUES (1, 'Alice'), (2, 'Bob'), (3, 'Charlie');
SELECT * FROM test_tbl;
\q
# 执行逻辑备份
gs_dump -U omm -p 5432 -f /opengauss/backup/test_backup.sql postgres
# 模拟数据损坏:删除测试表
gsql -U omm -p 5432 -d postgres -c "DROP TABLE test_tbl;"
# 恢复数据
gsql -U omm -p 5432 -d postgres -f /opengauss/backup/test_backup.sql
# 验证数据是否恢复
gsql -U omm -p 5432 -d postgres -c "SELECT * FROM test_tbl;"
若查询结果与之前一致,则验证通过。
7.2 物理备份验证步骤
物理备份需要停止数据库,替换数据目录。我们可以快速模拟:
# 停止数据库
gs_om -t stop
# 备份当前数据目录(模拟损坏前的状态)
mv /opengauss/data /opengauss/data_orig
# 使用之前已创建的物理备份恢复
cp -r /opengauss/backup/phys_base /opengauss/data
chown -R omm:dbgrp /opengauss/data
# 启动数据库
gs_om -t start
# 检查测试表是否存在及数据是否完整
gsql -U omm -p 5432 -d postgres -c "SELECT * FROM test_tbl;"
如果恢复后数据与备份前一致,说明物理备份恢复成功。
八、日常备份建议
- 逻辑备份:适合定期备份特定数据库或表,建议与业务低峰期执行。
- 物理备份:适合全量数据保护,结合归档日志可实现任意时间点恢复。
- 备份保留策略:至少保留最近 7 天备份,每月做一次全量备份并异地存储。
- 监控:使用 crontab 定时执行备份脚本,并记录日志,通过邮件或监控系统告警。
示例定时任务(crontab -e):
# 每天凌晨2点执行逻辑备份
0 2 * * * /home/omm/scripts/logical_backup.sh >> /opengauss/log/backup.log 2>&1
# 每周日凌晨3点执行物理全量备份
0 3 * * 0 /home/omm/scripts/phys_backup.sh >> /opengauss/log/backup.log 2>&1
九、总结
本文档详细介绍了在 openEuler 22.03 系统上部署 OpenGauss 数据库的完整流程,包括环境准备、安装、验证、备份与恢复操作。通过逻辑备份与物理备份的实践验证,确保在数据灾难时能够快速恢复。OpenGauss 凭借其高性能、高可靠性和安全特性,可作为企业级应用的核心数据库选择。
DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐

所有评论(0)