做此工具的目的: 有的时候客户没有备份,在数据库出问题的时候修复起来难度和时间成本太高。这个工具就是为了快速处理故障的今早恢复生产而来。
备份项-
保留页
-
pe
-
onstat -d
-
onstat -l
-
partation页
-
dbschema
-
系统表:sysdatabses,systabnames,systables
oncheck -pr > pr
rootpath=$(onstat -d |awk '$2==1 && $3==1 {print $NF}' )
rootpgsize=$(onstat -d |awk '$2==1 && $4==1 {print $6}')
dd if=$rootpath of=pr0 skip=0 seek=0 bs=$rootpgsize count=1
dd if=$rootpath of=pr1 skip=1 seek=0 bs=$rootpgsize count=1
dd if=$rootpath of=pr2 skip=2 seek=0 bs=$rootpgsize count=1
dd if=$rootpath of=pr3 skip=3 seek=0 bs=$rootpgsize count=1
dd if=$rootpath of=pr4 skip=4 seek=0 bs=$rootpgsize count=1
dd if=$rootpath of=pr5 skip=5 seek=0 bs=$rootpgsize count=1
dd if=$rootpath of=pr6 skip=6 seek=0 bs=$rootpgsize count=1
dd if=$rootpath of=pr7 skip=7 seek=0 bs=$rootpgsize count=1
dd if=$rootpath of=pr8 skip=8 seek=0 bs=$rootpgsize count=1
dd if=$rootpath of=pr9 skip=9 seek=0 bs=$rootpgsize count=1
dd if=$rootpath of=pr10 skip=10 seek=0 bs=$rootpgsize count=1
dd if=$rootpath of=pr11 skip=11 seek=0 bs=$rootpgsize count=1
dd if=$rootpath of=pr12 skip=12 seek=0 bs=$rootpgsize count=1
备份pe
oncheck -pe > pe
tmpdbs=$(onstat -d |grep "TBA" |awk '{printf("%s|",$NF)}'|sed 's/|$//')
cat pe |grep -E "TBLSpace| /" |grep -vE "$tmpdbs" |grep -v "DBspace Usage"|awk '{
if ($2~///){path=$2; pgsize=$3*1024}
if ($1~/TBLSpace/) {print path,pgsize,$2,$3}
}' > TBLSpace.list
cat TBLSpace.list |while read path pgsize begin len
do
i=$begin
let end=${begin}+${len}
name=$(echo $path|awk -F '/' '{print $NF}')
while [ $i -lt $end ]; do
dd if=$path of=${name}_${i} skip=$i seek=0 bs=$pgsize count=1
let i=$i+1
done
done
备份onstat -d
onstat -d > on_d备份onstat -l
onstat -l > on_l onstat -c > on_c onstat -g env > on_env备份sysdatabases
echo "unload to masterDatabas.unl select * from sysdatabases;" |dbaccess sysmaster echo "unload to masterDblocal.unl select * from sysdbslocale;" |dbaccess sysmaster备份systabnames
echo "unload to masterTabnames.unl select * from systabnames;" |dbaccess sysmaster备份库结构dbschema和systables
cat masterDatabas.unl | awk -F | '{print $1}' |while read dbname
do
dbschema -d $dbname -ss SQL_${dbname}.sql
echo "unload to Tables_${dbname}.unl select * from systables;" |dbaccess $dbname
done
合并脚本版本 备份核心数据
#!/bin/bash
onmode -c
#保留页
oncheck -pr > pr
rootpath=$(onstat -d |awk '$2==1 && $3==1 {print $NF}' )
rootpgsize=$(onstat -d |awk '$2==1 && $4==1 {print $6}')
dd if=$rootpath of=pr0 skip=0 seek=0 bs=$rootpgsize count=1
dd if=$rootpath of=pr1 skip=1 seek=0 bs=$rootpgsize count=1
dd if=$rootpath of=pr2 skip=2 seek=0 bs=$rootpgsize count=1
dd if=$rootpath of=pr3 skip=3 seek=0 bs=$rootpgsize count=1
dd if=$rootpath of=pr4 skip=4 seek=0 bs=$rootpgsize count=1
dd if=$rootpath of=pr5 skip=5 seek=0 bs=$rootpgsize count=1
dd if=$rootpath of=pr6 skip=6 seek=0 bs=$rootpgsize count=1
dd if=$rootpath of=pr7 skip=7 seek=0 bs=$rootpgsize count=1
dd if=$rootpath of=pr8 skip=8 seek=0 bs=$rootpgsize count=1
dd if=$rootpath of=pr9 skip=9 seek=0 bs=$rootpgsize count=1
dd if=$rootpath of=pr10 skip=10 seek=0 bs=$rootpgsize count=1
dd if=$rootpath of=pr11 skip=11 seek=0 bs=$rootpgsize count=1
dd if=$rootpath of=pr12 skip=12 seek=0 bs=$rootpgsize count=1
#pe和patration
oncheck -pe > pe
tmpdbs=$(onstat -d |grep "TBA" |awk '{printf("%s|",$NF)}'|sed 's/|$//')
cat pe |grep -E "TBLSpace| /" |grep -vE "$tmpdbs" |grep -v "DBspace Usage"|awk '{
if ($2~///){path=$2; pgsize=$3*1024}
if ($1~/TBLSpace/) {print path,pgsize,$2,$3}
}' > TBLSpace.list
cat TBLSpace.list |while read path pgsize begin len
do
i=$begin
let end=${begin}+${len}
name=$(echo $path|awk -F '/' '{print $NF}')
while [ $i -lt $end ]; do
dd if=$path of=${name}_${i} skip=$i seek=0 bs=$pgsize count=1
let i=$i+1
done
done
#环境
onstat -d > on_d
onstat -l > on_l
onstat -c > on_c
onstat -g env > on_env
#全局库
echo "unload to masterDatabas.unl select * from sysdatabases;" |dbaccess sysmaster
echo "unload to masterDblocal.unl select * from sysdbslocale;" |dbaccess sysmaster
echo "unload to masterTabnames.unl select * from systabnames;" |dbaccess sysmaster
#每个库
cat masterDatabas.unl | awk -F | '{print $1}' |while read dbname
do
dbschema -d $dbname -ss SQL_${dbname}.sql
echo "unload to Tables_${dbname}.unl select * from systables;" |dbaccess $dbname
done
C语言版本 dumpTBLSall页
#include#include #include char* itoa(int num,char* str,int radix) { char index[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; unsigned unum; int i=0,j,k; if(radix==10&&num<0) { unum=(unsigned)-num; str[i++]='-'; } else unum=(unsigned)num; do { str[i++]=index[unum%(unsigned)radix]; unum/=radix; }while(unum); str[i]=' '; if(str[0]=='-') k=1; else k=0; char temp; for(j=k;j<=(i-1)/2;j++) { temp=str[j]; str[j]=str[i-1+k-j]; str[i-1+k-j]=temp; } return str; } void writePE(){ system("oncheck -pe > pe ; sleep 1"); } void wstrtok(char *str, char s[2], int n, char *list[]){ int i = 0; list[0]=strtok(str,s); while(list[i] != NULL){ list[++i] = strtok(NULL,s); } for(i=0; i if(list[i] != NULL){ //printf("%d: %sn",i,list[i]); } } } void dumpAllTBLS(){ FILE *pe; pe = fopen("pe","r"); if (pe == NULL){ fputs("打开文件出错!n",stderr); exit(1); } char h[1024]; char path[200]; int pgsize; int begin ; int lens ; while (!feof(pe)){ fgets(h,1025,pe); if( ferror(pe) ){ break; } if(strstr(h,"TBLSpace") || strstr(h," /")){ if(!strstr(h,"(0x")){ if(strstr(h," /")){ char *list[6]; wstrtok(h," ",6,list); strcpy(path,list[1]); pgsize = atoi(list[2]); } if(strstr(h,"TBLSpace")){ //printf("%s",h); char *ltbl[3]; wstrtok(h," ",3,ltbl); begin = atoi(ltbl[1]); lens = atoi(ltbl[2]); printf("#%s %d %d %dn",path,pgsize,begin,lens); int i = begin; int end = begin + lens; char string[1024] ; strcpy(string,path); const char sd[2] = "/"; char *name; char *lastname; name = strtok(string,sd); while( name != NULL ){ lastname=name; name = strtok(NULL,sd); } strcat(lastname,"_"); //printf("%s====%s=====%s====n",name,lastname,path); char strc[200] = {0} ; char strt[200] = {0} ; char pgsizes[20] ; char is[20]; system("mkdir -p pag; mkdir -p txt; "); //itoa(pgsize,pgsizes,16); sprintf(pgsizes,"%d",pgsize*1024); while ( i < end ){ //itoa(i,is,16); sprintf(is,"%d",i); strcpy(strc,"dd if="); strcat(strc,path); strcat(strc," of=pag/"); strcat(strc,lastname); strcat(strc,is); strcat(strc," skip="); strcat(strc,is); strcat(strc," bs="); strcat(strc,pgsizes); strcat(strc," count=1"); //printf("%sn",strc); system(strc); strcpy(strt,"xxd pag/"); strcat(strt,lastname); strcat(strt,is); strcat(strt," > txt/"); strcat(strt,lastname); strcat(strt,is); strcat(strt,".txt"); //printf("%sn",strt); system(strt); i++; } } } } } } int main(){ writePE(); dumpAllTBLS(); system("tar -zcvf pag.tar.gz pag/"); system("tar -zcvf txt.tar.gz txt/"); return 0; }



