.. _执行还原:

==========
执行还原
==========

在执行还原之前，需要确定恢复目标点，尤其是要确定是否需要添加时间线参数限制，另外还原操作的其他说明如下所示：

**还原操作特点说明：**

-  **还原时，只能在数据库节点上进行。**

-  **还原完成数据库进入正常状态后，请及时手动清空kingbase.auto.conf文件内容。**

-  若是集群环境，做还原的数据库节点将作为集群的主节点，利用该节点重做备机（请参考  《 :ref:`金仓数据守护集群和读写分离集群使用手册`  》）。拉起集群后，可选择新的主节点。

-  还原时，需要确保kingbase进程已经停止。

-  还原时，KB_DATA目录本身不存在将由本工具创建。

-  还原时，KB_DATA目录下 **如果还有文件存在，需要--delta选项进行选择性还原。**

-  还原时，KB_DATA目录下如果没有文件存在，默认以下的还原模式均可使用。

-  还原成功之后，应该尽快进行一次全量备份。

-  还原所采用的数据库配置文件为指定（或默认最接近还原目标的）备份集中的配置文件，例如在备份集1生成后将kingbase.conf中的max_connections由100改为200，之后生成新备份集，还原时指定使用备份集1还原到新备份集后的时刻（或XID等），还原后的max_connections为备份集1中的100。


本部分包含以下章节：

- 停止数据库服务_
- 最新备份文件还原_
- 指定备份集还原_
- 指定事务ID还原_
- 指定时间点还原_
- 表空间支持_
- 集群还原_
- 备机还原_
- 还原后节点处理_


停止数据库服务
----------------

停止单机服务的示例：

.. code::

  $ /opt/Kingbase/ES/V8/Server/bin/sys_ctl -D /opt/Kingbase/ES/V8/Server/data stop
  waiting for server to shut down........... done
  server stopped


.. _停止集群服务:

停止集群服务的示例：

.. code::

  $ /home/kingbase/cluster/install/kingbase/bin/sys_monitor.sh stop
  2023-07-04 15:59:16 Ready to stop all DB ...
  2023-07-04 15:59:20 begin to stop repmgrd on "[192.168.45.152]".
  2023-07-04 15:59:21 repmgrd on "[192.168.45.152]" already stopped.
  2023-07-04 15:59:21 begin to stop repmgrd on "[192.168.45.197]".
  2023-07-04 15:59:22 repmgrd on "[192.168.45.197]" already stopped.
  2023-07-04 15:59:22 begin to stop DB on "[192.168.45.197]".
  waiting for server to shut down.... done
  server stopped
  2023-07-04 15:59:23 DB on "[192.168.45.197]" stop success.
  2023-07-04 15:59:23 begin to stop DB on "[192.168.45.152]".
  waiting for server to shut down.... done
  server stopped
  2023-07-04 15:59:24 DB on "[192.168.45.152]" stop success.
  2023-07-04 15:59:24 Done.



最新备份文件还原
----------------

