批處理數據--db2備份數據


如果要插入數據,前提先根據主鍵刪除記錄,然后在插入。

批處理包含兩個必要文件

init.bat和start.bat

文檔內容如下

init.bat內容如下

@echo on
db2 connect to cashman user db2inst1 using '1qaz!QAZ'
db2 set schema db2inst1
db2 -td@ -vf update.sql>log.txt   //將執行結果輸出到log.txt
db2 -td@ -vf delete.sql>>log.txt      //注意,如果是>會覆蓋上一個執行結果日志,如果是>>會在上一個日志的基礎上追加
db2 -td@ -vf insert.sql>>log.txt   
db2 import from t_user.sql of del modified by codepage=1386 insert into t_user(corpid,userid)>>log.txt   //這個定義參照db2set -all設置,可以保證插入數據全部顯示

db2 commit 

db2 terminate
pause
exit

 

start.bat內容如下

@echo off
db2cmd init.bat
exit

編輯的每一個語句以@結束,insert以最后一條記錄為准后@結束

db2 import from t_user.sql of del modified by codepage=1386 insert into t_user(corpid,userid)>>log.txt 

用到這句話時,t_user.sql用記事本打開

"武金晶","010184","901010200",5,"120108198502280023","13662003683","965EB72C92A549DD","01"
"強雅薇","012111","901010200",5,"120106199107267023","13802098032","965EB72C92A549DD","01"

無需加@,執行結果在log日志中顯示

 

備份

http://www.infoq.com/cn/presentations/zee-system-performance-analysis


-bash-3.2# db2 backup db cashman
 SQL1092N  "ROOT" does not have the authority to perform the requested command
or operation.
-bash-3.2# db2 backup db cashman to /opt
SQL1092N  "ROOT" does not have the authority to perform the requested command
or operation.
注:在aix系統中root用戶沒有權限備份db2數據庫
-bash-3.2# su -db2inst1
-db2inst1: 0403-010 A specified flag is not valid for this command.
-bash-3.2# su - db2inst1  
注:切換到db2數據庫所在的用戶
-bash-3.2$ db2 backup db cashman to /opt
SQL1035N  The database is currently in use.  SQLSTATE=57019
注:備份前先關閉數據庫
-bash-3.2$ db2stop
02/17/2015 12:32:16     0   0   SQL1025N  The database manager was not stopped because databases are still active.
SQL1025N  The database manager was not stopped because databases are still active.
-bash-3.2$ db2stop force
02/17/2015 12:41:18     0   0   SQL1064N  DB2STOP processing was successful.
SQL1064N  DB2STOP processing was successful.
-bash-3.2$ db2start
注:然后在啟動
02/17/2015 12:42:11     0   0   SQL1063N  DB2START processing was successful.
SQL1063N  DB2START processing was successful.
-bash-3.2$ db2 connect to cashman user db2inst1 using 1qaz!QAZ
-bash: !QAZ: event not found
-bash-3.2$ db2 connect to cashman user db2inst1 using '1qaz!QAZ'
SQL0104N  An unexpected token "!" was found following "<identifier>". 
Expected tokens may include:  "NEW".  SQLSTATE=42601
-bash-3.2$ db2 connect to cashman user db2inst1 using "1qaz!QAZ"
-bash: !QAZ": event not found
-bash-3.2$ db2 connect to cashman

   Database Connection Information

 Database server        = DB2/AIX64 9.7.0
 SQL authorization ID   = DB2INST1
 Local database alias   = CASHMAN

-bash-3.2$ db2 backup db cashman to /opt
SQL1035N  The database is currently in use.  SQLSTATE=57019
-bash-3.2$ db2 backup db cashman online use tsm
SQL2413N  Online backup is not allowed because the database is not recoverable
or a backup pending condition is in effect.
-bash-3.2$ db2 list applications for db cashman
注:查看當前正在使用的db2連接的情況

