逻辑数据备份

逻辑备份可以进行数据库的逻辑备份,其中ud_dump可以按需导出指定的库表等数据,ud_dumpall备份所有数据; 逻辑备份不会锁表导致无法读或写,但是会阻塞DDL操作;

ud_dumpall

可以备份整个udb实例中所有的数据,包括角色和表空间定义 示例

ud_dumpall -h 127.0.0.1 -U unvdb -p 5678 > bk-all.sql
#导出所有数据到bk-all.sql
ud_dumpall -h 127.0.0.1 -U unvdb --port=5678 -f globals.sql --globals-only
#仅备份角色和表空间定义到文件globals.sql
ud_dumpall -h 127.0.0.1 -U unvdb --port=5678 -f schema.sql -s
#仅备份表结构

详细参数

[root@udb-81 ~]# ud_dumpall --help
ud_dumpall extracts a UnvDB database cluster into an SQL script file.
Usage:
  ud_dumpall [OPTION]...
General options:
  -f, --file=FILENAME          output file name
  -v, --verbose                verbose mode
  -V, --version                output version information, then exit
  --lock-wait-timeout=TIMEOUT  fail after waiting TIMEOUT for a table lock
  -?, --help                   show this help, then exit
Options controlling the output content:
  -a, --data-only              dump only the data, not the schema
  -c, --clean                  clean (drop) databases before recreating
  -E, --encoding=ENCODING      dump the data in encoding ENCODING
  -g, --globals-only           dump only global objects, no databases
  -O, --no-owner               skip restoration of object ownership
  -r, --roles-only             dump only roles, no databases or tablespaces
  -s, --schema-only            dump only the schema, no data
  -S, --superuser=NAME         superuser user name to use in the dump
  -t, --tablespaces-only       dump only tablespaces, no databases or roles
  -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
  --exclude-database=PATTERN   exclude databases whose name matches PATTERN
  --extra-float-digits=NUM     override default setting for extra_float_digits
  --if-exists                  use IF EXISTS when dropping objects
  --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-role-passwords          do not dump passwords for roles
  --no-security-labels         do not dump security label assignments
  --no-subscriptions           do not dump subscriptions
  --no-sync                    do not wait for changes to be written safely to disk
  --no-tablespaces             do not dump tablespace assignments
  --no-toast-compression       do not dump TOAST compression methods
  --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
  --use-set-session-authorization
                               use SET SESSION AUTHORIZATION commands instead of
                               ALTER OWNER commands to set ownership
Connection options:
  -d, --dbname=CONNSTR     connect using connection string
  -h, --host=HOSTNAME      database server host or socket directory
  -l, --database=DBNAME    alternative default database
  -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 -f/--file is not used, then the SQL script will be written to the standard
output.

ud_dump

备份指定数据 示例

ud_dump -h localhost -U unvdb test1 > /backup/test1.sql
#备份test1库的数据
ud_dump -h localhost -U unvdb test1 -t tb2 > /backup/test1-tb2.sql
#只备份test1库的tb2表数据
ud_dump -h localhost -U unvdb test1 -t tb2 -t tb1 > /backup/test1-tb.sql
#只备份test1库的tb2 和tb1 表数据
ud_dump -h localhost -U unvdb test1 -s > /backup/test1-s.sql
#只导出表结构,不导出数据
ud_dump -h localhost -U unvdb test1 -a > /backup/test1-d.sql
#只导出数据,不导出表结构

详细参数

[root@udb-81 ~]# ud_dump --help
ud_dump dumps a database as a text file or to other formats.
Usage:
  ud_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
  -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, --extension=PATTERN      dump the specified extension(s) only
  -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-foreign-data=PATTERN
                               include data of foreign tables on foreign
                               servers matching PATTERN
  --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-toast-compression       do not dump TOAST compression methods
  --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

ud_sql 方式恢复数据

创建数据库

unvdb=# create database test1;
CREATE DATABASE

导入数据

ud_sql -h localhost  -U unvdb test1 < /backup/test1.sql

验证结果

unvdb=# \c test1;
unvdb=# \c test1;
You are now connected to database "test1" as user "unvdb".
test1=# select * from tb1;
 id |                                                 time                                                 | con_id | ins_id |      
     select_ip            |           server_ip            
----+------------------------------------------------------------------------------------------------------+--------+--------+------
--------------------------+--------------------------------
  1 | 2022-06-10 11:37:50.808206+08                                                                        |      1 |      1 | aa   
                          | 127.0.0.1/32                  
(1 row)
test1=# select * from tb2;
 id |                                                 time                                                 | con_id | ins_id |      
     select_ip            |           server_ip            
----+------------------------------------------------------------------------------------------------------+--------+--------+------
--------------------------+--------------------------------
  1 | 2022-06-10 11:52:29.412243+08                                                                        |      1 |      1 | tb2  
                          | 127.0.0.1/32                  
(1 row)

ud_restore 方式恢复数据

注:当dump时选用参数 -F c 备份为二进制格式时,只能用ud_restore恢复 ud_dump -F c -f /backup/test1.c -h 127.0.0.1 -U unvdb test1

创建数据库

unvdb=# create database test1;
CREATE DATABASE

恢复全部数据

ud_restore -d test1 /backup/test1.c

恢复部分数据

ud_restore -l -f /tmp/test1.toc /backup/test1.c
#从备份文件中恢复toc文件
vi /tmp/test1.toc
#修改toc,把不想恢复的行用;注释
ud_restore -F c -L /tmp/test1.toc -d test1 /backup/test1.c
#根据toc文件,恢复没有注释的部分数据