.. code::

  $ /opt/Kingbase/ES/V8/Server/bin/sys_rman --config=/home/kingbase/kbbr_repo/sys_rman.conf --stanza=kingbase --delta restore
  2023-07-04 10:29:35.010 P00   INFO: restore command begin 2.27: --band-width=0 --config=/home/kingbase/kbbr_repo/sys_rman.conf --delta --exec-id=394476-4ab30268 --kb1-path=/opt/Kingbase/ES/V8/Server/data --link-all --lock-path=/tmp/sys_rman --log-level-console=info --log-level-file=info --log-path=/opt/Kingbase/ES/V8/Server/log --log-subprocess --non-archived-space=1024 --process-max=4 --repo1-path=/home/kingbase/kbbr_repo --stanza=kingbase
  2023-07-04 10:29:35.042 P00   INFO: repo1: restore backup set 20230628-171503F, recovery will start at 2023-06-28 17:15:03
  2023-07-04 10:29:35.045 P00   INFO: remove invalid files/links/paths from '/opt/Kingbase/ES/V8/Server/data'
  2023-07-04 10:29:35.253 P00   INFO: Restore Process: FILE: 1 / 2302 0%       SZIE: 2195456 bytes / 161069195 bytes 2MB / 153.6MB 1%
  2023-07-04 10:29:35.264 P00   INFO: Restore Process: FILE: 2 / 2302 0%       SZIE: 4390912 bytes / 161069195 bytes 4.2MB / 153.6MB 2%
  2023-07-04 10:29:35.329 P00   INFO: Restore Process: FILE: 3 / 2302 0%       SZIE: 6586368 bytes / 161069195 bytes 6.3MB / 153.6MB 4%
  ..........
  ..........
  2023-07-04 10:29:47.307 P00   INFO: Restore Process: FILE: 2300 / 2302 99%       SZIE: 161069192 bytes / 161069195 bytes 153.6MB / 153.6MB 99%
  2023-07-04 10:29:47.307 P00   INFO: Restore Process: FILE: 2301 / 2302 99%       SZIE: 161069195 bytes / 161069195 bytes 153.6MB / 153.6MB 100%
  2023-07-04 10:29:47.307 P00   INFO: Restore Process: FILE: 2302 / 2302 100%       SZIE: 161069195 bytes / 161069195 bytes 153.6MB / 153.6MB 100%
  2023-07-04 10:29:47.307 P00   INFO: write updated /opt/Kingbase/ES/V8/Server/data/kingbase.auto.conf
  2023-07-04 10:29:47.341 P00   INFO: restore global/sys_control (performed last to ensure aborted restores cannot be started)
  2023-07-04 10:29:47.341 P00   INFO: restore size = 153.6MB, file total = 2302
  2023-07-04 10:29:47.342 P00   INFO: restore command end: completed successfully (12336ms)


指定备份集还原
----------------

.. code::

  $ /opt/Kingbase/ES/V8/Server/bin/sys_rman --config=/home/kingbase/kbbr_repo/sys_rman.conf --stanza=kingbase --set='20230704-104206F_20230704-104229D' --delta restore
  2023-07-04 11:03:19.722 P00   INFO: restore command begin 2.27: --band-width=0 --config=/home/kingbase/kbbr_repo/sys_rman.conf --delta --exec-id=426334-f60d8ec6 --kb1-path=/opt/Kingbase/ES/V8/Server/data --link-all --lock-path=/tmp/sys_rman --log-level-console=info --log-level-file=info --log-path=/opt/Kingbase/ES/V8/Server/log --log-subprocess --non-archived-space=1024 --process-max=4 --repo1-path=/home/kingbase/kbbr_repo --set=20230704-104206F_20230704-104229D --stanza=kingbase
  WARN: sys_rman auto set target time to backupset stop time 2023-07-04 10:42:32
  2023-07-04 11:03:19.741 P00   INFO: repo1: restore backup set 20230704-104206F_20230704-104229D, recovery will start at 2023-07-04 10:42:29
  2023-07-04 11:03:19.748 P00   INFO: remove invalid files/links/paths from '/opt/Kingbase/ES/V8/Server/data'
  2023-07-04 11:03:19.893 P00   INFO: Restore Process: FILE: 1 / 2296 0%       SZIE: 2195456 bytes / 161042923 bytes 2MB / 153.6MB 1%
  2023-07-04 11:03:19.893 P00   INFO: Restore Process: FILE: 2 / 2296 0%       SZIE: 4390912 bytes / 161042923 bytes 4.2MB / 153.6MB 2%
  ..........
  ..........
  2023-07-04 11:03:20.381 P00   INFO: Restore Process: FILE: 2294 / 2296 99%       SZIE: 161042923 bytes / 161042923 bytes 153.6MB / 153.6MB 100%
  2023-07-04 11:03:20.581 P00   INFO: Restore Process: FILE: 2295 / 2296 99%       SZIE: 161042923 bytes / 161042923 bytes 153.6MB / 153.6MB 100%
  2023-07-04 11:03:20.581 P00   INFO: Restore Process: FILE: 2296 / 2296 100%       SZIE: 161042923 bytes / 161042923 bytes 153.6MB / 153.6MB 100%
  2023-07-04 11:03:20.581 P00   INFO: write updated /opt/Kingbase/ES/V8/Server/data/kingbase.auto.conf
  2023-07-04 11:03:20.605 P00   INFO: restore global/sys_control (performed last to ensure aborted restores cannot be started)
  2023-07-04 11:03:20.605 P00   INFO: restore size = 153.6MB, file total = 2296
  2023-07-04 11:03:20.606 P00   INFO: restore command end: completed successfully (886ms)