Auth Id  Application    Appl.      Application Id                                                 DB       # of
         Name           Handle                                                                    Name    Agents
-------- -------------- ---------- -------------------------------------------------------------- -------- -----
DB2INST1 toad.exe       25         192.168.1.42.60927.150216044739                                CASHMAN  1   
DB2INST1 toad.exe       21         192.168.1.40.62487.150216044337                                CASHMAN  1   
DB2INST1 toad.exe       7          192.168.1.40.62486.150216044332                                CASHMAN  1   

-bash-3.2$ db2stop force db2start
SQL2032N  The "db2start" parameter is not valid.  SQLSTATE=22531
-bash-3.2$ db2stop force        
02/17/2015 12:48:43     0   0   SQL1064N  DB2STOP processing was successful.
SQL1064N  DB2STOP processing was successful.
-bash-3.2$ db2 backup db cashman use tsm
SQL1032N  No start database manager command was issued.
-bash-3.2$ db2start
02/17/2015 12:49:07     0   0   SQL1063N  DB2START processing was successful.
SQL1063N  DB2START processing was successful.
-bash-3.2$ db2 backup db cashman use tsm
SQL2071N  An error occurred while accessing the shared library
"/home/db2inst1/sqllib/adsm/libtsm.a". Reason code: "1".
-bash-3.2$ db2 backup db cashman to /opt
SQL2061N  An attempt to access media "/opt" is denied.
-bash-3.2$ su - root
root's Password:
-bash-3.2# db2 backup db cashman to /opt
SQL1092N  "ROOT" does not have the authority to perform the requested command
or operation.
-bash-3.2# su - db2inst1
-bash-3.2$ cd /home
-bash-3.2$ ls
ASKeyLinux  chong       db2inst1    hegl        poka01      yang
TT_DB       dasusr1     esaadmin    lost+found  test
backup      db2fenc1    guest       poka        wqs
-bash-3.2$ db2 backup db cashman to /home
SQL2061N  An attempt to access media "/home" is denied.
-bash-3.2$ cd db2inst1
-bash-3.2$ ls
DB2         createsql   db2inst1    hegl        t_inst
bak         db.sql      export_sql  sqllib
-bash-3.2$ cd ..
-bash-3.2$ ls
ASKeyLinux  chong       db2inst1    hegl        poka01      yang
TT_DB       dasusr1     esaadmin    lost+found  test
backup      db2fenc1    guest       poka        wqs
-bash-3.2$ cd backup
-bash-3.2$ ls
mydata
-bash-3.2$ cd ..
-bash-3.2$ ll
-bash: ll: command not found
-bash-3.2$ ls -l
total 56
drwxr-xr-x    2 root     system         4096 Oct 25 2012  ASKeyLinux
drwxr-xr-x    2 root     system         4096 Jul 19 2012  TT_DB
drwxr-xr-x    3 root     system          256 Nov 21 09:19 backup
drwxr-xr-x    2 chong    system          256 Sep 04 2012  chong
drwxr-xr-x    3 dasusr1  dasadm1         256 Sep 15 09:50 dasusr1
drwxr-xr-x    2 214      102             256 May 14 2014  db2fenc1
drwxr-xr-x   10 db2inst1 db2iadm1       4096 Nov 21 10:36 db2inst1
drwxr-xr-x    2 esaadmin system          256 Feb 26 1970  esaadmin
drwxr-xr-x    3 guest    usr             256 Jun 16 2012  guest
drwxr-xr-x    9 hegl     system         4096 Sep 05 2013  hegl
drwx------    2 root     system          256 May 24 2007  lost+found
drwxr-xr-x   20 poka     staff          4096 Dec 02 2013  poka
drwxr-xr-x   11 root     system         4096 Apr 02 2013  poka01
drwxr-xr-x    3 test     staff           256 Sep 15 09:59 test
drwxr-xr-x    3 root     system         4096 Jun 25 2012  wqs
drwxr-xr-x    2 yang     system          256 Sep 04 2012  yang
-bash-3.2$ chmod +w backup
注:db2inst1用戶不能賦權限
chmod: backup: Operation not permitted.
-bash-3.2$ su - root     
root's Password:
-bash-3.2# chmod 775 /home/backup
注:root用戶需要賦權限
-bash-3.2# su - db2inst1
-bash-3.2$ db2 backup db cashman to /home/backup
SQL2061N  An attempt to access media "/home/backup" is denied.
-bash-3.2$ db2 list history backup all for cashman
注:db2歷史上備份的情況

                    List History File for cashman

