现在的位置: 首页 > 综合 > 正文

DB2跨平台备份与恢复

2018年04月26日 ⁄ 综合 ⁄ 共 11543字 ⁄ 字号 评论关闭
DB2跨平台备份与恢复

一、DB29.1 For AIX操作系统数据库跨平台迁移至DB29.1 For Windows操作系统的方法
由于跨平台进行数据库的备份,因此需要使用db2move和db2look命令,对不同平台的DB2数据进行转移.
首先在AIX操作系统下,进行数据库的备份.
运行db2命令窗口
db2cmd
运行
db2命令,进入命令行处理器界面,在此界面下,可以执行db2的日常命令.
Db2->
Db2move nc5011 export
Db2look –d nc5011 –a –e –x –o create.sql

    …………

数据库备份后,会生成上述文件.
重构数据库时,如果想修改表的SCHEMA和TABLESPACE,可以直接修改create.sql文件中tablespace脚本和db2move.lst文件中的schema脚本, 即修改示例中的db2admin为指定的schema即可。
db2move.lst的内容示例:
!"DB2ADMIN"."EBANK_DFDK_LOG_H"!tab250.ixf!tab250.msg!
!"DB2ADMIN"."EBANK_DFDK_LOG_R"!tab251.ixf!tab251.msg!
说明:
db2admin 表示schema名称
ebank_dfdk_loh_h  表示导出的数据库表的名称
tab250.ixf  表示导出的数据文件
tab251.msg  表示导出数据时的输出信息

Db2look参数说明:
    -d DBname :数据库名称,这是必须的.
    -e :抽取数据库对象的DDL,这个参数是必须的.对象包括Tables |Views | Automatic summary tables (AST) | Aliases | Indexes |
Triggers | Sequences | User-defined distinct types | Primary key, referential integrity, and check constraints | User-defined structured types | User-defined functions | User-defined methods |
User-defined transforms | Wrappers | Servers | User mappings | Nicknames | Type mappings |
Function templates | Function mappings | Index specifications | 7 Stored procedures.
注意抽取的stored procedures的没有明细内容
    -u Creator :指定用户.如果和-a同时使用,-u被忽略.
    -z schema :指定模式.如果和-a同时使用,-z被忽略.
    -t Tname1 Tname2 ... TnameN :表的名称.N的最大数是30个,如果要区分大小写格式如"my_table",否则会认为是MY_TABLE.如果table name间有空格还有小写字符格式如""My Table""
    -tw Tname :模糊查询表的名称.通配符-代表任意字符,%代表任意好多个字符及字符串.使用-tw参数时,-t参数会被忽略.
    -v Vname1 Vname2 ... VnameN :视图名称.N的最大数是30个.-t的参数被指定时,-v参数会被忽略.
    -h :help
    -o :输出到文件的名称.
    -a :所有的用户和模式.-a -u都没有指定时,使用当前登陆用户.
    -m :生成对表的列表和索引统计的update语句.-m被指定时, -p, -g, and -s被忽略.
    -c :不生成COMMIT, CONNECT and CONNECT |RESET 语句.-c需要和-m联合使用,如果没有指定-m ,-c不起作用.
    -r :不生成 RUNSTATS command.-r需要和-m联合使用,如果没有指定-m ,-r不起作用.
    -l :定义表空间的DDL.User-defined table spaces | User-defined database partition groups | User-defined buffer pools.
    -x :生成授权的DDL.
    -xd:表/视图上的权限语句
    -f :数据库和“数据库管理器”配置参数
    -td :指定定界符,默认的为分号;.使用时-td后加空格 如:-td @.
    -noview :If this option is specified, CREATE VIEW DDL statements will not be |extracted
    -i :userid.当远程登陆时使用.
    -w assword 远程登陆的用户密码.

db2move 参数说明:
*** Please read the DB2 Administration Guide for more info!

--------db2move - database movement tool--------