指定事务ID还原
----------------

.. code::

  $ /opt/Kingbase/ES/V8/Server/bin/sys_rman --config=/home/kingbase/kbbr_repo/sys_rman.conf --stanza=kingbase --type=xid --target='1060' --set='20230704-111905F_20230704-112235D' --delta restore
  2023-07-04 11:26:51.376 P00   INFO: restore command begin 2.27: --band-width=0 --config=/home/kingbase/kbbr_repo/sys_rman.conf --delta --exec-id=125177-193d984b --kb1-path=/opt/Kingbase/ES/V8/Server/data --link-all --lock-path=/tmp/sys_rman --log-level-console=info --log-level-file=info --log-path=/opt/Kingbase/ES/V8/Server/log --log-subprocess --non-archived-space=1024 --process-max=4 --repo1-path=/home/kingbase/kbbr_repo --set=20230704-111905F_20230704-112235D --stanza=kingbase --target=1060 --type=xid
  2023-07-04 11:26:51.395 P00   INFO: repo1: restore backup set 20230704-111905F_20230704-112235D, recovery will start at 2023-07-04 11:22:35
  2023-07-04 11:26:51.436 P00   INFO: remove invalid files/links/paths from '/opt/Kingbase/ES/V8/Server/data'
  2023-07-04 11:26:51.702 P00   INFO: Restore Process: FILE: 1 / 2301 0%       SZIE: 5636096 bytes / 221700852 bytes 5.4MB / 211.4MB 2%
  2023-07-04 11:26:51.705 P00   INFO: Restore Process: FILE: 2 / 2301 0%       SZIE: 7831552 bytes / 221700852 bytes 7.5MB / 211.4MB 3%
  2023-07-04 11:26:51.708 P00   INFO: Restore Process: FILE: 3 / 2301 0%       SZIE: 10027008 bytes / 221700852 bytes 9.6MB / 211.4MB 4%
  ..........
  ..........
  2023-07-04 11:26:52.806 P00   INFO: Restore Process: FILE: 2299 / 2301 99%       SZIE: 221700852 bytes / 221700852 bytes 211.4MB / 211.4MB 100%
  2023-07-04 11:26:52.806 P00   INFO: Restore Process: FILE: 2300 / 2301 99%       SZIE: 221700852 bytes / 221700852 bytes 211.4MB / 211.4MB 100%
  2023-07-04 11:26:52.906 P00   INFO: Restore Process: FILE: 2301 / 2301 100%       SZIE: 221700852 bytes / 221700852 bytes 211.4MB / 211.4MB 100%
  2023-07-04 11:26:52.906 P00   INFO: write updated /opt/Kingbase/ES/V8/Server/data/kingbase.auto.conf
  2023-07-04 11:26:53.756 P00   INFO: restore global/sys_control (performed last to ensure aborted restores cannot be started)
  2023-07-04 11:26:53.759 P00   INFO: restore size = 211.4MB, file total = 2301
  2023-07-04 11:26:53.760 P00   INFO: restore command end: completed successfully (2387ms)



指定时间点还原
----------------

.. code::

  $ /opt/Kingbase/ES/V8/Server/bin/sys_rman --config=/home/kingbase/kbbr_repo/sys_rman.conf --stanza=kingbase --type=time --target='2023-07-04 10:42:29' --delta restore
  2023-07-04 10:59:27.852 P00   INFO: restore command begin 2.27: --band-width=0 --config=/home/kingbase/kbbr_repo/sys_rman.conf --delta --exec-id=349746-8b57b6a2 --kb1-path=/opt/Kingbase/ES/V8/Server/data --link-all --lock-path=/tmp/sys_rman --log-level-console=info --log-level-file=info --log-path=/opt/Kingbase/ES/V8/Server/log --log-subprocess --non-archived-space=1024 --process-max=4 --repo1-path=/home/kingbase/kbbr_repo --stanza=kingbase --target="2023-07-04 10:42:29" --type=time
  2023-07-04 10:59:27.868 P00   INFO: repo1: restore backup set 20230704-104206F, recovery will start at 2023-07-04 10:42:06
  2023-07-04 10:59:27.886 P00   INFO: remove invalid files/links/paths from '/opt/Kingbase/ES/V8/Server/data'
  2023-07-04 10:59:28.031 P00   INFO: Restore Process: FILE: 1 / 2297 0%       SZIE: 2195456 bytes / 177817466 bytes 2MB / 169.6MB 1%
  2023-07-04 10:59:28.031 P00   INFO: Restore Process: FILE: 2 / 2297 0%       SZIE: 4390912 bytes / 177817466 bytes 4.2MB / 169.6MB 2%
  2023-07-04 10:59:28.034 P00   INFO: Restore Process: FILE: 3 / 2297 0%       SZIE: 6586368 bytes / 177817466 bytes 6.3MB / 169.6MB 3%
  2023-07-04 10:59:28.034 P00   INFO: Restore Process: FILE: 4 / 2297 0%       SZIE: 8781824 bytes / 177817466 bytes 8.4MB / 169.6MB 4%
  ..........
  ..........
  2023-07-04 10:59:28.672 P00   INFO: Restore Process: FILE: 2294 / 2297 99%       SZIE: 177817466 bytes / 177817466 bytes 169.6MB / 169.6MB 100%
  2023-07-04 10:59:28.973 P00   INFO: Restore Process: FILE: 2295 / 2297 99%       SZIE: 177817466 bytes / 177817466 bytes 169.6MB / 169.6MB 100%
  2023-07-04 10:59:28.973 P00   INFO: Restore Process: FILE: 2296 / 2297 99%       SZIE: 177817466 bytes / 177817466 bytes 169.6MB / 169.6MB 100%
  2023-07-04 10:59:28.973 P00   INFO: Restore Process: FILE: 2297 / 2297 100%       SZIE: 177817466 bytes / 177817466 bytes 169.6MB / 169.6MB 100%
  2023-07-04 10:59:28.974 P00   INFO: write updated /opt/Kingbase/ES/V8/Server/data/kingbase.auto.conf
  2023-07-04 10:59:28.980 P00   INFO: restore global/sys_control (performed last to ensure aborted restores cannot be started)
  2023-07-04 10:59:28.980 P00   INFO: restore size = 169.6MB, file total = 2297
  2023-07-04 10:59:28.981 P00   INFO: restore command end: completed successfully (1132ms)


表空间支持
----------------

备份时，表空间是默认地、隐式地包含在备份文件集中。具体操作可参考  《 :ref:`KingbaseES备份与恢复工具手册 <KingbaseES物理备份恢复工具手册>` 》。


.. _备机还原:

备机还原
----------------------

若只是集群中的某个备机出现问题或者已经完成主节点的恢复，使用repmgr standby clone 命令从主节点恢复备机即可，具体请参考  《 :ref:`金仓数据守护集群和读写分离集群使用手册` 》。

.. code::

  $ /home/kingbase/cluster/install/kingbase/bin/repmgr -h 192.168.45.152 -U esrep -d esrep -p 54321 standby clone -F --fast-checkpoint
  [NOTICE] destination directory "/home/kingbase/cluster/install/kingbase/data" provided
  [INFO] connecting to source node
  [DETAIL] connection string is: host=192.168.45.152 user=esrep port=54321 dbname=esrep
  [DETAIL] current installation size is 156 MB
  [NOTICE] checking for available walsenders on the source node (2 required)
  [NOTICE] checking replication connections can be made to the source server (2 required)
  [WARNING] directory "/home/kingbase/cluster/install/kingbase/data" exists but is not empty
  [NOTICE] -F/--isForce provided - deleting existing data directory "/home/kingbase/cluster/install/kingbase/data"
  [INFO] creating replication slot as user "esrep"
  [NOTICE] starting backup (using sys_basebackup)...
  [INFO] executing:
    /home/kingbase/cluster/install/kingbase/bin/sys_basebackup -l "repmgr base backup"  -D /home/kingbase/cluster/install/kingbase/data -h 192.168.45.152 -p 54321 -U esrep -c fast -X stream -S repmgr_slot_2
  [NOTICE] standby clone (using sys_basebackup) complete
  [NOTICE] you can now start your Kingbase server
  [HINT] for example: sys_ctl -D /home/kingbase/cluster/install/kingbase/data start
  [HINT] after starting the server, you need to re-register this standby with "repmgr standby register --force" to update the existing node record