Number of matching file entries = 8


 Op Obj Timestamp+Sequence Type Dev Earliest Log Current Log  Backup ID
 -- --- ------------------ ---- --- ------------ ------------ --------------
  B  D  20141015151732001   F    D  S0000000.LOG S0000000.LOG 
 ----------------------------------------------------------------------------
  Contains 4 tablespace(s):

  00001 SYSCATSPACE                                                          
  00002 USERSPACE1                                                           
  00003 MYTBS                                                                
  00004 SYSTOOLSPACE                                                         
 ----------------------------------------------------------------------------
    Comment: DB2 BACKUP CASHMAN OFFLINE                                      
 Start Time: 20141015151732
   End Time: 20141015151736
     Status: A
 ----------------------------------------------------------------------------
  EID: 5 Location: /home/db2inst1/bak


 Op Obj Timestamp+Sequence Type Dev Earliest Log Current Log  Backup ID
 -- --- ------------------ ---- --- ------------ ------------ --------------
  R  D  20141016112050001   F       S0000000.LOG S0000000.LOG 20141015151732
 ----------------------------------------------------------------------------
  Contains 4 tablespace(s):

  00001 SYSCATSPACE                                                          
  00002 USERSPACE1                                                           
  00003 MYTBS                                                                
  00004 SYSTOOLSPACE                                                         
 ----------------------------------------------------------------------------
    Comment: RESTORE CASHMAN NO RF                                           
 Start Time: 20141016112050
   End Time: 20141016112126
     Status: A
 ----------------------------------------------------------------------------
  EID: 6 Location:


 Op Obj Timestamp+Sequence Type Dev Earliest Log Current Log  Backup ID
 -- --- ------------------ ---- --- ------------ ------------ --------------
  B  D  20141114121100001   F    D  S0000000.LOG S0000000.LOG 
 ----------------------------------------------------------------------------
  Contains 4 tablespace(s):

  00001 SYSCATSPACE                                                          
  00002 USERSPACE1                                                           
  00003 MYTBS                                                                
  00004 SYSTOOLSPACE                                                         
 ----------------------------------------------------------------------------
    Comment: DB2 BACKUP CASHMAN OFFLINE                                      
 Start Time: 20141114121100
   End Time: 20141114121107
     Status: A
 ----------------------------------------------------------------------------
  EID: 13 Location: /home/db2inst1/bak


 Op Obj Timestamp+Sequence Type Dev Earliest Log Current Log  Backup ID
 -- --- ------------------ ---- --- ------------ ------------ --------------
  B  D  20150217124659000   N       S0000000.LOG              
 ----------------------------------------------------------------------------

 ----------------------------------------------------------------------------
    Comment: DB2 BACKUP CASHMAN ONLINE                                       
 Start Time: 20150217124659
   End Time: 20150217124700
     Status: A
 ----------------------------------------------------------------------------
  EID: 20 Location:

SQLCA Information

 sqlcaid : SQLCA     sqlcabc: 136   sqlcode: -2413   sqlerrml: 0

 sqlerrmc:
 sqlerrp : sqlubIni
 sqlerrd : (1) 0                (2) 0                (3) 0
           (4) 0                (5) 0                (6) 0
 sqlwarn : (1)      (2)      (3)      (4)        (5)       (6)
           (7)      (8)      (9)      (10)       (11)
 sqlstate:

 Op Obj Timestamp+Sequence Type Dev Earliest Log Current Log  Backup ID
 -- --- ------------------ ---- --- ------------ ------------ --------------
  B  D  20150217124927000   F       S0000000.LOG              
 ----------------------------------------------------------------------------
  Contains 4 tablespace(s):

  00001 SYSCATSPACE                                                          
  00002 USERSPACE1                                                           
  00003 MYTBS                                                                
  00004 SYSTOOLSPACE                                                         
 ----------------------------------------------------------------------------
    Comment: DB2 BACKUP CASHMAN OFFLINE                                      
 Start Time: 20150217124927
   End Time: 20150217124928
     Status: A
 ----------------------------------------------------------------------------
  EID: 21 Location:

SQLCA Information

 sqlcaid : SQLCA     sqlcabc: 136   sqlcode: -2071   sqlerrml: 37

 sqlerrmc: /home/db2inst1/sqllib/adsm/libtsm.a?1
 sqlerrp : sqlubMWR
 sqlerrd : (1) 0                (2) 0                (3) 0
           (4) 0                (5) 0                (6) 0
 sqlwarn : (1)      (2)      (3)      (4)        (5)       (6)
           (7)      (8)      (9)      (10)       (11)
 sqlstate:

 Op Obj Timestamp+Sequence Type Dev Earliest Log Current Log  Backup ID
 -- --- ------------------ ---- --- ------------ ------------ --------------
  B  D  20150217125001000   F       S0000000.LOG              
 ----------------------------------------------------------------------------
  Contains 4 tablespace(s):

  00001 SYSCATSPACE                                                          
  00002 USERSPACE1                                                           
  00003 MYTBS                                                                
  00004 SYSTOOLSPACE                                                         
 ----------------------------------------------------------------------------
    Comment: DB2 BACKUP CASHMAN OFFLINE                                      
 Start Time: 20150217125001
   End Time: 20150217125002
     Status: A
 ----------------------------------------------------------------------------
  EID: 22 Location:

SQLCA Information

 sqlcaid : SQLCA     sqlcabc: 136   sqlcode: -2061   sqlerrml: 4

 sqlerrmc: /opt
 sqlerrp : sqlubMWR
 sqlerrd : (1) 0                (2) 0                (3) 0
           (4) 0                (5) 0                (6) 0
 sqlwarn : (1)      (2)      (3)      (4)        (5)       (6)
           (7)      (8)      (9)      (10)       (11)
 sqlstate:

 Op Obj Timestamp+Sequence Type Dev Earliest Log Current Log  Backup ID
 -- --- ------------------ ---- --- ------------ ------------ --------------
  B  D  20150217125134000   F       S0000000.LOG              
 ----------------------------------------------------------------------------
  Contains 4 tablespace(s):

  00001 SYSCATSPACE                                                          
  00002 USERSPACE1                                                           
  00003 MYTBS                                                                
  00004 SYSTOOLSPACE                                                         
 ----------------------------------------------------------------------------
    Comment: DB2 BACKUP CASHMAN OFFLINE                                      
 Start Time: 20150217125134
   End Time: 20150217125135
     Status: A
 ----------------------------------------------------------------------------
  EID: 23 Location:

