更新时间:2026-07-28 GMT+08:00
分享

备份恢复

简介

备份是通过复制数据库文件到指定的备份位置的过程,主要为了防止因硬件故障、人为误删、勒索攻击等场景数据丢失,从而可以更好地保护数据安全。恢复是将备份数据还原到数据库系统的过程。

备份方式分类:

  • 物理备份:是指复制数据库的物理文件到指定的备份位置,不涉及数据库内部逻辑结构,备份速度快,但备份文件不可读,适合大规模数据库。
  • 逻辑备份:是指将数据库中的逻辑对象(表、索引、触发器等)以逻辑关联的格式导出。备份文件可读性好,方便查看,但备份速度较慢。

恢复方式的分类:

  • 物理恢复:把原数据库中复制的物理数据文件恢复到新数据库中的过程。
  • 逻辑恢复:把数据库中导出的数据文件(SQL语句或可读的文本格式)再导入原来的数据库中的过程。

备份恢复的作用:

  • 数据安全

防止因硬件故障、误操作或勒索攻击导致的数据丢失,保障数据完整性。

  • 业务连续性

通过快速恢复减少停机时间,满足金融、医疗等行业的高可用要求。

  • 合规性

支持数据长期保留与审计,符合行业监管要求。

示例

全量物理备份数据库。

--准备工作。
cd $GPHOME/script  #切换至roach工具路径。

--查询集群状态。
cm_ctl query -Cv
[  CMServer State   ]

node             instance state
---------------------------------
1  pekphis351063 1        Primary
2  pekphis351064 2        Standby
3  pekphis351065 3        Standby

[   Cluster State   ]

cluster_state   : Normal
redistributing  : No
balanced        : Yes
current_az      : AZ_ALL
...
只有集群状态正常时,备份才有意义,从上面回显可以看出,集群状态是正常的。

--登录数据库,写入数据。
gaussdb=# create table test(id int);
CREATE TABLE
gaussdb=# insert into test values (GENERATE_SERIES(1,100000));
INSERT 0 100000

--开始备份。
python3 GaussRoach.py -t backup --master-port 6000 --media-destination /home/userA/media --media-type DISK --metadata-destination /home/userA/metadata
Parsing the configuration file.

Roach master is ready to accept connections. Starting agents now...
Performing presetup activities...

N0 pekphis351063: 100% COMPLETED
N1 pekphis351064: 100% COMPLETED
N2 pekphis351065: 100% COMPLETED

[MASTER]Backup operation SUCCESSFUL. Backup key: 20250819_161344
[MASTER] Time taken : 26s

Successfully send signal to kill all gs_roach.
Cleaning temp files after backup or restore.
Successfully cleaned temp files after backup or restore.
Roach operation backup completed.

用户必须设置相关必选参数来保证备份操作的成功完成。

全量物理恢复数据库。
--恢复数据库。
python3 GaussRoach.py -t restore --clean --master-port 6000 --media-destination /home/userA/media --media-type DISK --backup-key 20250819_161344 --metadata-destination /home/userA/metadata
Cleaning cluster.
Parsing the configuration file.
Stopping cluster.
Successfully stopped cluster.
Cleaning data before restoration.
Successfully cleaned data before restoration.
Successfully cleaned cluster.

Roach master is ready to accept connections. Starting agents now...
Performing presetup activities...

N0 pekphis351063: 100% COMPLETED
N1 pekphis351064: 100% COMPLETED
N2 pekphis351065: 100% COMPLETED

[MASTER] Restore SUCCESSFUL.
[MASTER] Time taken : 17s

Cleaning temp files after backup or restore.
Successfully cleaned temp files after backup or restore.
Roach operation restore completed.

用户必须设置相关必选参数来保证恢复操作的成功完成。

--恢复完成后,启动数据库。
python3 GaussRoach.py -t start
Stopping the cluster.
Successfully stopped the cluster.
Pre-cleaning ETCD before 'restore-new-cluster' startup...
Successfully pre-cleaned ETCD.
Starting the cluster.
Starting DN master.
Successfully started DN master.
Starting CN.
Successfully started CN.
Starting ETCD.
Successfully started ETCD.
Starting GTM.
Successfully started GTM.
Starting build DN standby.
Successfully start DN standby.
Starting DN dummy standby.
Successfully start DN dummy standby.
Updating catalog phase 1.
Successfully updated catalog phase 1.
Stoping DN instances.
Successfully stop DN instances.
Starting DN master.
Successfully started DN master.
Starting DN standby.
Successfully start DN standby.
Starting CN.
Successfully started CN.
Starting CN.
Successfully started CN.
Updating catalog phase 2.
Successfully updated catalog phase 2.
Starting CN.
Successfully started CN.
Stopping DN instances.
Successfully stop DN instances.
Starting the cluster.
Successfully started primary instance. Please wait for standby instances.
.
Successfully started standby instances.
Waiting for the cluster become normal...
Stopping the cluster.
Successfully stopped the cluster.
Starting the cluster.
Successfully started primary instance. Please wait for standby instances.
.
Successfully started standby instances.
======================================================================
cluster_state      : Normal
redistributing     : No
node_count         : 3
Coordinator State
    normal         : 3
    abnormal       : 0
GTM State
    primary        : 1
    standby        : 2
    abnormal       : 0
    down           : 0
Datanode State
    primary        : 3
    standby        : 6
    secondary      : 0
    building       : 0
    abnormal       : 0
    down           : 0
Successfully started the cluster.
Cleaning backup slot after restore.
Roach operation start completed.
--再次登录数据库验证数据是否完整。
gaussdb=# \d
                                        List of relations
 Schema | Name | Type  | Owner |                             Storage                              
--------+------+-------+-------+------------------------------------------------------------------
 public | test | table | zwb2  | {orientation=row,compression=no,storage_type=USTORE,segment=off}
(1 row)

--查询数据。
gaussdb=# select count(*) from test;
 count  
--------
 100000
(1 row)

另请阅读

若要了解更多的备份恢复相关的操作,另请阅读:

《参考》中“工具参考 > 数据导入导出工具”章节。

相关文档