集群还原
----------------

集群的恢复首先需要 `停止集群服务`_，然后参照下方例子利用sys_rman恢复主节点，再利用repmgr进行 `备机还原`_，恢复备节点。

.. code::

  $ /home/kingbase/cluster/install/kingbase/bin/sys_rman --config=/home/kingbase/cluster/install/kingbase/kbbr_repo/sys_rman.conf --stanza=kingbase --delta restore
  2023-07-04 19:40:38.355 P00   INFO: restore command begin 2.27: --band-width=0 --config=/home/kingbase/cluster/install/kingbase/kbbr_repo/sys_rman.conf --delta --exec-id=5251-096280d9 --kb2-host=192.168.45.197 --kb1-path=/home/kingbase/cluster/install/kingbase/data --kb2-path=/home/kingbase/cluster/install/kingbase/data --link-all --lock-path=/tmp/sys_rman --log-level-console=info --log-level-file=info --log-path=/home/kingbase/cluster/install/kingbase/log --log-subprocess --non-archived-space=1024 --process-max=4 --repo1-path=/home/kingbase/cluster/install/kingbase/kbbr_repo --stanza=kingbase
  2023-07-04 19:40:38.380 P00   INFO: repo1: restore backup set 20230704-193515F, recovery will start at 2023-07-04 19:35:15
  2023-07-04 19:40:38.380 P00   INFO: remove invalid files/links/paths from '/home/kingbase/cluster/install/kingbase/data'
  2023-07-04 19:40:38.644 P00   INFO: Restore Process: FILE: 1 / 2769 0%       SZIE: 1802240 bytes / 205336055 bytes 1.7MB / 195.8MB 0%
  2023-07-04 19:40:38.654 P00   INFO: Restore Process: FILE: 3 / 2769 0%       SZIE: 5406720 bytes / 205336055 bytes 5.2MB / 195.8MB 2%
  ..........
  ..........
  2023-07-04 19:40:39.600 P00   INFO: Restore Process: FILE: 2768 / 2769 99%       SZIE: 205336055 bytes / 205336055 bytes 195.8MB / 195.8MB 100%
  2023-07-04 19:40:39.600 P00   INFO: Restore Process: FILE: 2769 / 2769 100%       SZIE: 205336055 bytes / 205336055 bytes 195.8MB / 195.8MB 100%
  2023-07-04 19:40:39.601 P00   INFO: write updated /home/kingbase/cluster/install/kingbase/data/kingbase.auto.conf
  2023-07-04 19:40:39.604 P00   INFO: restore global/sys_control (performed last to ensure aborted restores cannot be started)
  2023-07-04 19:40:39.605 P00   INFO: restore size = 195.8MB, file total = 2769
  2023-07-04 19:40:39.606 P00   INFO: restore command end: completed successfully (1256ms)


集群还原后，确认数据是否符合预期，观察应用或手动的数据操作是否同步到备节点以验证集群还原是否成功。


还原后节点处理
----------------

可参考  《 :ref:`KingbaseES备份与恢复工具手册 <KingbaseES物理备份恢复工具手册>` 》，最后启动数据库服务：

单机启动示例：

.. code::

  $ /opt/Kingbase/ES/V8/Server/bin/sys_ctl -D /opt/Kingbase/ES/V8/data start
  waiting for server to start....2023-07-04 17:14:13.066 CST [6091] LOG:  sepapower extension initialized
  2023-07-04 17:14:13.070 CST [6091] LOG:  starting KingbaseES V008R006 on x86_64-unknown-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-36), 64-bit
  2023-07-04 17:14:13.072 CST [6091] LOG:  listening on IPv4 address "0.0.0.0", port 54321
  2023-07-04 17:14:13.072 CST [6091] LOG:  listening on IPv6 address "::", port 54321
  2023-07-04 17:14:13.079 CST [6091] LOG:  listening on Unix socket "/tmp/.s.KINGBASE.54321"
  2023-07-04 17:14:13.278 CST [6091] LOG:  redirecting log output to logging collector process
  2023-07-04 17:14:13.278 CST [6091] HINT:  Future log output will appear in directory "sys_log".
   done
  server started


集群启动示例：

.. code::

  $ /home/kingbase/cluster/install/kingbase/bin/sys_monitor.sh start
  2023-07-04 19:55:12 Ready to start all DB ...
  2023-07-04 19:55:12 begin to start DB on "[192.168.45.152]".
  waiting for server to start.... done
  server started
  2023-07-04 19:55:13 execute to start DB on "[192.168.45.152]" success, connect to check it.
  2023-07-04 19:55:14 DB on "[192.168.45.152]" start success.
  2023-07-04 19:55:14 Try to ping trusted_servers on host 192.168.45.152 ...
  2023-07-04 19:55:16 Try to ping trusted_servers on host 192.168.45.197 ...
  2023-07-04 19:55:19 begin to start DB on "[192.168.45.197]".
  waiting for server to start.... done
  server started
  2023-07-04 19:55:20 execute to start DB on "[192.168.45.197]" success, connect to check it.
  2023-07-04 19:55:22 DB on "[192.168.45.197]" start success.
   ID | Name  | Role    | Status    | Upstream | Location | Priority | Timeline | LSN_Lag | Connection string                                                                                                                                                   
  ----+-------+---------+-----------+----------+----------+----------+----------+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
   1  | node1 | primary | * running |          | default  | 100      | 1        |         | host=192.168.45.152 user=esrep dbname=esrep port=54321 connect_timeout=10 keepalives=1 keepalives_idle=2 keepalives_interval=2 keepalives_count=3 tcp_user_timeout=9000
   2  | node2 | standby |   running | node1    | default  | 100      | 1        | 0 bytes | host=192.168.45.197 user=esrep dbname=esrep port=54321 connect_timeout=10 keepalives=1 keepalives_idle=2 keepalives_interval=2 keepalives_count=3 tcp_user_timeout=9000
  2023-07-04 19:55:22 The primary DB is started.
  2023-07-04 19:55:22 begin to start repmgrd on "[192.168.45.152]".
  [2023-07-04 19:55:22] [NOTICE] using provided configuration file "/home/kingbase/cluster/install/kingbase/bin/../etc/repmgr.conf"
  [2023-07-04 19:55:22] [NOTICE] redirecting logging output to "/home/kingbase/cluster/install/kingbase/log/hamgr.log"
  
  2023-07-04 19:55:24 repmgrd on "[192.168.45.152]" start success.
  2023-07-04 19:55:24 begin to start repmgrd on "[192.168.45.197]".
  [2023-07-04 07:55:24] [NOTICE] using provided configuration file "/home/kingbase/cluster/install/kingbase/bin/../etc/repmgr.conf"
  [2023-07-04 07:55:24] [NOTICE] redirecting logging output to "/home/kingbase/cluster/install/kingbase/log/hamgr.log"
  
  2023-07-04 19:55:27 repmgrd on "[192.168.45.197]" start success.
   ID | Name  | Role    | Status    | Upstream | repmgrd | PID   | Paused? | Upstream last seen
  ----+-------+---------+-----------+----------+---------+-------+---------+--------------------
   1  | node1 | primary | * running |          | running | 10624 | no      | n/a
   2  | node2 | standby |   running | node1    | running | 19691 | no      | 1 second(s) ago
  [2023-07-04 19:55:28] [NOTICE] redirecting logging output to "/home/kingbase/cluster/install/kingbase/log/kbha.log"
  
  [2023-07-04 07:55:32] [NOTICE] redirecting logging output to "/home/kingbase/cluster/install/kingbase/log/kbha.log"
  
  2023-07-04 19:55:34 Done.