SQLCA Information

 sqlcaid : SQLCA     sqlcabc: 136   sqlcode: -2061   sqlerrml: 5

 sqlerrmc: /home
 sqlerrp : sqlubMWR
 sqlerrd : (1) 0                (2) 0                (3) 0
           (4) 0                (5) 0                (6) 0
 sqlwarn : (1)      (2)      (3)      (4)        (5)       (6)
           (7)      (8)      (9)      (10)       (11)
 sqlstate:

 Op Obj Timestamp+Sequence Type Dev Earliest Log Current Log  Backup ID
 -- --- ------------------ ---- --- ------------ ------------ --------------
  B  D  20150217125411000   F       S0000000.LOG              
 ----------------------------------------------------------------------------
  Contains 4 tablespace(s):

  00001 SYSCATSPACE                                                          
  00002 USERSPACE1                                                           
  00003 MYTBS                                                                
  00004 SYSTOOLSPACE                                                         
 ----------------------------------------------------------------------------
    Comment: DB2 BACKUP CASHMAN OFFLINE                                      
 Start Time: 20150217125411
   End Time: 20150217125412
     Status: A
 ----------------------------------------------------------------------------
  EID: 24 Location:

SQLCA Information

 sqlcaid : SQLCA     sqlcabc: 136   sqlcode: -2061   sqlerrml: 12

 sqlerrmc: /home/backup
 sqlerrp : sqlubMWR
 sqlerrd : (1) 0                (2) 0                (3) 0
           (4) 0                (5) 0                (6) 0
 sqlwarn : (1)      (2)      (3)      (4)        (5)       (6)
           (7)      (8)      (9)      (10)       (11)
 sqlstate:
-bash-3.2$ db2 backup db cashman use tsm
SQL2071N  An error occurred while accessing the shared library
"/home/db2inst1/sqllib/adsm/libtsm.a". Reason code: "1".
-bash-3.2$

-bash-3.2$ db2 backup db cashman use tsm
SQL2071N  An error occurred while accessing the shared library
"/home/db2inst1/sqllib/adsm/libtsm.a". Reason code: "1".
-bash-3.2$ db2 backup db cashman to /home/db2inst1
注:在db2用戶有權限的目錄下備份

Backup successful. The timestamp for this backup image is : 20150217130757

-bash-3.2$

備份數據庫步驟如下
-bash-3.2$ db2 backup db cashman to /home/db2inst1
SQL1035N  The database is currently in use.  SQLSTATE=57019
-bash-3.2$ db2stop force
02/17/2015 13:39:14     0   0   SQL1064N  DB2STOP processing was successful.
SQL1064N  DB2STOP processing was successful.
-bash-3.2$ db2start
02/17/2015 13:39:20     0   0   SQL1063N  DB2START processing was successful.
SQL1063N  DB2START processing was successful.
-bash-3.2$ db2 connect to cashman

   Database Connection Information

 Database server        = DB2/AIX64 9.7.0
 SQL authorization ID   = DB2INST1
 Local database alias   = CASHMAN

-bash-3.2$ db2 backup db cashman to /home/db2inst1


bash-3.2$  db2 restore db cashman incremental automatic from /home/db2inst1 taken at 20150217134041 logtarget /db2_log/cashman
SQL1035N  The database is currently in use.  SQLSTATE=57019
-bash-3.2$ db2stop force
02/17/2015 13:56:53     0   0   SQL1064N  DB2STOP processing was successful.
SQL1064N  DB2STOP processing was successful.
-bash-3.2$ db2start
02/17/2015 13:56:59     0   0   SQL1063N  DB2START processing was successful.
SQL1063N  DB2START processing was successful.
-bash-3.2$ db2 connect to cashman

   Database Connection Information

 Database server        = DB2/AIX64 9.7.0
 SQL authorization ID   = DB2INST1
 Local database alias   = CASHMAN

-bash-3.2$ db2 restore db cashman incremental automatic from /home/db2inst1 taken at 20150217134041 logtarget /db2_log/cashman
SQL2581N  Restore is unable to extract log files or restore a log directory
from the backup image to the specified path. Reason code "2".
-bash-3.2$ db2 restore db cashman incremental automatic from /home/db2inst1 taken at 20150217134041  
注:停止db2,啟動連接后,可覆蓋原數據                       
SQL2539W  Warning!  Restoring to an existing database that is the same as the
backup image database.  The database files will be deleted.
Do you want to continue ? (y/n)
Do you want to continue ? (y/n) y
DB20000I  The RESTORE DATABASE command completed successfully.