Usage: "db2move <dbname> <action> [options]"

  <dbname> is the name of the database.

  <action> is one of:
     EXPORT - Export all tables that meet the filtering criteria in [options]
              (If no [options] specified, export all tables).
              Internal staging information is stored in file 'db2move.lst'.
              Messages are stored in 'EXPORT.msg'.

     IMPORT - Import all tables listed in the staging file 'db2move.lst'.
              Messages are stored in 'IMPORT.msg'..
              Use the -io option for IMPORT specific actions.

     LOAD   - Load all tables listed in the staging file 'db2move.lst'.
              Messages are stored in 'LOAD.msg'.
              Use the -lo option for LOAD specific actions.

     COPY   - Duplicate a schema(s) into a target database.
              Use the -sn option to specify one or more schemas.
              See the -co option for COPY specific options.
              Use the -tn or -tf option to filter tables in LOAD_ONLY mode.
              Messages are stored in 'COPY.<timestamp>.msg'
              Error only messages in 'COPY.<timestamp>.err'
              Load messages in 'LOADTABLE.<timestamp>.msg'
              List of tables that failed Load in 'LOADTABLE.<timestamp>.err'

  [options] are one or more of:

    Option      Description                  Use for   Notes
                                             Action
  ------------------------------------------------------------------------
  -tc <value>  Table Definers. Filter on     EXPORT   Comma seperated list,
                SYSCAT.TABLES.DEFINER                 Wildcard (*) allowed.

  -tn <value>  Table Names. Filter on        EXPORT   Comma seperated list,
                SYSCAT.TABLES.TABNAME        COPY     Wildcard (*) allowed for
                                                      EXPORT (not COPY).

  -sn <value>  Schema Names. Filter on       EXPORT,  Comma seperated list,
                SYSCAT.TABLES.TABSCHEMA      COPY     Wildcard (*) allowed for
                                                      EXPORT (not COPY).

  -ts <value>  Tablespace Names. Filter on   EXPORT   Comma seperated list,
                SYSCAT.TABLES.TBSPACE                 Wildcard (*) allowed.

  -tf <file>   Fully qualified list of       EXPORT   one table per line,like:
                table names in <file>        COPY      "MYSCHEMA1"."TAB1"
                                                       "MY SCHEMA2"."TAB2"

  -aw          Allow warnings                EXPORT   Don't disclude tables
                                                      that encounter warnings.

  -io <value>  IMPORT specific actions.      IMPORT   One of: INSERT,
               Default is REPLACE_CREATE               INSERT_UPDATE, REPLACE,
               (see docs for limitations               CREATE, REPLACE_CREATE
                of Import create function)

  -lo <value>  LOAD specific options.        LOAD     One of: INSERT,
                Default is INSERT                      REPLACE

  -l <path>    Path to lobfiles.             EXPORT,  Comma seperated list,
                Default is cwd               IMPORT,  no space characters
                                             LOAD

  -u <value>   Userid to connect to db.      ALL
                Default is current user

  -p <value>   Password to connect to db.    ALL

  -co <opt>    COPY specific options.        COPY     See options listed below.

      TARGET_DB <dbname> [USER <userid> [USING <passwd>]
         This mandatory option specifies the target database (and optional
         user/passwd) where the schema(s) will be created. (Default
         user/passwd are current user/passwd).
      MODE <value>
         The mode is one of DDL_AND_LOAD (default), DDL_ONLY, or LOAD_ONLY.
      SCHEMA_MAP <value>
         To rename the schema on the target db. For example, to rename schema
         'srcsch1' to 'tgtsch1', and schema 'srcsch2' to 'tgtsch2', use:
         "((srcsch1,tgtsch1), (scrsch2,tgtsch2))".
      TABLESPACE_MAP <value>
         To override tablespaces on the target db. For example, to
         recreate all tables that reside in TS1 on the source db, into TS2 on
         the target db, and all other tablespaces to use the default tablespace
         on the target, use: "((TS1,TS2), SYS_ANY)".
      NONRECOVERABLE
         To override the default Load recoverability action COPY-NO, to
         NONRECOVERABLE.
      OWNER <owner-name>
         To override the owner of each new object created on the target db.

Examples:
  1) Use db2move to Export all tables:
      >db2move dbname EXPORT
  2) Use db2move to Export all tables, filtering on table-name with 'mytab*'
     and table-definer with 'USER1', and lobs in /bigfs/mydir/:
      >db2move dbname EXPORT -tn mytab* -tc USER1 -l /bigfs/mydir/
  3) Use db2move to Import (recreate and repopulate) all previously exported
     tables:
      >db2move dbname IMPORT
  4) Use db2move to Load (repopulate) all previously exported tables:
      >db2move dbname LOAD
  5) Use db2move to Load (append) all previously exported tables, where
     lobfiles reside in /bigfs/mydir:
      >db2move dbname LOAD -lo INSERT -l /bigfs/mydir
  6) Use db2move to duplicate schema 'schema1' from source database 'dbsrc'
     to target database 'dbtgt':
      >db2move dbsrc COPY -sn schema1 -co TARGET_DB dbtgt
        USER myuser1 USING mypass1
  7) Use db2move to duplicate schema 'schema1' from source database 'dbsrc'
     to target database 'dbtgt', rename the schema to 'newschema1' on the
     target, and map source tablespace 'ts1' to 'ts2' on the target:
     >db2move dbsrc COPY -sn schema1 -co TARGET_DB dbtgt
    USER myuser1 USING mypass1 SCHEMA_MAP ((schema1,newschema1))
    TABLESPACE_MAP ((ts1,ts2), SYS_ANY))

在目标系统中,恢复数据库时,由于备份数据库和目标数据库的信息不一致。如:在执行ddl文件时,ddl文件中连接数据库的信息,根据目标系统需要进行相应的修改。如要导入的数据库名与原数据库不同,要修改creatab.sql中CONNECT 项
如相同则不用更改。

在db2命令窗口中,运行以下命令即可
db2 connect to nc5011 user *** using ***
db2look –tf create.sql
db2move nc5011 load                   ------db2move nc5011 import

先执行建库脚本,即批量执行create.sql,再导入数据,事先在目标库上建好对象,然后再导入数据。
如果先执行db2move nc5011 import 或者db2move nc5011 load
db2move时系统虽然会自动建表,但是也仅限于表,表上的视图和索引还需要自行创建。
所以需要在导完数据后,用脚本创建索引和视图,以及授权等操作。这样可以防止破坏表的完整性。

在执行上述命令时,需要注意load 和 import的区别.
在使用中,使用import进行数据导入时,当存在外建给时,会校验主表和子表的约束,必须先导入子表,再导入主表,否则无法导入主表的数据。
如果直接执行load命令,但不再对外键约束进行检查,直接对所有表的数据进行导入。
因此建议在导入数据时,可以优先考虑使用load命令,进行数据的导入。

Db2move在使用export,import或load,进行数据迁移时,可能会发生破坏表完整性约束的情况,需要执行set integrity for <tablename> immediate checked消除表check pending的状态。
如:
db2 connect to nc5011 user *** using ***

db2 SET INTEGRITY FOR DB2ADMIN.ebank_dfdk_h_r GENERATED COLUMN, FOREIGN KEY, MATERIALIZED QUERY, CHECK, STAGING FULL ACCESS IMMEDIATE UNCHECKED
如果导入表较多时,可以把命令编辑成批命令,进行批量执行。
如果仍然存在完整性不完整的情况,可以执行如下命令。
db2 reorg table DB2ADMIN.ebank_dfdk_log_h allow read access

  
批命令执行语法如下:
db2命令行窗口中,连接目标系统中的数据库。
Db2 connect to nc5011 user *** using ***
Alltables.bat
Reorgalltables.bat

二、DB2数据库配置文件批量修改说明
由于在搭建NC环境中,需要对新建数据库参数进行修改,而通过GUI进行修改,决不如通过批命令批量修改效率高。
对于需要修改的参数,编制批命令,直接在db2cmd中进行执行。
update dbm cfg using aslheapsz  256;
update dbm cfg using sheapthres  20000 ;
update dbm cfg using maxagents  100;
update dbm cfg using NUM_POOLAGENTS  30;
update database configuration for nc5011 using DBHEAP  4096;
update database configuration for nc5011 using logbufsz  512;
update database configuration for nc5011 using CATALOGCACHE_SZ  1024;
update database configuration for nc5011 using locklist  4096;
update database configuration for nc5011 using app_ctl_heap_sz  2048;
update database configuration for nc5011 using sortheap  2048;
update database configuration for nc5011 using stmtheap  2048;
update database configuration for nc5011 using applheapsz  1024;
update database configuration for nc5011 using pckcachesz  10240;
update database configuration for nc5011 using maxlocks  18;
update database configuration for nc5011 using NUM_IOCLEANERS  3;
update database configuration for nc5011 using NUM_IOSERVERS  3;
update database configuration for nc5011 using MAXAPPLS  50;
update database configuration for nc5011 using AVG_APPLS  30;
update database configuration for nc5011 using logfilsiz  4096;
update database configuration for nc5011 using logprimary  20;
update database configuration for nc5011 using logsecond 200;
update database configuration for nc5011 using MINCOMMIT  2;

