导出数据库

openGauss支持使用gs_dump工具导出某个数据库级的内容,包含数据库的数据和所有对象定义。可根据需要自定义导出如下信息:

  • 导出数据库全量信息,包含数据和所有对象定义。

    使用导出的全量信息可以创建一个与当前库相同的数据库,且库中数据也与当前库相同。

  • 仅导出所有对象定义,包括:库定义、函数定义、模式定义、表定义、索引定义和存储过程定义等。

    使用导出的对象定义,可以快速创建一个相同的数据库,但是库中并无原数据库的数据。

  • 仅导出数据,不包含所有对象定义。

操作步骤

  1. 以操作系统用户omm登录数据库主节点。

  2. 使用gs_dump导出postgres数据库。

    gs_dump -U jack -f /home/omm/backup/postgres_backup.tar -p 8000 postgres -F t 
    Password:
    
    
**表 1**  常用参数说明
    
    <a name="zh-cn_topic_0237121169_table1781122816253"></a>
    <table><thead align="left"><tr id="zh-cn_topic_0237121169_row10837281257"><th class="cellrowborder" valign="top" width="25%" id="mcps1.2.4.1.1"><p id="zh-cn_topic_0237121169_p48382816258"><a name="zh-cn_topic_0237121169_p48382816258"></a><a name="zh-cn_topic_0237121169_p48382816258"></a>参数</p>
    </th>
    <th class="cellrowborder" valign="top" width="43.33%" id="mcps1.2.4.1.2"><p id="zh-cn_topic_0237121169_p18332812257"><a name="zh-cn_topic_0237121169_p18332812257"></a><a name="zh-cn_topic_0237121169_p18332812257"></a>参数说明</p>
    </th>
    <th class="cellrowborder" valign="top" width="31.669999999999998%" id="mcps1.2.4.1.3"><p id="zh-cn_topic_0237121169_p583122816257"><a name="zh-cn_topic_0237121169_p583122816257"></a><a name="zh-cn_topic_0237121169_p583122816257"></a>举例</p>
    </th>
    </tr>
    </thead>
    <tbody><tr id="zh-cn_topic_0237121169_row1483528152520"><td class="cellrowborder" valign="top" width="25%" headers="mcps1.2.4.1.1 "><p id="zh-cn_topic_0237121169_p3830287252"><a name="zh-cn_topic_0237121169_p3830287252"></a><a name="zh-cn_topic_0237121169_p3830287252"></a>-U</p>
    </td>
    <td class="cellrowborder" valign="top" width="43.33%" headers="mcps1.2.4.1.2 "><p id="zh-cn_topic_0237121169_p8232111218592"><a name="zh-cn_topic_0237121169_p8232111218592"></a><a name="zh-cn_topic_0237121169_p8232111218592"></a>连接数据库的用户名。</p>
    <div class="note" id="zh-cn_topic_0237121169_note02310422912"><a name="zh-cn_topic_0237121169_note02310422912"></a><a name="zh-cn_topic_0237121169_note02310422912"></a><span class="notetitle"> 说明: </span><div class="notebody"><p id="zh-cn_topic_0237121169_p152354213910"><a name="zh-cn_topic_0237121169_p152354213910"></a><a name="zh-cn_topic_0237121169_p152354213910"></a>不指定连接数据库的用户名时,默认以安装时创建的初始系统管理员连接。</p>
    </div></div>
    </td>
    <td class="cellrowborder" valign="top" width="31.669999999999998%" headers="mcps1.2.4.1.3 "><p id="zh-cn_topic_0237121169_p1583152842510"><a name="zh-cn_topic_0237121169_p1583152842510"></a><a name="zh-cn_topic_0237121169_p1583152842510"></a>-U jack</p>
    </td>
    </tr>
    <tr id="zh-cn_topic_0237121169_row199295855317"><td class="cellrowborder" valign="top" width="25%" headers="mcps1.2.4.1.1 "><p id="zh-cn_topic_0237121169_p89920588539"><a name="zh-cn_topic_0237121169_p89920588539"></a><a name="zh-cn_topic_0237121169_p89920588539"></a>-W</p>
    </td>
    <td class="cellrowborder" valign="top" width="43.33%" headers="mcps1.2.4.1.2 "><p id="zh-cn_topic_0237121169_p69431335210"><a name="zh-cn_topic_0237121169_p69431335210"></a><a name="zh-cn_topic_0237121169_p69431335210"></a>指定用户连接的密码。</p>
    <a name="zh-cn_topic_0237121169_ul194074341627"></a><a name="zh-cn_topic_0237121169_ul194074341627"></a><ul id="zh-cn_topic_0237121169_ul194074341627"><li>如果主机的认证策略是trust,则不会对数据库管理员进行密码验证,即无需输入-W选项;</li><li>如果没有-W选项,并且不是数据库管理员,会提示用户输入密码。</li></ul>
    </td>
    <td class="cellrowborder" valign="top" width="31.669999999999998%" headers="mcps1.2.4.1.3 "><p id="zh-cn_topic_0237121169_p1898043113581"><a name="zh-cn_topic_0237121169_p1898043113581"></a><a name="zh-cn_topic_0237121169_p1898043113581"></a>-W abcd@123</p>
    </td>
    </tr>
    <tr id="zh-cn_topic_0237121169_row1823810139566"><td class="cellrowborder" valign="top" width="25%" headers="mcps1.2.4.1.1 "><p id="zh-cn_topic_0237121169_p11238171316560"><a name="zh-cn_topic_0237121169_p11238171316560"></a><a name="zh-cn_topic_0237121169_p11238171316560"></a>-f</p>
    </td>
    <td class="cellrowborder" valign="top" width="43.33%" headers="mcps1.2.4.1.2 "><p id="zh-cn_topic_0237121169_p323861311565"><a name="zh-cn_topic_0237121169_p323861311565"></a><a name="zh-cn_topic_0237121169_p323861311565"></a>将导出文件发送至指定目录文件夹。如果这里省略,则使用标准输出。</p>
    </td>
    <td class="cellrowborder" valign="top" width="31.669999999999998%" headers="mcps1.2.4.1.3 "><p id="zh-cn_topic_0237121169_p11238513175619"><a name="zh-cn_topic_0237121169_p11238513175619"></a><a name="zh-cn_topic_0237121169_p11238513175619"></a>-f /home/<span id="zh-cn_topic_0237121169_text550520295319"><a name="zh-cn_topic_0237121169_text550520295319"></a><a name="zh-cn_topic_0237121169_text550520295319"></a>omm</span>/backup/<em id="zh-cn_topic_0237121169_i522734715214"><a name="zh-cn_topic_0237121169_i522734715214"></a><a name="zh-cn_topic_0237121169_i522734715214"></a>postgres</em>_backup.tar</p>
    </td>
    </tr>
    <tr id="zh-cn_topic_0237121169_row9411195215519"><td class="cellrowborder" valign="top" width="25%" headers="mcps1.2.4.1.1 "><p id="zh-cn_topic_0237121169_p84119521250"><a name="zh-cn_topic_0237121169_p84119521250"></a><a name="zh-cn_topic_0237121169_p84119521250"></a>-p</p>
    </td>
    <td class="cellrowborder" valign="top" width="43.33%" headers="mcps1.2.4.1.2 "><p id="zh-cn_topic_0237121169_p14838631464"><a name="zh-cn_topic_0237121169_p14838631464"></a><a name="zh-cn_topic_0237121169_p14838631464"></a>指定服务器所侦听的TCP端口或本地Unix域套接字后缀,以确保连接。</p>
    </td>
    <td class="cellrowborder" valign="top" width="31.669999999999998%" headers="mcps1.2.4.1.3 "><p id="zh-cn_topic_0237121169_p341117521853"><a name="zh-cn_topic_0237121169_p341117521853"></a><a name="zh-cn_topic_0237121169_p341117521853"></a>-p <span id="zh-cn_topic_0237121169_text74171210135017"><a name="zh-cn_topic_0237121169_text74171210135017"></a><a name="zh-cn_topic_0237121169_text74171210135017"></a>8000</span></p>
    </td>
    </tr>
    <tr id="zh-cn_topic_0237121169_row1362511481379"><td class="cellrowborder" valign="top" width="25%" headers="mcps1.2.4.1.1 "><p id="zh-cn_topic_0237121169_p8782872280"><a name="zh-cn_topic_0237121169_p8782872280"></a><a name="zh-cn_topic_0237121169_p8782872280"></a>dbname</p>
    </td>
    <td class="cellrowborder" valign="top" width="43.33%" headers="mcps1.2.4.1.2 "><p id="zh-cn_topic_0237121169_p1278211752814"><a name="zh-cn_topic_0237121169_p1278211752814"></a><a name="zh-cn_topic_0237121169_p1278211752814"></a>需要导出的数据库名称</p>
    </td>
    <td class="cellrowborder" valign="top" width="31.669999999999998%" headers="mcps1.2.4.1.3 "><p id="zh-cn_topic_0237121169_p12782147122820"><a name="zh-cn_topic_0237121169_p12782147122820"></a><a name="zh-cn_topic_0237121169_p12782147122820"></a>postgres</p>
    </td>
    </tr>
    <tr id="zh-cn_topic_0237121169_row1885993510341"><td class="cellrowborder" valign="top" width="25%" headers="mcps1.2.4.1.1 "><p id="zh-cn_topic_0237121169_p141711116303"><a name="zh-cn_topic_0237121169_p141711116303"></a><a name="zh-cn_topic_0237121169_p141711116303"></a>-F</p>
    </td>
    <td class="cellrowborder" valign="top" width="43.33%" headers="mcps1.2.4.1.2 "><p id="zh-cn_topic_0237121169_p9417511183018"><a name="zh-cn_topic_0237121169_p9417511183018"></a><a name="zh-cn_topic_0237121169_p9417511183018"></a>选择导出文件格式。-F参数值如下:</p>
    <a name="zh-cn_topic_0237121169_ul4489102813339"></a><a name="zh-cn_topic_0237121169_ul4489102813339"></a><ul id="zh-cn_topic_0237121169_ul4489102813339"><li>p:纯文本格式</li><li>c:自定义归档</li><li>d:目录归档格式</li><li>t:tar归档格式</li></ul>
    </td>
    <td class="cellrowborder" valign="top" width="31.669999999999998%" headers="mcps1.2.4.1.3 "><p id="zh-cn_topic_0237121169_p4417181118302"><a name="zh-cn_topic_0237121169_p4417181118302"></a><a name="zh-cn_topic_0237121169_p4417181118302"></a>-F t</p>
    </td>
    </tr>
    </tbody>