創建數據庫
C:\Users\Administrator>db2 create database cashman using codeset utf-8 territory
 cn
 注:用db2inst1的用戶創建數據庫,然后創建模式,模式隸屬於用戶的關系,在賦予權限
DB20000I  CREATE DATABASE命令成功完成。

C:\Users\Administrator>
C:\Users\Administrator>
C:\Users\Administrator>db2 connect to cashman

   數據庫連接信息

 數據庫服務器         = DB2/NT64 9.5.0
 SQL 授權標識         = ADMINIST...
 本地數據庫別名       = CASHMAN


C:\Users\Administrator>db2 restore db cashman incremental automatic from D:\ tak
en at 20150203144733
SQL2523W  警告!正在復原至與備份映像上的數據庫不同,但具有匹配名稱的現有數據庫。

目標數據庫將被備份版本覆蓋。將刪除與目標數據庫相關聯的前滾恢復日志。
想要繼續嗎?(y/n)
DB20000I  RESTORE DATABASE命令成功完成。

 db2注冊碼失效


C:\Users\Administrator>db2start
DB2-0 : 服務已返回特定的服務錯誤代碼。

2015-03-06 13:49:54     0   0   SQL8000N  DB2START 處理失敗;找不到有效的產品許
可證。如果許可您使用此產品,那么應確保正確注冊了許可證密鑰。可以通過“許可證中
”或 db2licm 命令行實用程序來注冊許可證。許可證密鑰可從您的許可產品 CD 中獲得。
SQL1032N  未發出啟動數據庫管理器的命令。  SQLSTATE=57019

注:當出現上述問題,可能是注冊碼失效了

C:\Users\Administrator>db2 connect to cashman
SQL1032N  未發出啟動數據庫管理器的命令。  SQLSTATE=57019

C:\Users\Administrator>db2licm -l      注:可用該命令進行查證
Product name:                     "DB2 Enterprise Server Edition"
Expiry date:                      "已到期"
Product identifier:               "db2ese"
Version information:              "9.5"

在網上下載db2ese_cV9.5CPU.lic,用下邊的命令加載

C:\Users\Administrator>db2licm -a D:\db2ese_cV9.5CPU.lic

LIC1402I  成功地添加了許可證。

 


LIC1426I   現在,您已獲取本產品的使用許可,詳細信息請參閱許可協議。使用此產品必
須接受 IBM 許可協議的條款,該許可協議位於以下目錄: "D:\PROGRA~1\IBM\SQLLIB\lice
nse\en"

C:\Users\Administrator>
C:\Users\Administrator>db2licm -l     注:加載完和驗證結果
Product name:                     "DB2 Enterprise Server Edition"
License type:                     "CPU 選項"
Expiry date:                      "永久"
Product identifier:               "db2ese"
Version information:              "9.5"
Features:
DB2 Database Partitioning:        "未許可"
DB2 Performance Optimization ESE: "未許可"
DB2 Storage Optimization:         "未許可"
DB2 Advanced Access Control:      "未許可"
DB2 Geodetic Data Management:     "未許可"
DB2 pureXML ESE:                  "未許可"
IBM Homogeneous Federation ESE:   "未許可"
IBM Homogeneous Replication ESE:  "未許可"

C:\Users\Administrator>db2start
2015-03-06 14:27:54     0   0   SQL1063N  DB2START 處理成功。
SQL1063N  DB2START 處理成功。

C:\Users\Administrator>db2 drop db cashman
DB20000I  DROP DATABASE命令成功完成。

C:\Users\Administrator>db2 create database cashman using codeset utf-8 territory
 cn
DB20000I  CREATE DATABASE命令成功完成。
注:用db2的用戶創建數據庫
C:\Users\Administrator>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM