• 【KingbaseES】sys_dump命令详解及示例


    概述

    sys_dump 是一个将 KingbaseES 数据库保存到一个脚本或者归档文件中的工具.这个脚本文件的格式是纯文本,它包含许多 SQL 命令, 这些 SQL 命令可以用于重建该数据库并将之恢复到保存成脚本的时候的状态.要恢复这些脚本,使用 psql。 它们甚至可以用于在其它机器甚至是其它硬件体系的机器上 重建该数据库,通过对脚本进行一些修改,甚至可以在其它 SQL 数据库产品 上重建该数据库. 另外,还有候选的归档文件格式可以和 sys_restore 一起使用重建数据库, 并且它们也允许 sys_restore 对恢复什么东西进行选择,或者甚至是在恢复之前对需要恢复的条目进行重新排序. 归档文件也是设计成可以跨平台移植的. sys_dump 将保存用于重建所有用户定义的类型,函数,表,索引聚集和操作符所必须的信息.另外, 所有数据是用文本格式拷贝出来的,因而也可以很容易地拷贝回去, 也很容易用工具编辑. 如 果一种候选文件格式和 sys_restore 结合,那么sys_dump就能提供一种灵活的归档和传输机制. sys_dump 可以用于备份整个数据库, 然后就可以使用 sys_restore 检查 这个归档和/或选择要恢复的数据库部分. 最灵活等输出文件格式是"custom(客户化)"格式 (-Fc).

    它允许对归档元素进行选取和重新排列,并且缺省时是压缩的.tar 格式 (-Ft) 不是压缩的并且我们在装载等时候不可能重排列,不过它也很灵活;还有,它可以用其它工具,比如 tar 处理. 在运行 sys_dump 的时候,我们应该检查 输出,看看是否有任何警告存在(在标准错误上打印), 特别是下面列出的限制. 即使数据库在并行使用着的时候,sys_dump 也制作出连贯的备份.sys_dump 并不阻塞其它 用户对数据库的访问(读或写). 选项下面的命令行参数用于控制输出格式。

    [kingbase@node1 ~]$ sys_dump --help
    sys_dump dumps a database as a text file or to other formats.
    
    Usage:
      sys_dump [OPTION]... [DBNAME]
    
    General options:
      -f, --file=FILENAME          output file or directory name
      -F, --format=c|d|t|p         output file format (custom, directory, tar,
                                   plain text (default))
      -j, --jobs=NUM               use this many parallel jobs to dump
      -K, --key=encryptkey         encryption key
      -v, --verbose                verbose mode
      -V, --version                output version information, then exit
      -Z, --compress=0-9           compression level for compressed formats
      --lock-wait-timeout=TIMEOUT  fail after waiting TIMEOUT for a table lock
      --no-sync                    do not wait for changes to be written safely to disk
      -?, --help                   show this help, then exit
    
    Options controlling the output content:
      -a, --data-only              dump only the data, not the schema
      -b, --blobs                  include large objects in dump
      -B, --no-blobs               exclude large objects in dump
      -c, --clean                  clean (drop) database objects before recreating
      -C, --create                 include commands to create database in dump
      -E, --encoding=ENCODING      dump the data in encoding ENCODING
      -n, --schema=PATTERN         dump the specified schema(s) only
      -N, --exclude-schema=PATTERN do NOT dump the specified schema(s)
      -O, --no-owner               skip restoration of object ownership in
                                   plain-text format
      -s, --schema-only            dump only the schema, no data
      -S, --superuser=NAME         superuser user name to use in plain-text format
      -t, --table=PATTERN          dump the specified table(s) only
      -T, --exclude-table=PATTERN  do NOT dump the specified table(s)
      -x, --no-privileges          do not dump privileges (grant/revoke)
      --binary-upgrade             for use by upgrade utilities only
      --column-inserts             dump data as INSERT commands with column names
      --disable-dollar-quoting     disable dollar quoting, use SQL standard quoting
      --disable-triggers           disable triggers during data-only restore
      --enable-row-security        enable row security (dump only content user has
                                   access to)
      --exclude-table-data=PATTERN do NOT dump data for the specified table(s)
      --extra-float-digits=NUM     override default setting for extra_float_digits
      --if-exists                  use IF EXISTS when dropping objects
      --include-all-partitions     dump all partitions of the partitioned table
      --inserts                    dump data as INSERT commands, rather than COPY
      --load-via-partition-root    load partitions via the root table
      --no-comments                do not dump comments
      --no-publications            do not dump publications
      --no-security-labels         do not dump security label assignments
      --no-subscriptions           do not dump subscriptions
      --no-synchronized-snapshots  do not use synchronized snapshots in parallel jobs
      --no-tablespaces             do not dump tablespace assignments
      --no-unlogged-table-data     do not dump unlogged table data
      --on-conflict-do-nothing     add ON CONFLICT DO NOTHING to INSERT commands
      --quote-all-identifiers      quote all identifiers, even if not key words
      --rows-per-insert=NROWS      number of rows per INSERT; implies --inserts
      --section=SECTION            dump named section (pre-data, data, or post-data)
      --serializable-deferrable    wait until the dump can run without anomalies
      --snapshot=SNAPSHOT          use given snapshot for the dump
      --strict-names               require table and/or schema include patterns to
                                   match at least one entity each
      --use-set-session-authorization
                                   use SET SESSION AUTHORIZATION commands instead of
                                   ALTER OWNER commands to set ownership
    
    Connection options:
      -d, --dbname=DBNAME      database to dump
      -h, --host=HOSTNAME      database server host or socket directory
      -p, --port=PORT          database server port number
      -U, --username=NAME      connect as specified database user
      -w, --no-password        never prompt for password
      -W, --password           force password prompt (should happen automatically)
      --role=ROLENAME          do SET ROLE before dump
    
    If no database name is supplied, then the KINGBASE_DATABASE environment
    variable value is used.
    
    Report bugs to <kingbase-bugs@kingbase.com.cn>.
    [kingbase@node1 ~]$ 
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81

    中文详解

    [kingbase@node1 ~]$ sys_dump --help
    sys_dump dumps a database as a text file or to other formats.
    
    Usage:
      sys_dump [OPTION]... [DBNAME]
    
    General options:
      -f, --file=FILENAME          输出到指定的文件中。FILENAME是文件名比如test.sql,记住一定要跟上后缀
      -F, --format=c|d|t|p         选择输出的格式(默认为TXT),
      							   p 是 plain 的意思,为纯文本 sql 脚本文件格式,为默认格式。
    							   c 是 custom 的意思,以一个适合 sys_restore 使用的自定义格式输出并归档。这是最灵活的输出格式,在该										                
    							     格式中允许手动查询并且可以在 sys_restore 恢复时重排归档项的顺序。该格式默认是压缩的。
                                   t 是 tar 的意思,以一个适合输入 sys_restore 的 tar 格式输出并归档。该格式允许手动选择并且在恢复时重排
                                     序归档项的顺序,但这个重排序是有限制的,比如,表数据项的相关顺序在恢复时不能更改。同时,tar 格式不支持
                                     压缩,并且对独立表的大小限制为 8GB。
                                   d 是diretory的意思,输出一个适合作为sys_restore输入的目录格式归档。这将创建一个目录,其中每个被转储的表
                                     和大对象都有一个文件,外加一个所谓的目录文件,该文件以一种sys_restore能读取的机器可读格式描述被转储的
                                     对象。一个目录格式归档能用标准 Unix 工具操纵,例如一个未压缩归档中的文件可以使用gzip工具压缩。这种格式
                                     默认情况下是被压缩的并且也支持并行转储。
      -j, --jobs=NUM               通过同时归档njobs个表来运行并行转储。这个选项缩减了转储的时间,但是它也增加了数据库服务器上的负载。你只能
      							   和目录输出格式一起使用这个选项,因为这是唯一一种让多个进程能在同一时间写其数据的输出格式。
      -K, --key=encryptkey         encryption key
      -v, --verbose                执行过程中打印更详细的信息。使用此选项后,sys_dump 将输出详细的对象评注及输出文件的启停时间和进度信息(输
      							   出到标准错误上)。
      -V, --version                输出 sys_dump 版本并退出。
      -Z, --compress=0-9           指定要使用的压缩级别,0表示不压缩。对于自定义归档格式,改参数指定压缩的单个表数据段,并且默认用中等水平压
      							   缩。对于纯文本输出,设置一个非零的压缩级别会导致全部输出文件被压缩,默认不压缩。
      							   tar 归档格式目前不支持压缩。
      --lock-wait-timeout=TIMEOUT  等待表锁的时间,超时失败。
      --no-sync                    不等待立即更改安全地写入磁盘。
      -?, --help                   显示帮助并退出。
    
    Options controlling the output content:
      -a, --data-only              只对纯文本(--format=t)格式有意义,只输出数据,不输出数据定义的sql。
      -b, --blobs                  在输出中是否包含大对象。除非指定了选择性输出的选项 --schema、–table、–schema-only 开关,否则默认会输
      							   出大对象。
      -B, --no-blobs               不包含大对象。
      -c, --clean                  只对纯文本(--format=t)格式有意义。指定输出的脚本中是否生成清理该数据库对象语句(如drop table 命
      							   令)。
      -C, --create                 只对纯文本格式有意义。指定脚本中是否输出一条 create database 语句和连接到该数据库的语句。一般在备份的源
      							   数据库和目标数据库的名称一致时,才指定此参数。
      -E, --encoding=ENCODING      以指定的字符集编码创建输出,默认为数据库字符集。
      -n, --schema=PATTERN         只输出匹配 schema 的模式内容,如果没有指定这个选项,目标数据库中所有非系统模式都会被输出。可以使用多个 
                                   -n 指定多个模式,也可以使用通配符匹配多个模式,使用通配符时最好用引号进行界定,防止 shell 将通配符进行
                                   扩展。
      -N, --exclude-schema=PATTERN 排除模式输出。优先级低于 -n。
      -O, --no-owner               只对纯文本(--format=t)格式有意义,表示不把对象的所有权设置为对应源端数据库中的owner,为了让脚本可以被
      							   任何用户使用。
      -s, --schema-only            只输出对象定义(模式),不输出数据。这个选项在备份表结构或在另一个数据库上创建相同结构的表时比较有用。
      -S, --superuser=NAME         指定关闭触发器时需要用到的超级用户名。它只在使用了 --disable-triggers 时才有作用。一般情况下最好不要输
      							   入这个参数,而是用超级用户启动生成的脚本。
      -t, --table=PATTERN          只输出匹配表、视图、序列,可以使用多个 -t 匹配多个表,也可以匹配通配符,使用通配符时最好用引号进行界定,防
      							   止shell将通配符进行扩展。优先级高于 -n。
      -T, --exclude-table=PATTERN  排除表输出。优先级低于 -t。
      -x, --no-privileges          禁止输出访问权限(grant/revoke命令)。
      --binary-upgrade             专为升级工具准备的。
      --column-inserts             显式指定字段名输出数据(insert into table(column1,……)values(……))。
      --disable-dollar-quoting     关闭美元符号界定函数体。强制函数体内容用 SQL 标准的字符串语法的引号包围。
      --disable-triggers           仅对纯文本(--format=t)格式有意义,只和创建仅有数据的输出相关。在恢复数据时,临时关闭目标表上的触发器。
      							   目前,发出 --disable-triggers命令的必须是超级用户,执行输出脚本时,应用 -S 执行一个超级用户的名称。
      --enable-row-security        启用行安全性(只转储用户能够访问的内容)
      --exclude-table-data=PATTERN 不转储指定名称的表中的数据
      --extra-float-digits=NUM     在转储浮点数据时使用extra_float_digits的指定值控制浮点数据精度,而不是使用最大可用精度。
      --if-exists                  时间条件性命令(即增加一个IF EXISTS子句)来清除数据库和其他对象。 只有同时指定了–clean时,这个选项才可
      							   用。
      --include-all-partitions     在单独备份一个分区表时,同时备份该分区表下所有分区的结构和数据。
      --inserts                    insert命令输出数据,默认使用 copy 命令输出数据,这个选项主要用于将数据加载到非 PostgreSql 数据库。该
      							   选项为每一行生成一个 insert 命令,因此恢复时非常缓慢,但当数据库恢复时遇到一行错误时,它将仅丢失一行数据
      							   而不是全部的表内容。若目标端字段与源端字段顺序不一致,恢复可能完全失败,此时可使用 --column-inserts 选
      							   项。
      --load-via-partition-root    通过根表加载分区。
      --no-comments                不转储注释。
      --no-publications            不转储订阅。
      --no-security-labels         不转储安全标签。
      --no-subscriptions           不导出逻辑复制订阅端的数据订阅定义.
      --no-synchronized-snapshots  这个选项允许对KingbaseESV8R3以前的服务器运行sys_dump -j,详见-j参数的文档。
      --no-tablespaces             仅对纯文本格(--format=t)式有意义,不输出命令来选择表空间。
      --no-unlogged-table-data     不转储非日志记录表的内容。这个选项对于表定义(模式)是否被转储没有影响,它只会限制转储表数据。当从一个后备
      							   服务器转储时,在非日志记录表中的数据总是会被排除。
      --on-conflict-do-nothing     此选项自动将ON CONFLICT DO NOTHING子句分配给输出INSERT语句。 必须在使用–inserts选项或
      							   –column-inserts选项指定。
      --quote-all-identifiers      强制引用所有标识符。当从PostgreSQL主版本与sys_dump不同的服务器上转储一个数据库时或者当输出准备载入到一个
      							   具有不同主版本的服务器时,推荐使用这个选项。默认情况下,sys_dump只对在其主版本中是被保留词的标识符加上引
      							   号。在转储其他版本服务器时,这种默认行为有时会导致兼容性问题,因为那些版本可能具有些许不同的被保留词集合。
      							   使用–quote-all-identifiers能阻止这种问题,但代价是转储脚本更难阅读。
      --rows-per-insert=NROWS      插入的行数; 处于 --inserts 参数下。
      --section=SECTION            只转储命名节。节的名称可以是pre-data、data或post-data。这个选项可以被指定多次来选择多个节。默认是转储
      							   所有节。
      --serializable-deferrable    为转储使用一个可序列化事务,以保证所使用的快照与后来的数据库状态是一致的。但是这样做是在事务流中等待一个
      							   点,在该点上不能存在异常,这样就不会有转储失败或者导致其他事务带着serialization_failure回滚的风险。
      --snapshot=SNAPSHOT          在做一个数据库的转储时指定一个同步的快照.
      --strict-names               要求每一个模式(-n/–schema)和表(-t/–table)限定符匹配要转储的数据库中至少一个模式/表。注意,如果没有
      						       找到有这样的模式/表限定符匹配,即便没有–strict-names,sys_dump也将生成一个错误。
      --use-set-session-authorization
                                   使用 set session authorization 命令来代替 alter owner 命令设置所有权。
    Connection options:
      -d, --dbname=DBNAME      	   数据库名。
      -h, --host=HOSTNAME          数据库服务器地址。
      -p, --port=PORT              数据库端口号
      -U, --username=NAME          数据库用户名。
      -w, --no-password            永远不提示输入口令。
      -W, --password               强制口令提示 (自动)--role=ROLENAME              在转储前运行SET ROLE。
    
    If no database name is supplied, then the KINGBASE_DATABASE environment
    variable value is used.
    
    Report bugs to <kingbase-bugs@kingbase.com.cn>.
    [kingbase@node1 ~]$ 
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110

    示例:

    --备份数据库用户角色表空间
    sys_dumpall -f backup.sql --globals-only
    
    --备份数据库结构
    sys_dump -d cdrapp -C -s > cdrapp.sql
    tar -czvf comm.tar.gz comm.sql
    tar -xzvf comm.tar.gz
    
    --备份 syd 数据库
    sys_dump syd > syd.sql
    sys_dump -Fc syd > syd.dump
    
    
    --备份schema
    sys_dump -d chis -n icuis > chis_icuis.sql
    
    
    
    --备份表
    sys_dump -d syd -t syd.test > test.sql
    copy syd.test to '/home/kingbase/test.csv' with csv;
    
    
    --导出表定义
    sys_dump -d chis -O -s -t "finance.out_trade_order" > finance.out_trade_order.sql
    
    --断开数据库所有连接
    SELECT sys_terminate_backend(sys_stat_activity.pid)
    FROM sys_stat_activity
    WHERE datname='syd' AND pid<>sys_backend_pid();
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
  • 相关阅读:
    MySQL添加、查看、修改与删除数据
    神经网络的图像识别技术,神经网络图像角度分析
    MoveIt的【Use Cartesian Path】选项及computeCartesianPath函数
    wc命令使用指南 | 教你如何高效统计文件字数、行数和字符数
    全球最受欢迎的「数字游民」城市竟然是它?
    stm32之PWM呼吸灯
    大数据讲课笔记1.4 进程管理
    05、GO数组与切片
    渗透测试-渗透测试常见的总结
    DDANet: Dual Decoder Attention Network for Automatic Polyp Segmentation
  • 原文地址:https://blog.csdn.net/sinat_36528886/article/details/134450141