</table>
    
    
    其他参数说明请参见《工具参考》中“服务端工具 \> gs\_dump”章节。


## 示例<a name="zh-cn_topic_0237121169_zh-cn_topic_0096392335_s3e34e9e7c7e948d49d1f24d6038514c0"></a>

示例一:执行gs\_dump,导出postgres数据库全量信息,并对导出文件进行压缩,导出文件格式为sql文本格式。

gs_dump -f /home/omm/backup/postgres_backup.sql -p 8000 postgres -Z 8 -F p Password: gs_dump[port='8000'][postgres][2017-07-21 15:36:13]: dump database postgres successfully gs_dump[port='8000'][postgres][2017-07-21 15:36:13]: total time: 3793 ms


示例二:执行gs\_dump,仅导出postgres数据库中的数据,不包含数据库对象定义,导出文件格式为自定义归档格式。

gs_dump -f /home/omm/backup/postgres_data_backup.dmp -p 8000 postgres -a -F c Password: gs_dump[port='8000'][postgres][2017-07-21 15:36:13]: dump database postgres successfully gs_dump[port='8000'][postgres][2017-07-21 15:36:13]: total time: 3793 ms


示例三:执行gs\_dump,仅导出postgres数据库所有对象的定义,导出文件格式为sql文本格式。

gs_dump -f /home/omm/backup/postgres_def_backup.sql -p 8000 postgres -s -F p Password: gs_dump[port='8000'][postgres][2017-07-20 15:04:14]: dump database postgres successfully gs_dump[port='8000'][postgres][2017-07-20 15:04:14]: total time: 472 ms


示例四:执行gs\_dump,仅导出postgres数据库的所有对象的定义,导出文件格式为文本格式,并对导出文件进行加密。

gs_dump -f /home/omm/backup/postgres_def_backup.sql -p 8000 postgres --with-encryption AES128 --with-key 1234567812345678 -s -F p Password: gs_dump[port='8000'][postgres][2018-11-14 11:25:18]: dump database postgres successfully gs_dump[port='8000'][postgres][2018-11-14 11:25:18]: total time: 1161 ms

意见反馈
编组 3备份
openGauss 2024-03-19 00:45:43
取消