三、NC在DB2下批量建立数据库和表空间,提高搭建环境的效率

四、常用问题分析
在使用DB2的过程中,当出现问题时,如果快速定位问题原因,是我们解决问题最有效的方式。DB2自身提供了一套Db2错误代码,以及引用此错误的相关原因的内置信息。可以通过错误相关原因,快速定位错误类型,及时提供相应的解决方案。
我们可以通过db2 ? SQL(错误代码)  命令查询错误原因。

Eg 4.1NC在db2环境下进行升级客户化的过程中,出现1585错误,导致无法升级成功
解决方案,此错误代码经查询是由于系统临界时表空间定义了4k的页大小,新建系统临界时表空间为16k页大小,解决了升级问题。
Cocuments and SettingsAdministrator>db2 ? SQL01585N

SQL1585N 不存在具有足够页大小的系统临时表空间
          。

解释:

可能发生了下列其中一种情况:

1.    系统临时表的行长度超过了数据库中最大系统
    临时表空间中可接受的限制。

2.    系统临时表中所需的列数超过了数据库中最大
    系统临时表空间中可接受的限制。

系统临时表空间限制取决于其页大小。这些值是

  最大         最大   临时
  记录         列数   表空间的
  长度                页大小
  -----------  ----  ------------
  1957  字节   244   2K
  4005  字节   500   4K
  8101  字节   1012  8K
  16293 字节   1012  16K
  32677 字节   1012  32K

用户响应:

创建受支持的更大页大小的系统临时表空间(如
果还没有)。
若已存在这种表空间,则从系统临时表中消去一列
或多列。 按需要创建独立表或视图来存放超出限
制的更多信息。

sqlcode :  -1585

sqlstate :  54048

4.2 在NC系统中做付款结算单网上转账业务操作时,系统报“Non-atomic batch failure.  The batch was submitted, but at least one exception occurred on an individual member of the batch. Use getNextException() to retrieve the exceptions for specific batched elements.”?
解决方案:日志信息显示DB2错误代码为668,当在sql中输入sql信息后,在DB2命令处理器中输入后,仍然报668错误,故可以判断是由于数据库表不完整所致。
执行以下命令,问题解决。
db2 SET INTEGRITY FOR DB2ADMIN.ebank_dfdk_h_r GENERATED COLUMN, FOREIGN KEY, MATERIALIZED QUERY, CHECK, STAGING FULL ACCESS IMMEDIATE UNCHECKED
db2 reorg table DB2ADMIN.ebank_dfdk_log_h allow read access

日志信息如下:
[http-127.0.0.1-8888-Processor21] 127.0.0.1 23373 2008/01/23 10:07:28 [nc.itf.fi.ejb.FIArapPrivateServiceEJB] ERROR - <>throws Non-atomic batch failure.  The batch was submitted, but at least one exception occurred on an individual member of the batch. Use getNextException() to retrieve the exceptions for specific batched elements.
[http-127.0.0.1-8888-Processor21] 127.0.0.1 23373 2008/01/23 10:07:28 [nc.itf.fi.ejb.FIArapPrivateServiceEJB] ERROR - sql original exception
com.ibm.db2.jcc.b.rg: Non-atomic batch failure.  The batch was submitted, but at least one exception occurred on an individual member of the batch. Use getNextException() to retrieve the exceptions for specific batched elements.
        at com.ibm.db2.jcc.b.j.a(j.java:397)
        at com.ibm.db2.jcc.b.tf.b(tf.java:3009)
        at com.ibm.db2.jcc.b.tf.a(tf.java:2778)
        at com.ibm.db2.jcc.b.tf.executeBatch(tf.java:2590)
。。。。。。
。。。。。。
。。。。。。
[http-127.0.0.1-8888-Processor21] 127.0.0.1 23373 2008/01/23 10:07:28 [nc.itf.fi.ejb.FIArapPrivateServiceEJB] ERROR - execute batch exception
com.ibm.db2.jcc.b.SqlException: Error for batch element #0: DB2 SQL error: SQLCODE: -668, SQLSTATE: 57016, SQLERRMC: 7;DB2ADMIN.EBANK_DFDK_LOG_H

当把后台输出信息调为debug后,输出debug信息中的sql日志,在db2命令编辑器中执行,也仍然报错,可以判断完全是数据库表数据不一致引起。

抱歉!评论已关闭.