truncate表后使用FY_Recover_Data恢复数据(过程内容是另一个版本)

我的未来我决定 提交于 2020-12-10 11:16:04

FY_Recover_Data的另一个版本(内容略有差异)FY_Recover_Data.sql ,源码如下:

create or replace package FY_Recover_Data is
  ---------------------------------------------------------------------------
  -- WWW.HelloDBA.COM                                                     ---
  -- Created By: Fuyuncat                                                 ---
  -- Created Date: 08/08/2012                                             ---
  -- Email: Fuyuncat@gmail.com                                            ---
  -- Coyprigh (c), 2012, WWW.HelloDBA.COM All rights reserved.            ---
  -- Last Version: http://www.HelloDBA.com/download/FY_Recover_Data.zip   ---
  --                                                                      ---
  -- Update Logs                                                          ---
  -- 15/08/2012, Fuyuncat:                                                ---
  --   1. Fixed Bug in Clean_Up_Ts (Not chage TS status correctly)        ---
  --   2. Added Exception Handle when Restore Data                        ---
  --   3. Added Parameter in recover_table,                               ---
  --            to balance Fault Tolerance and Performance                ---
  --                                                                      ---
  -- 16/08/2012, Fuyuncat:                                                ---
  --   1. Enhanced corrupted block processing, get rows as possilbe       ---
  --                                                                      ---
  -- 17/08/2012, Fuyuncat:                                                ---
  --   1. Ommit the LOB columns raised ORA-22922 exception                ---
  --                                                                      ---
  -- 20/08/2012, Fuyuncat:                                                ---
  --   1. Ommit the LOB columns via db link                               ---
  --                                                                      ---
  -- 22/08/2012, Fuyuncat:                                                ---
  --   1. Updated logging and tracing interface                           ---
  ---------------------------------------------------------------------------

  type r_cursor is REF CURSOR;
  type o_fileprop is record (
   file# number,
   status$ number);
  type t_fileprops is table of o_fileprop;

  /************************************************************************
  ** Copy file
  **
  ** srcdir: Directory of Source File;
  ** srcfile: Source File Name;
  ** dstdir: Directory of Destination File;
  ** dstfile: Destination File Name;
  ************************************************************************/
  procedure copy_file(srcdir varchar2, 
                      srcfile varchar2, 
                      dstdir varchar2 default null, 
                      dstfile varchar2 default null);

  /************************************************************************
  ** Recover Table Data From Special Data File;
  **
  ** oriobjid: Object Id of Table to be Recovered;
  ** recowner: Owner of Table to be used as recovering dummy table;
  ** rectable: Name of Table to be used as recovering dummy table;
  ** rstowner: Owner of Table to store the recovered data;
  ** rsttable: Name of Table to store the recovered data;
  ** srcdir: Directory of the Data File to be recovered;
  ** srcfile: Name of the Data File to be recovered;
  ** recdir: Directory of Data File that rectable is stored;
  ** recfile: Name of Data File that rectable is stored;
  ** copydir: Directory of Copy of Data File that rectable is stored;
  ** coryfile: Name of Copy of Data File that rectable is stored;
  ** blksz: Block size of the Tablespace Storing the Table to be recovered;
  ** selflink: database link refer to instance self connect to dba account;
  ************************************************************************/
  procedure recover_table(oriobjid number,
                          recowner varchar2, 
                          rectab varchar2, 
                          rstowner varchar2, 
                          rsttab varchar2, 
                          srcdir varchar2, 
                          srcfile varchar2, 
                          recdir varchar2, 
                          recfile varchar2, 
                          copydir varchar2, 
                          copyfile varchar2, 
                          blksz number default 8192,
                          fillblks number default 5,
                          selflink varchar2 default '',
                          endianess number default 1);

  /************************************************************************
  ** Recover Table Data From Data Files of Targe Table;
  **
  ** tgtowner: Owner of Target Table to be recovered;
  ** tgttable: Name of Target Table to be recovered;
  ** recowner: Owner of Table to be used as recovering dummy table;
  ** rectable: Name of Table to be used as recovering dummy table;
  ** rstowner: Owner of Table to store the recovered data;
  ** rsttable: Name of Table to store the recovered data;
  ** srcdir: Directory of the Data File to be recovered;
  ** srcfile: Name of the Data File to be recovered;
  ** recdir: Directory of Data File that rectable is stored;
  ** recfile: Name of Data File that rectable is stored;
  ** copydir: Directory of Copy of Data File that rectable is stored;
  ** coryfile: Name of Copy of Data File that rectable is stored;
  ** blksz: Block size of the Tablespace Storing the Table to be recovered;
  ** selflink: database link refer to instance self connect to dba account;
  ************************************************************************/
  procedure recover_table(tgtowner varchar2,
                          tgttable varchar2,
                          recowner varchar2, 
                          rectab varchar2, 
                          rstowner varchar2, 
                          rsttab varchar2, 
                          srcdir varchar2, 
                          recdir varchar2, 
                          recfile varchar2, 
                          copydir varchar2, 
                          copyfile varchar2, 
                          blksz number default 8192,
                          fillblks number default 5,
                          selflink varchar2 default '');

  /************************************************************************
  ** Prepare the data files to be use during recovering;
  **
  ** tgtowner: Owner of Target Table to be recovered;
  ** tgttable: Name of Target Table to be recovered;
  ** datapath: Absolute path of Data Files;
  ** datadir: Directory to be created referring to datapath;
  ** rects: Tablespace to store the recovering dummy table;
  ** recfile: Name of Data File to store the recovering dummy table;
  ** rstts: Tablespace to store table storing the recovered data;
  ** rstfile: Name of Data File to store restoring table;
  ** blksz: Block size of the Tablespace Storing the Table to be recovered;
  ** rectsblks: block number of recovery tablespace
  ** rectsblks: block number of restore tablespace
  ************************************************************************/
  procedure prepare_files(tgtowner varchar2,
                          tgttable varchar2,
                          datapath in varchar2,
                          datadir in out varchar2,
                          rects out varchar2, 
                          recfile out varchar2, 
                          rstts out varchar2, 
                          rstfile out varchar2, 
                          blksz out varchar2,
                          rectsblks number default 16,
                          rsttsblks number default 2560);

  /************************************************************************
  ** Clean up existing Recover and Restore Tablespace. Drop tables in the tablespaces
  **
  ** rects: Recover tablespace name
  ** rects: Restore tablespace name, default NULL, will not do cleaning up;
  ************************************************************************/
  procedure clean_up_ts(rects varchar2, 
                        rstts varchar2 default null);

  /************************************************************************
  ** Fill Blocks of Recovering Table, to format the blocks;
  **
  ** tgtowner: Owner of Target Table to be recovered;
  ** tgttable: Name of Target Table to be recovered;
  ** datadir: Directory to be created referring to datapath;
  ** rects: Tablespace to store the recovering dummy table;
  ** recfile: Name of Data File to store the recovering dummy table;
  ** rstts: Tablespace to store table storing the recovered data;
  ** blks: Number blocks in Initial Extent of the recovering dummy table;
  ** recowner: Owner of Table to be used as recovering dummy table;
  ** rstowner: Owner of Table to store the recovered data;
  ** rectable: Name of Table to be used as recovering dummy table;
  ** rsttable: Name of Table to store the recovered data;
  ** coryfile: Name of Copy of Data File that rectable is stored;
  ************************************************************************/
  procedure fill_blocks(tgtowner varchar2,
                        tgttable varchar2,
                        datadir varchar2, 
                        rects varchar2,
                        recfile varchar2,
                        rstts varchar2,
                        blks number default 8,
                        recowner varchar2 default user,
                        rstowner varchar2 default user,
                        rectab in out varchar2,
                        rsttab in out varchar2,
                        copyfile out varchar2);

  /************************************************************************
  ** Testing recovering procedure, involved initializing tablespaces and data files
  **
  ** tgtowner: Owner of Target Table to be recovered;
  ** tgttable: Name of Target Table to be recovered;
  ** datapath: Absolute path of Data Files;
  ** fbks: block number to be filled in recovery table;
  ************************************************************************/
  procedure test_rec1( tow varchar2 default 'SYS', 
                       ttb varchar2 default 'T_CHAIN',
                       fbks number default 1, 
                       datapath varchar2 default 'D:\oracle\product\10.2.0\oradata\EDGAR\DATAFILE\');

  /************************************************************************
  ** Testing recovering procedure, without initializing tablespaces and data files
  **
  ** tgtowner: Owner of Target Table to be recovered;
  ** tgttable: Name of Target Table to be recovered;
  ** fbks: block number to be filled in recovery table;
  ************************************************************************/
  procedure test_rec2( tow varchar2 default 'SYS', 
                       ttb varchar2 default 'T_CHAIN',
                       fbks number default 1);

  /************************************************************************
  ** Set Initial parameters
  **
  ** tracing: trace the process for debug;
  ** logging: show logging information;
  ** repobjid: replace the data object id wiht the recover table data object id;
  ************************************************************************/
  procedure init_set( tracing boolean default true,
                      logging boolean default true,
                      repobjid boolean default true);
end FY_Recover_Data;
/
create or replace package body FY_Recover_Data is
  ---------------------------------------------------------------------------
  -- WWW.HelloDBA.COM                                                     ---
  -- Created By: Fuyuncat                                                 ---
  -- Created Date: 08/08/2012                                             ---
  -- Email: Fuyuncat@gmail.com                                            ---
  -- Coyprigh (c), 2012, WWW.HelloDBA.COM All rights reserved.            ---
  -- Last Version: http://www.HelloDBA.com/download/FY_Recover_Data.zip   ---
  ---------------------------------------------------------------------------

  s_tracing       boolean:= true;
  s_logging       boolean:= true;
  s_repobjid      boolean:= false;

  procedure init_set (tracing boolean default true,
                      logging boolean default true,
                      repobjid boolean default true)
  as
  begin
    s_tracing := tracing;
    s_logging := logging;
    s_repobjid := repobjid;
  end;

  procedure trace (msg varchar2)
  as
  begin
    if s_tracing then
      dbms_output.put_line(to_char(sysdate, 'HH24:MI:SS')||': '||msg);
    end if;
  end;

  procedure log (msg varchar2)
  as
  begin
    if s_logging then
      dbms_output.put_line(to_char(sysdate, 'HH24:MI:SS')||': '||msg);
    end if;
  end;

  function d2r (dig varchar2,
                len number default 0)
  return raw
  is
  begin
    --trace('[d2r] hextoraw(lpad(trim(to_char('||dig||', ''XXXXXXXX'')),'||len||',''0''))');
    return hextoraw(lpad(trim(to_char(dig, 'XXXXXXXX')),len,'0'));
  end;                

  procedure copy_file(srcdir varchar2, 
                      srcfile varchar2, 
                      dstdir varchar2 default null, 
                      dstfile varchar2 default null)
  as
    bfr    utl_file.file_type;
    bfw    utl_file.file_type; 
    -- vrw    raw(8192);
    frw    raw(8192);
    bsz    number := 8192;
    --p_srcdir varchar2(255) := upper(srcdir);
    --p_srcfile varchar2(255) := upper(srcfile);
    --p_dstdir varchar2(255) := upper(dstdir);
    --p_dstfile varchar2(255) := upper(dstfile);
    p_srcdir varchar2(255) := srcdir;
    p_srcfile varchar2(255) := srcfile;
    p_dstdir varchar2(255) := dstdir;
    p_dstfile varchar2(255) := dstfile;
  begin
    if dstdir is null then
      p_dstdir := p_srcdir;
    end if;
    if p_dstfile is null then
      p_dstfile := p_srcfile||'$';
    end if;
    trace('[copy_file] begin copy file: '||srcdir||'\'||srcfile||' => '||dstdir||'\'||dstfile);
    bfr := utl_file.fopen(p_srcdir, p_srcfile, 'RB');
    bfw := utl_file.fopen(p_dstdir, p_dstfile, 'WB');
    while true loop
    begin
      utl_file.get_raw(bfr, frw, bsz);
      exit when frw is null;
      utl_file.put_raw(bfw, frw);
      utl_file.fflush(bfw);
      exception when others then
        exit;
    end;
    end loop;
    utl_file.fclose(bfw);
    utl_file.fclose(bfr);
    trace('[copy_file] completed.');
  end;

  procedure replace_segmeta_in_file(srcdir varchar2, 
                                    srcfile varchar2, 
                                    dstdir varchar2, 
                                    dstfile varchar2,
                                    tgtobjid number,
                                    newobjid number, 
                                    dtail raw,
                                    addpos number,
                                    addinfo raw,
                                    blksz number default 8192,
                                    endianess number default 1)
  as
    bfr    utl_file.file_type;
    bfw    utl_file.file_type; 
    hsz    number := 24;
    objr   raw(4);
    objn   number;
    dhead  raw(32);
    dbody  raw(32767);
    nbody  raw(32767);
    p_srcdir varchar2(255) := srcdir;
    p_srcfile varchar2(255) := srcfile;
    p_dstdir varchar2(255) := dstdir;
    p_dstfile varchar2(255) := dstfile;
  begin
    if p_dstdir is null then
      p_dstdir := p_srcdir;
    end if;
    trace('[replace_objid_in_file] replace object id in '||srcdir||'\'||srcfile||' ['||tgtobjid||' => '||newobjid||']');
    bfr := utl_file.fopen(p_srcdir, p_srcfile, 'RB');
    bfw := utl_file.fopen(p_dstdir, p_dstfile, 'WB');
    while true loop
    begin
      nbody := '';
      utl_file.get_raw(bfr, dhead, hsz);
      exit when dhead is null;
      utl_file.get_raw(bfr, dbody, blksz-hsz);
      --objr := hextoraw(substrb(rawtohex(dbody), 1, 8));
      objr := utl_raw.substr(dbody, 1, 4);
      if endianess > 0 then
        objn := to_number(rawtohex(utl_raw.reverse(objr)), 'XXXXXXXX');
      else
        objn := to_number(rawtohex(objr), 'XXXXXXXX');
      end if;
      -- replace data object id with the recover object id
      --if objn = tgtobjid and substrb(rawtohex(dhead), 1, 2) = '06' then
      if objn = tgtobjid then
        if addpos <= hsz then
          --utl_file.put_raw(bfw, utl_raw.concat(utl_raw.substr(dhead, 1, addpos-1), addinfo, utl_raw.substr(dhead, addpos+utl_raw.length(addinfo))));
          nbody := utl_raw.concat(nbody, utl_raw.substr(dhead, 1, addpos-1), addinfo, utl_raw.substr(dhead, addpos+utl_raw.length(addinfo)));
        else
          --utl_file.put_raw(bfw, dhead);
          nbody := utl_raw.concat(nbody, dhead);
        end if;
        --utl_file.put_raw(bfw, utl_raw.concat(utl_raw.substr(dhead, 1, 8), addinfo, utl_raw.substr(dhead, 9+utl_raw.length(addinfo))));
        --nbody := utl_raw.concat(nbody, utl_raw.substr(dhead, 1, 8), addinfo, utl_raw.substr(dhead, 9+utl_raw.length(addinfo)));
        --trace('[replace_objid_in_file] old id in raw: '||rawtohex(objr));
        if endianess > 0 then
          --trace('[replace_objid_in_file] new id in raw: '||utl_raw.reverse(d2r(newobjid, 8)));
          --utl_file.put_raw(bfw, utl_raw.reverse(d2r(newobjid, 8)));
          nbody := utl_raw.concat(nbody, utl_raw.reverse(d2r(newobjid, 8)));
        else
          --trace('[replace_objid_in_file] new id in raw: '||(d2r(newobjid, 8)));
          --utl_file.put_raw(bfw, d2r(newobjid, 8));
          nbody := utl_raw.concat(nbody, d2r(newobjid, 8));
        end if;
        -- skip objid
        if addpos > hsz+5 and addinfo is not null then
          trace('[replace_objid_in_file] old body len: '||utl_raw.length(dbody)||' new = 4 + '||utl_raw.length(utl_raw.substr(dbody, 5, addpos-hsz-5))||' + '||utl_raw.length(addinfo)||' + '||utl_raw.length(utl_raw.substr(dbody, addpos-hsz-4+utl_raw.length(addinfo), blksz-(addpos-1)-utl_raw.length(dtail)-utl_raw.length(addinfo)))||' + 4');
          --utl_file.put_raw(bfw, utl_raw.concat(utl_raw.substr(dbody, 5, addpos-hsz-5), addinfo, utl_raw.substr(dbody, addpos-hsz, blksz-(addpos-1)-utl_raw.length(dtail)-utl_raw.length(addinfo))));
          nbody := utl_raw.concat(nbody, utl_raw.substr(dbody, 5, addpos-hsz-5), addinfo, utl_raw.substr(dbody, addpos-hsz-4+utl_raw.length(addinfo), blksz-(addpos-1)-utl_raw.length(dtail)-utl_raw.length(addinfo)));
          --trace('[replace_objid_in_file] new body len: '||utl_raw.length(nbody));
        elsif addpos = hsz+5 and addinfo is not null then
          --utl_file.put_raw(bfw, utl_raw.concat(addinfo, utl_raw.substr(dbody, addpos-hsz, blksz-(addpos-1)-utl_raw.length(dtail)-utl_raw.length(addinfo))));
          nbody := utl_raw.concat(nbody, addinfo, utl_raw.substr(dbody, addpos-hsz, blksz-(addpos-1)-utl_raw.length(dtail)-utl_raw.length(addinfo)));
        else
          --utl_file.put_raw(bfw, utl_raw.substr(dbody, 5, blksz-hsz-4-utl_raw.length(dtail)));
          nbody := utl_raw.concat(nbody, utl_raw.substr(dbody, 5, blksz-hsz-4-utl_raw.length(dtail)));
        end if;
        --trace('[replace_objid_in_file] tail in raw: '||dtail||'('||utl_raw.length(dtail)||')');
        --utl_file.put_raw(bfw, dtail);
        nbody := utl_raw.concat(nbody, dtail);
        trace('[replace_objid_in_file] new body length: '||utl_raw.length(nbody));
      else
        --utl_file.put_raw(bfw, dhead);
        --utl_file.put_raw(bfw, dbody);
        nbody := utl_raw.concat(nbody, dhead, dbody);
      end if;
      --if utl_raw.length(nbody) != blksz then
      --  trace('[replace_objid_in_file] new body length: '||utl_raw.length(nbody));
      --end if;
      utl_file.put_raw(bfw, nbody);

      utl_file.fflush(bfw);
      exception
        when no_data_found then
          exit;
        when others then
          trace('[replace_objid_in_file] '||SQLERRM);
          trace('[replace_objid_in_file] '||dbms_utility.format_error_backtrace);
          exit;
    end;
    end loop;
    utl_file.fclose(bfw);
    utl_file.fclose(bfr);
    trace('[replace_objid_in_file] completed.');
  end;

  function gen_table_name(tgttable varchar2,
                          plus     varchar2 default '',
                          genowner varchar2 default user)
  return varchar2
  as
    gentab varchar2(30);
  begin
    select upper(tgttable||plus||surfix) into gentab from (select surfix from (select null surfix from dual union all select level surfix from dual connect by level <= 255) where not exists (select 1 from dba_tables where owner = genowner and table_name = upper(tgttable||plus||surfix)) order by surfix nulls first) where rownum<=1;
    return gentab;
  end;

  function gen_file_name( tgtfile varchar2,
                          plus    varchar2 default '')
  return varchar2
  as
    genfile varchar2(30);
  begin
    select tgtfile||plus||surfix||'.DAT' into genfile from (select surfix from (select null surfix from dual union all select level surfix from dual connect by level <= 255) where not exists (select 1 from dba_data_files where file_name like '%\'||tgtfile||plus||surfix||'.DAT') order by surfix nulls first) where rownum<=1;
    return genfile;
  end;

  function gen_ts_name( tgtts  varchar2,
                        plus   varchar2 default '')
  return varchar2
  as
    gents varchar2(30);
  begin
    select tgtts||plus||surfix into gents from (select surfix from (select null surfix from dual union all select level surfix from dual connect by level <= 255) where not exists (select 1 from dba_tablespaces where tablespace_name = tgtts||plus||surfix) order by surfix nulls first) where rownum<=1;
    return gents;
  end;

  function get_cols_no_lob( recowner varchar2, 
                            rectab varchar2)
  return varchar2
  as
    cols        varchar2(32767);
    colno       number := 0;
  begin
    cols := '';
    for col_rec in (select column_name, data_type, nullable from dba_tab_cols where owner = recowner and table_name = rectab) loop
      if col_rec.data_type NOT LIKE '%LOB' then
        if colno > 0 then
          cols := cols||',';
        end if;
        cols := cols||col_rec.column_name;
        colno := colno + 1;
      end if;
    end loop;
    return cols;
  end;

  function restore_table_row_no_lob(recowner varchar2, 
                                    rectab varchar2, 
                                    rstowner varchar2, 
                                    rsttab varchar2,
                                    cols varchar2,
                                    rid rowid)
  return number
  as
    recnum      number := 0;
  begin
    begin
      execute immediate 'insert /*+*/ into '||rstowner||'.'||rsttab||'('||cols||') select '||cols||' from '||recowner||'.'||rectab||' where rowid = :rid' using rid;
      recnum := recnum + SQL%ROWCOUNT;
    exception when others then
      trace('[restore_table_row_no_lob] '||SQLERRM);
      trace('[restore_table_row_no_lob] '||dbms_utility.format_error_backtrace);
      null;
    end;  
    return recnum;
  end;

  function restore_table_in_rows( recowner varchar2, 
                                  rectab varchar2, 
                                  rstowner varchar2, 
                                  rsttab varchar2)
  return number
  as
    recnum      number := 0;
    blk_cur     r_cursor;
    objid       number;
    fid         number;
    blkno       number;
    rnum        number;
    gnum        number;
    cols        varchar2(32767);
  begin
    begin
      --trace('[restore_table_in_rows] '||'select dbms_rowid.rowid_object(rowid) objid, dbms_rowid.rowid_relative_fno(rowid) fid, dbms_rowid.rowid_block_number(rowid) blkno, count(1) rnum from '||recowner||'.'||rectab||' group by dbms_rowid.rowid_object(rowid), dbms_rowid.rowid_relative_fno(rowid), dbms_rowid.rowid_block_number(rowid)');
      open blk_cur for 'select dbms_rowid.rowid_object(rowid) objid, dbms_rowid.rowid_relative_fno(rowid) fid, dbms_rowid.rowid_block_number(rowid) blkno, count(1) rnum from '||recowner||'.'||rectab||' group by dbms_rowid.rowid_object(rowid), dbms_rowid.rowid_relative_fno(rowid), dbms_rowid.rowid_block_number(rowid)';
      loop
        fetch blk_cur into objid, fid, blkno, rnum;
        exit when blk_cur%NOTFOUND;
        trace('[restore_table_in_rows] expected rows: '||rnum);
        gnum := 0;
        --trace('[restore_table_in_rows] block: '||blkno);
        for i in 1..rnum loop
        begin
          --trace('[restore_table_in_rows] row: '||i);
          --execute immediate 'insert /*+*/into '||rstowner||'.'||rsttab||' select * from '||recowner||'.'||rectab||' where rowid = dbms_rowid.rowid_create(1, :objid, :fid, :blkno, :i)' using objid, fid, blkno, i-1;
          execute immediate 'insert /*+*/into '||rstowner||'.'||rsttab||' select * from '||recowner||'.'||rectab||' where dbms_rowid.rowid_relative_fno(ROWID)=:fid and dbms_rowid.rowid_block_number(ROWID)=:blkno and dbms_rowid.rowid_row_number(ROWID)=:i' using fid, blkno, i-1;
          recnum := recnum + SQL%ROWCOUNT;
          gnum := gnum + SQL%ROWCOUNT;
        exception when others then
          if sqlcode = -22922 then
            -- trace('[restore_table_in_rows] Warning: Unrecoverable Lob found!');
            if cols is null then
              cols := get_cols_no_lob(recowner, rectab);
            end if;
            recnum := recnum + restore_table_row_no_lob(recowner, rectab, rstowner, rsttab, cols, dbms_rowid.rowid_create(1, objid, fid, blkno, i-1));
          else
            trace('[restore_table_in_rows] '||SQLERRM);
            trace('[restore_table_in_rows] '||dbms_utility.format_error_backtrace);
          end if;
          null;
        end;
        end loop;
        if gnum != rnum then
          log('Warning: '||(rnum-gnum)||' records lost!');
        end if;
      end loop;
      exception when others then
        trace('[restore_table_in_rows] '||sqlerrm);
        trace('[restore_table_in_rows] '||dbms_utility.format_error_backtrace);
        null;
    end;  
    return recnum;
  end;

  function restore_table_in_rows_remote(recowner varchar2, 
                                        rectab varchar2, 
                                        rstowner varchar2, 
                                        rsttab varchar2,
                                        dblink varchar2)
  return number
  as
    recnum      number := 0;
    blk_cur     r_cursor;
    objid       number;
    fid         number;
    blkno       number;
    rnum        number;
    cols        varchar2(32767);
  begin
    begin
      --rollback;
      open blk_cur for 'select dbms_rowid.rowid_object(rowid) objid, dbms_rowid.rowid_relative_fno(rowid) fid, dbms_rowid.rowid_block_number(rowid) blkno, count(1) rnum from '||recowner||'.'||rectab||' group by dbms_rowid.rowid_object(rowid), dbms_rowid.rowid_relative_fno(rowid), dbms_rowid.rowid_block_number(rowid)';
      loop
        fetch blk_cur into objid, fid, blkno, rnum;
        exit when blk_cur%NOTFOUND;
        trace('[restore_table_in_rows_remote] expected rows: '||rnum);
        for i in 1..rnum loop
        begin
          --execute immediate 'insert /*+no_append*/into '||rstowner||'.'||rsttab||' select * from '||recowner||'.'||rectab||'@'||dblink||' where rowid = dbms_rowid.rowid_create(1, :objid, :fid, :blkno, :i)' using objid, fid, blkno, i-1;
          execute immediate 'insert /*+*/into '||rstowner||'.'||rsttab||' select * from '||recowner||'.'||rectab||'@'||dblink||' where dbms_rowid.rowid_relative_fno(ROWID)=:fid and dbms_rowid.rowid_block_number(ROWID)=:blkno and dbms_rowid.rowid_row_number(ROWID)=:i' using fid, blkno, i-1;
          recnum := recnum + SQL%ROWCOUNT;
          --commit;
        exception when others then
          if sqlcode = -22922 then
            if cols is null then
              cols := get_cols_no_lob(recowner, rectab);
            end if;
            recnum := recnum + restore_table_row_no_lob(recowner, rectab, rstowner, rsttab, cols, dbms_rowid.rowid_create(1, objid, fid, blkno, i-1));
          else
            trace('[restore_table_in_rows_remote] '||SQLERRM);
            trace('[restore_table_in_rows_remote] '||dbms_utility.format_error_backtrace);
            --commit;
          end if;
          null;
        end;
        end loop;
      end loop;
    end;  
    return recnum;
  end;

  function restore_table_ctas(recowner varchar2, 
                              rectab varchar2, 
                              rstowner varchar2, 
                              rsttab varchar2)
  return number
  as
    recnum      number := 0;
    tmptab      varchar2(30);
  begin
    tmptab := gen_table_name(rsttab, '', rstowner);
    begin
      execute immediate 'create table '||rstowner||'.'||tmptab||' as select /*+full(t)*/* from '||recowner||'.'||rectab||' t';
      execute immediate 'insert /*+append*/ into '||rstowner||'.'||rsttab||' select /*+full(t)*/* from '||recowner||'.'||tmptab||' t';
      recnum := SQL%ROWCOUNT;
      execute immediate 'drop table '||rstowner||'.'||tmptab;
      exception when others then
        --trace('[restore_table_ctas] '||SQLERRM);
        --trace('[restore_table_ctas] '||dbms_utility.format_error_backtrace);
        null;
    end;  
    return recnum;
  end;

  function restore_table_no_lob(recowner varchar2, 
                                rectab varchar2, 
                                rstowner varchar2, 
                                rsttab varchar2)
  return number
  as
    recnum      number := 0;
    cols        varchar2(32767);
  begin
    cols := get_cols_no_lob(recowner, rectab);

    begin
      --execute immediate 'alter system flush buffer_cache';
      execute immediate 'insert /*+append*/ into '||rstowner||'.'||rsttab||'('||cols||') select /*+full(t)*/'||cols||' from '||recowner||'.'||rectab||' t';

      recnum := recnum + SQL%ROWCOUNT;
    exception when others then
      --raise;
      if sqlcode = -22922 then
        null;
      else
        recnum := recnum + restore_table_in_rows(recowner, rectab, rstowner, rsttab);
      end if;
    end;
    trace('[restore_table_no_lob] '||recnum||' records recovered');
    return recnum;
  end;

  function restore_table( recowner varchar2, 
                          rectab varchar2, 
                          rstowner varchar2, 
                          rsttab varchar2,
                          selflink varchar2 default '')
  return number
  as
    recnum      number := 0;
    expnum      number := 0;
  begin
    begin
      trace('[restore_table] Trying to restore data to '||rstowner||'.'||rsttab);
      execute immediate 'alter system flush buffer_cache';
      if s_tracing then
        execute immediate 'select /*+full(t)*/count(*) from '||recowner||'.'||rectab||' t' into expnum;
        trace('[restore_table] Expected Records in this round: '||expnum);
      end if;
      execute immediate 'insert /*+append*/ into '||rstowner||'.'||rsttab||' select /*+full(t)*/* from '||recowner||'.'||rectab||' t';

      recnum := recnum + SQL%ROWCOUNT;
      if s_tracing and expnum != SQL%ROWCOUNT then
        trace('[restore_table] '||(expnum-SQL%ROWCOUNT)||' records lost!');
        return -1; -- for test
      end if;
    exception when others then
      --raise;
      if sqlcode = -22922 then
        log('Warning: Unrecoverable Lob found!');
        recnum := recnum + restore_table_in_rows_remote(recowner, rectab, rstowner, rsttab, selflink);
        --recnum := recnum + restore_table_no_lob(recowner, rectab, rstowner, rsttab);
      else
        trace(SQLERRM);
        trace('[restore_table] '||dbms_utility.format_error_backtrace);
        --recnum := recnum + restore_table_in_rows_remote(recowner, rectab, rstowner, rsttab, selflink);
        --return -1; -- test
        recnum := recnum + restore_table_in_rows(recowner, rectab, rstowner, rsttab);
      end if;
    end;
    execute immediate 'alter system flush buffer_cache';
    trace('[restore_table] '||recnum||' records recovered');
    return recnum;
  end;

  procedure recover_table(oriobjid number,
                          recowner varchar2, 
                          rectab varchar2, 
                          rstowner varchar2, 
                          rsttab varchar2, 
                          srcdir varchar2, 
                          srcfile varchar2, 
                          recdir varchar2, 
                          recfile varchar2, 
                          copydir varchar2, 
                          copyfile varchar2, 
                          blksz number default 8192,
                          fillblks number default 5,
                          selflink varchar2 default '',
                          endianess number default 1)
  as
    -- blk    blob;
    --vrw    raw(32767);
    frw    raw(32767);
    tsz    number := 4;
    hsz    number := 28;
    objr   raw(4);
    objn   number;
    dtail  raw(4);
    dhead  raw(32);
    dbody  raw(32767);
    --bfr    bfile;
    bfo    utl_file.file_type;
    bfr    utl_file.file_type;
    bfw    utl_file.file_type; 
    fillednum number := 0;
    dummyheader number;
    dummyblks   number;
    blkstofill  number := fillblks;
    recnum      number := 0;
    rstnum      number := 0;
    i           number := 0;
    j           number := 0;
    truncblks   number := 0;
  begin
    execute immediate 'truncate table '||rstowner||'.'||rsttab;
    execute immediate 'alter system set db_block_checking=false scope=memory';
    execute immediate 'alter system set db_block_checksum=false scope=memory';
    execute immediate 'alter system set "_db_block_check_objtyp"=false scope=memory';
    execute immediate 'alter session set events ''10231 trace name context forever, level 10''';
    execute immediate 'alter session set events ''10233 trace name context forever, level 10''';
    select header_block+1, blocks-3 into dummyheader, dummyblks from dba_segments where owner = recowner and segment_name = rectab;
    if blkstofill > dummyblks then
      blkstofill := dummyblks;
    end if;

    bfo := utl_file.fopen(srcdir, srcfile, 'RB');
    --utl_file.get_raw(bfo, dbody, blksz-hsz-tsz);
    --utl_file.get_raw(bfo, dtail, tsz);

    bfr := utl_file.fopen(copydir, copyfile, 'RB');
    bfw := utl_file.fopen(recdir, recfile, 'WB');
    -- reach to the transaction blocks to be filled
    i := 0;
    while true loop
    begin
      utl_file.get_raw(bfr, frw, blksz);
      utl_file.put_raw(bfw, frw);
      i := i+1;

      exit when i=dummyheader+fillednum;
      exception when others then
        --raise;
        --trace('[recover_table] block NO.: '||i);
        exit;
    end;
    end loop;

    -- go through the data file of truncated table
    while true loop
    begin
      --trace('[recover_table] '||j);
      j := j+1;
      --objr := substrb(rawtohex(dhead), 49, 8);
      utl_file.get_raw(bfo, dhead, hsz);
      if hsz <= 24 then
        utl_file.get_raw(bfo, dbody, blksz-tsz-hsz);
        --objr := substrb(rawtohex(dbody), 49-hsz*2, 8);
        objr := utl_raw.substr(dbody, 25-hsz, 4);
      else
        --objr := substrb(rawtohex(dhead), 49, 8);
        objr := utl_raw.substr(dhead, 25, 4);
      end if;
      if endianess > 0 then
        --objn := to_number(utl_raw.reverse(hextoraw(objr)), 'XXXXXXXX');
        objn := to_number(rawtohex(utl_raw.reverse(objr)), 'XXXXXXXX');
      else
        --objn := to_number(hextoraw(objr), 'XXXXXXXX');
        objn := to_number(rawtohex(objr), 'XXXXXXXX');
      end if;

      -- check if block belongs to truncated table
      if objn != oriobjid or substrb(rawtohex(dhead), 1, 2) != '06' then
        if hsz > 24 then
          utl_file.get_raw(bfo, dbody, blksz-hsz);
        else
          utl_file.get_raw(bfo, dtail, tsz);
        end if;
      else
        --trace('[recover_table] Find it.');
        truncblks := truncblks + 1;
        if hsz > 24 then
          utl_file.get_raw(bfo, dbody, blksz-hsz-tsz);
        end if;
        utl_file.get_raw(bfo, dtail, tsz);

        if not utl_file.is_open(bfr) then
          bfr := utl_file.fopen(copydir, copyfile, 'RB');
        end if;
        if not utl_file.is_open(bfw) then
          bfw := utl_file.fopen(recdir, recfile, 'WB');
        end if;

        -- filling the trans block
        utl_file.get_raw(bfr, dhead, hsz);
        utl_file.put_raw(bfw, dhead); -- put original header
        utl_file.put_raw(bfw, dbody); -- replace body
        utl_file.get_raw(bfr, dbody, blksz-hsz-tsz); -- forward pointer in original file copy
        utl_file.get_raw(bfr, dtail, tsz); -- get original tail
        utl_file.put_raw(bfw, dtail); -- put original tail
        fillednum := fillednum+1;
        i := i+1;
        -- no trans data block left, copy recovered data to backup table and fill the left blocks
        if fillednum >= blkstofill then
        --if fillednum+blkstofill-1 >= dummyblks then
        begin
          while true loop
          begin
            utl_file.get_raw(bfr, frw, blksz);
            utl_file.put_raw(bfw, frw);
            i := i+1;

            exception when others then
              if utl_file.is_open(bfr) then
                utl_file.fclose(bfr);
              end if;
              if utl_file.is_open(bfw) then
                utl_file.fclose(bfw);
              end if;
              exit;
          end;
          end loop;

          rstnum := restore_table(recowner, rectab, rstowner, rsttab, selflink);
          -- for test
          exit when rstnum < 0;
          recnum := recnum+rstnum;
          fillednum := 0;
          commit;

          bfr := utl_file.fopen(copydir, copyfile, 'RB');
          bfw := utl_file.fopen(recdir, recfile, 'WB');
          -- go to the transaction blocks again
          i := 0;
          while true loop
          begin
            utl_file.get_raw(bfr, frw, blksz);
            utl_file.put_raw(bfw, frw);
            i := i+1;

            exit when i=dummyheader+fillednum;
            exception when others then
              --raise;
              --trace('[recover_table] block NO.: '||i);
              exit;
          end;
          end loop;
          utl_file.fflush(bfw);
          exception when others then
            trace('[recover_table 2-1] '||sqlerrm);
            trace('[recover_table 2-1] '||dbms_utility.format_error_backtrace);
            null;
          end;
        end if;
      end if;
      exception 
        when no_data_found then
          exit;
        when others then
          trace('[recover_table 2-2] '||sqlerrm);
          trace('[recover_table 2-2] '||dbms_utility.format_error_backtrace);
          exit;
    end;
    end loop;

    -- last blocks not full filled dummy table
    --if fillednum+blkstofill-1 < dummyblks then
    if fillednum < blkstofill and rstnum>=0 then
    begin
      while true loop
      begin
        utl_file.get_raw(bfr, frw, blksz);
        utl_file.put_raw(bfw, frw);
        i := i+1;

        exception when others then
          if utl_file.is_open(bfr) then
            utl_file.fclose(bfr);
          end if;
          if utl_file.is_open(bfw) then
            utl_file.fclose(bfw);
          end if;
          exit;
      end;
      end loop;
      recnum := recnum+restore_table(recowner, rectab, rstowner, rsttab, selflink);
      --fillednum := 0;
      commit;
    end;
    end if;
    if utl_file.is_open(bfr) then
      utl_file.fclose(bfr);
    end if;
    if utl_file.is_open(bfw) then
      utl_file.fclose(bfw);
    end if;
    if utl_file.is_open(bfo) then
      utl_file.fclose(bfo);
    end if;
    utl_file.fclose_all();

    execute immediate 'alter session set events ''10233 trace name context off''';
    execute immediate 'alter session set events ''10231 trace name context off''';
    execute immediate 'alter system set "_db_block_check_objtyp"=true scope=memory';
    execute immediate 'alter system set db_block_checksum=true scope=memory';
    execute immediate 'alter system set db_block_checking=true scope=memory';

    log(truncblks||' truncated data blocks found. ');
    log(recnum||' records recovered in backup table '||rstowner||'.'||rsttab);
  end;

  procedure get_seg_meta( segowner varchar2,
                          segname varchar2,
                          srcdir varchar2,
                          dtail out raw, 
                          addinfo out raw, 
                          blksz number default 8192)
  as
    frw    raw(32767);
    firstblk  number;
    hdfile varchar2(255);
    bfo    utl_file.file_type;
    i      number := 0;
  begin
    select header_block+1 into firstblk from dba_segments where owner = segowner and segment_name = segname;
    select substr(file_name,decode(instr(d.file_name, '\', -1), 0, instr(file_name, '/', -1), instr(file_name, '\', -1))+1) into hdfile from dba_data_files d, dba_segments s where s.header_file = d.file_id and s.owner = segowner and s.segment_name = segname;

    bfo := utl_file.fopen(srcdir, hdfile, 'RB');

    -- reach to the truncated data blocks
    i := 0;
    while true loop
    begin
      utl_file.get_raw(bfo, frw, blksz);
      i := i+1;

      exit when i = firstblk;
      exception when others then
        exit;
    end;
    end loop;

    utl_file.get_raw(bfo, frw, blksz);

    dtail := utl_raw.substr(frw, blksz-3, 4);
    addinfo := utl_raw.substr(frw, 39, 2);

    utl_file.fclose(bfo);
  end;

  function get_seg_data_id( segowner varchar2,
                            segname varchar2,
                            srcdir varchar2, 
                            blksz number default 8192,
                            endianess number default 1)
  return number                            
  as
    frw    raw(32767);
    hsz    number := 28;
    firstblk  number;
    hdfile varchar2(255);
    bfo    utl_file.file_type;
    i      number := 0;
    objr   raw(4);
    objn   number;
  begin
    select header_block+1 into firstblk from dba_segments where owner = segowner and segment_name = segname;
    select substr(file_name,decode(instr(d.file_name, '\', -1), 0, instr(file_name, '/', -1), instr(file_name, '\', -1))+1) into hdfile from dba_data_files d, dba_segments s where s.header_file = d.file_id and s.owner = segowner and s.segment_name = segname;

    bfo := utl_file.fopen(srcdir, hdfile, 'RB');

    -- reach to the truncated data blocks
    i := 0;
    while true loop
    begin
      utl_file.get_raw(bfo, frw, blksz);
      i := i+1;

      exit when i = firstblk;
      exception when others then
        exit;
    end;
    end loop;

    utl_file.get_raw(bfo, frw, hsz);

    objr := utl_raw.substr(frw, 25, 4);
    if endianess > 0 then
      objn := to_number(rawtohex(utl_raw.reverse(objr)), 'XXXXXXXX');
    else
      objn := to_number(rawtohex(objr), 'XXXXXXXX');
    end if;

    utl_file.fclose(bfo);
    return objn;
  end;

  procedure recover_table(tgtowner varchar2,
                          tgttable varchar2,
                          recowner varchar2, 
                          rectab varchar2, 
                          rstowner varchar2, 
                          rsttab varchar2, 
                          srcdir varchar2, 
                          recdir varchar2, 
                          recfile varchar2, 
                          copydir varchar2, 
                          copyfile varchar2, 
                          blksz number default 8192,
                          fillblks number default 5,
                          selflink varchar2 default '')
  as
    tgtobjid    number;
    recobjid    number;
    endianess   number;
    tmpcopyf    varchar2(256);
    tsname      varchar2(30);
    readprop    varchar2(30);
    dtail       raw(4);
    addinfo     raw(32);
  begin
    select instr(platform_name, 'Windows') into endianess from v_$database where rownum<=1;
    select data_object_id into recobjid from dba_objects where owner = recowner and object_name = rectab and object_type='TABLE' and rownum<=1;
    log('begin to recover table '||tgtowner||'.'||tgttable);
    tgtobjid := get_seg_data_id(tgtowner, tgttable, srcdir, blksz, endianess);

    if s_repobjid then
      get_seg_meta(recowner, rectab, srcdir, dtail, addinfo, blksz);
      select tablespace_name into tsname from dba_tables where owner = tgtowner and table_name = tgttable and rownum<=1;
      select STATUS into readprop from dba_tablespaces where tablespace_name = tsname;
      if readprop != 'READ ONLY' then
        execute immediate 'alter tablespace '||tsname||' read only';
        execute immediate 'alter system flush buffer_cache';
      end if;
      for file_rec in (select substr(file_name,decode(instr(d.file_name, '\', -1), 0, instr(file_name, '/', -1), instr(file_name, '\', -1))+1) as filename from dba_data_files d, dba_tables t where d.tablespace_name = t.tablespace_name and t.owner = tgtowner and t.table_name = tgttable) loop
      begin
        tmpcopyf := gen_file_name(file_rec.filename, '$');
        copy_file(srcdir, file_rec.filename, srcdir, tmpcopyf);
        --replace_segmeta_in_file(srcdir, tmpcopyf, srcdir, file_rec.filename, tgtobjid, recobjid, dtail, 39, addinfo, blksz, endianess);
        replace_segmeta_in_file(srcdir, tmpcopyf, srcdir, file_rec.filename, tgtobjid, recobjid, dtail, 39, '', blksz, endianess);
        recover_table(recobjid, recowner, rectab, rstowner, rsttab, srcdir, file_rec.filename, recdir, recfile, copydir, copyfile, blksz, fillblks, selflink, endianess);
        --recover_table(tgtobjid, recowner, rectab, rstowner, rsttab, srcdir, file_rec.filename, recdir, recfile, copydir, copyfile, blksz, fillblks, selflink, endianess);
        copy_file(srcdir, tmpcopyf, srcdir, file_rec.filename);
        utl_file.fremove(srcdir, tmpcopyf);
        trace('[recover_table 1] '||tmpcopyf||' removed.');
      exception when others then
        trace('[recover_table 1] '||sqlerrm);
        trace('[recover_table 1] '||dbms_utility.format_error_backtrace);
      end;
      end loop;
      if readprop != 'READ ONLY' then
        execute immediate 'alter tablespace '||tsname||' read write';
      end if;
    else
      for file_rec in (select substr(file_name,decode(instr(d.file_name, '\', -1), 0, instr(file_name, '/', -1), instr(file_name, '\', -1))+1) as filename from dba_data_files d, dba_tables t where d.tablespace_name = t.tablespace_name and t.owner = tgtowner and t.table_name = tgttable) loop
      begin
        recover_table(tgtobjid, recowner, rectab, rstowner, rsttab, srcdir, file_rec.filename, recdir, recfile, copydir, copyfile, blksz, fillblks, selflink, endianess);
      exception when others then
        trace('[recover_table 1] '||sqlerrm);
        trace('[recover_table 1] '||dbms_utility.format_error_backtrace);
      end;
      end loop;
    end if;

    log('Recovery completed.');
  end;

  procedure prepare_files(tgtowner varchar2,
                          tgttable varchar2,
                          datapath varchar2,
                          datadir in out varchar2,
                          rects out varchar2, 
                          recfile out varchar2, 
                          rstts out varchar2, 
                          rstfile out varchar2, 
                          blksz out varchar2,
                          rectsblks number default 16,
                          rsttsblks number default 2560)
  as
    ext_mgmt   varchar2(30);
    ss_mgmt    varchar2(30);
  begin
    select block_size, extent_management, segment_space_management into blksz, ext_mgmt, ss_mgmt from dba_tablespaces ts, dba_tables t where t.tablespace_name = ts.tablespace_name and t.owner = upper(tgtowner) and t.table_name = upper(tgttable);
    select datadir||surfix into datadir from (select surfix from (select null surfix from dual union all select level surfix from dual connect by level <= 255) where not exists (select 1 from dba_directories where directory_name = datadir||surfix) order by surfix nulls first) where rownum<=1;
    log('Directory Name: '||datadir);
    execute immediate 'create directory '||datadir||' as '''||datapath||'''';

    --select 'FY_REC_DATA'||surfix into rects from (select surfix from (select null surfix from dual union all select level surfix from dual connect by level <= 255) where not exists (select 1 from dba_tablespaces where tablespace_name = 'FY_REC_DATA'||surfix) order by surfix nulls first) where rownum<=1;
    --select 'FY_REC_DATA'||surfix||'.DAT' into recfile from (select surfix from (select null surfix from dual union all select level surfix from dual connect by level <= 255) where not exists (select 1 from dba_data_files where file_name like '%\FY_REC_DATA'||surfix||'.DAT') order by surfix nulls first) where rownum<=1;
    rects := gen_ts_name('FY_REC_DATA','');
    recfile := gen_file_name('FY_REC_DATA','');
    log('Recover Tablespace: '||rects||'; Data File: '||recfile);
    execute immediate 'create tablespace '||rects||' datafile '''||rtrim(datapath, '\')||'\'||recfile||''' size '||to_char(blksz*rectsblks/1024)||'K autoextend off extent management '||ext_mgmt||' SEGMENT SPACE MANAGEMENT '||ss_mgmt;

    select 'FY_RST_DATA'||surfix into rstts from (select surfix from (select null surfix from dual union all select level surfix from dual connect by level <= 255) where not exists (select 1 from dba_tablespaces where tablespace_name = 'FY_REST_DATA'||surfix) order by surfix nulls first) where rownum<=1;
    --select 'FY_RST_DATA'||surfix||'.DAT' into rstfile from (select surfix from (select null surfix from dual union all select level surfix from dual connect by level <= 255) where not exists (select 1 from dba_data_files where file_name like '%\FY_REST_DATA'||surfix||'.DAT') order by surfix nulls first) where rownum<=1;
    rstts := gen_ts_name('FY_RST_DATA','');
    rstfile := gen_file_name('FY_RST_DATA','');
    log('Restore Tablespace: '||rstts||'; Data File: '||rstfile);
    execute immediate 'create tablespace '||rstts||' datafile '''||rtrim(datapath, '\')||'\'||rstfile||''' size '||to_char(blksz*rsttsblks/1024)||'K autoextend on extent management '||ext_mgmt||' SEGMENT SPACE MANAGEMENT '||ss_mgmt;
  end;

  procedure clean_up_ts(rects varchar2, 
                        rstts varchar2 default null)
  as
    readprop varchar2(30);
  begin
    select STATUS into readprop from dba_tablespaces where tablespace_name = rects;
    if readprop = 'READ ONLY' then
      execute immediate 'alter tablespace '||rects||' read write';
    end if;
    for tab_rec in (select owner, table_name from dba_tables where tablespace_name = rects) loop
      execute immediate 'drop table '||tab_rec.owner||'.'||tab_rec.table_name;
    end loop;
    if rstts is not null then
      for tab_rec in (select owner, table_name from dba_tables where tablespace_name = rstts) loop
        execute immediate 'drop table '||tab_rec.owner||'.'||tab_rec.table_name;
      end loop;
    end if;
  end;

  procedure fill_blocks(tgtowner varchar2,
                        tgttable varchar2,
                        datadir varchar2, 
                        rects varchar2,
                        recfile varchar2,
                        rstts varchar2,
                        blks number default 8,
                        recowner varchar2 default user,
                        rstowner varchar2 default user,
                        rectab in out varchar2,
                        rsttab in out varchar2,
                        copyfile out varchar2)
  as
    blksz  number;
    blkno  number;
    cols   varchar2(32767);
    vals   varchar2(32767);
    colno  number := 0;
  begin
    if rectab is null then
      select block_size into blksz from dba_tablespaces ts, dba_tables t where t.tablespace_name = ts.tablespace_name and t.owner = upper(tgtowner) and t.table_name = upper(tgttable);
      -- select upper(tgttable||'$'||surfix) into rectab from (select surfix from (select null surfix from dual union all select level surfix from dual connect by level <= 255) where not exists (select 1 from dba_tables where owner = recowner and table_name = upper(tgttable||'$'||surfix)) order by surfix nulls first) where rownum<=1;
      rectab := gen_table_name(tgttable, '$', recowner);
      log('Recover Table: '||recowner||'.'||rectab);
      --trace('[fill_blocks] create table '||recowner||'.'||rectab||' tablespace '||rects||' storage(initial '||to_char(blks*blksz/1024)||'K) as select * from '||tgtowner||'.'||tgttable||' where 1=2');
      execute immediate 'create table '||recowner||'.'||rectab||' tablespace '||rects||' storage(initial '||to_char(blks*blksz/1024)||'K) as select * from '||tgtowner||'.'||tgttable||' where 1=2';
    else
      --execute immediate 'truncate table '||recowner||'.'||rectab;
      execute immediate 'delete from '||recowner||'.'||rectab;
      commit;
    end if;

    cols := '';
    vals := '';
    for col_rec in (select column_name, data_type, nullable from dba_tab_cols where owner = recowner and table_name = rectab) loop
      if col_rec.nullable = 'N' then
        execute immediate 'alter table '||recowner||'.'||rectab||' modify '||col_rec.column_name||' null';
      end if;
      if colno < 6 then
        if col_rec.data_type like '%CHAR%' or col_rec.data_type like '%RAW%' then
          if colno > 0 then
            cols := cols||',';
            vals := vals||',';
          end if;
          cols := cols||col_rec.column_name;
          vals := vals||'''A''';
          colno := colno + 1;
       elsif col_rec.data_type like '%NUMBER%' or col_rec.data_type = 'FLOAT' then
          if colno > 0 then
            cols := cols||',';
            vals := vals||',';
          end if;
          cols := cols||col_rec.column_name;
          vals := vals||'0';
          colno := colno + 1;
       elsif col_rec.data_type LIKE '%TIMESTAMP%' or col_rec.data_type = 'DATE' then
          if colno > 0 then
            cols := cols||',';
            vals := vals||',';
          end if;
          cols := cols||col_rec.column_name;
          vals := vals||'sysdate';
          colno := colno + 1;
       end if;
      end if;
    end loop;

    --select upper(tgttable||'$$'||surfix) into rsttab from (select surfix from (select null surfix from dual union all select level surfix from dual connect by level <= 255) where not exists (select 1 from dba_tables where owner = rstowner and table_name = upper(tgttable||'$$'||surfix)) order by surfix nulls first) where rownum<=1;
    if rsttab is null then
      rsttab := gen_table_name(tgttable, '$$', rstowner);
      log('Restore Table: '||rstowner||'.'||rsttab);
      execute immediate 'create table '||rstowner||'.'||rsttab||' tablespace '||rstts||' as select * from '||recowner||'.'||rectab||' where 1=2';
    else
      execute immediate 'truncate table '||rstowner||'.'||rsttab;
    end if;

    --trace('[fill_blocks] insert into '||recowner||'.'||rectab||'('||cols||') values ('||vals||')');
    while true loop
      execute immediate 'insert into '||recowner||'.'||rectab||'('||cols||') values ('||vals||')';
      execute immediate 'select count(unique(dbms_rowid.rowid_block_number( rowid ))) from '||recowner||'.'||rectab into blkno ;
      exit when blkno >= blks-3;
    end loop;
    commit;
    execute immediate 'alter system flush buffer_cache';
    execute immediate 'delete from '||recowner||'.'||rectab;
    commit;
    execute immediate 'alter system flush buffer_cache';
    trace('[fill_blocks] Data Blocks formatted.');

    execute immediate 'alter tablespace '||rects||' read only';

    select 'FY_REC_DATA_COPY'||surfix||'.DAT' into copyfile from (select surfix from (select null surfix from dual union all select level surfix from dual connect by level <= 255) where not exists (select 1 from dba_data_files where file_name like '%FY_REC_DATA_COPY'||surfix||'.DAT') order by surfix nulls first) where rownum<=1;
    copy_file(datadir, recfile, datadir, copyfile);
    log('Copy file of Recover Tablespace: '||copyfile);
  end;

  procedure test_rec1( tow varchar2 default 'SYS', 
                       ttb varchar2 default 'T_CHAIN',
                       fbks number default 1, 
                       datapath varchar2 default 'D:\oracle\product\10.2.0\oradata\EDGAR\DATAFILE\')
  as
    tgtowner varchar2(30):= upper(tow);
    tgttable varchar2(30):= upper(ttb);
    datadir varchar2(30);
    rects varchar2(30);
    recfile varchar2(30); 
    rstts varchar2(30);
    rstfile varchar2(30);
    blksz number;
    rectab varchar2(30);
    rsttab varchar2(30);
    copyfile varchar2(30);
  begin
    datadir := 'FY_DATA_DIR';
    prepare_files(tgtowner, tgttable, datapath, datadir, rects, recfile, rstts, rstfile, blksz);
    rects := 'FY_REC_DATA';
    rstts := 'FY_RST_DATA';
    recfile := 'FY_REC_DATA.DAT';
    fill_blocks(tgtowner, tgttable, datadir, rects, recfile, rstts, 8, tgtowner, tgtowner, rectab, rsttab, copyfile);
    recover_table(tgtowner, tgttable, tgtowner, rectab, tgtowner, rsttab, datadir, datadir, recfile, datadir, copyfile, blksz, fbks, 'myself');
  end;

  procedure test_rec2( tow varchar2 default 'SYS', 
                       ttb varchar2 default 'T_CHAIN',
                       fbks number default 1)
  as
    tgtowner varchar2(30):= upper(tow);
    tgttable varchar2(30):= upper(ttb);
    datadir varchar2(30);
    rects varchar2(30);
    recfile varchar2(30); 
    rstts varchar2(30);
    blksz number;
    rectab varchar2(30);
    rsttab varchar2(30);
    copyfile varchar2(30);
  begin
    datadir := 'FY_DATA_DIR';
    rects := 'FY_REC_DATA';
    rstts := 'FY_RST_DATA';
    recfile := 'FY_REC_DATA.DAT';
    clean_up_ts(rects, rstts);
    select block_size into blksz from dba_tablespaces ts, dba_tables t where ts.tablespace_name = t.tablespace_name and t.owner = tgtowner and t.table_name = tgttable;
    fill_blocks(tgtowner, tgttable, datadir, rects, recfile, rstts, 8, tgtowner, tgtowner, rectab, rsttab, copyfile);
    recover_table(tgtowner, tgttable, tgtowner, rectab, tgtowner, rsttab, datadir, datadir, recfile, datadir, copyfile, blksz, fbks, 'myself');
  end;

begin
  null;
end FY_Recover_Data;
/

操作过程
步骤1:在linux中的oracle用户下解压FY_Recover_Data.zip包

$ unzip FY_Recover_Data.zip
Archive: FY_Recover_Data.zip
inflating: FY_Recover_Data.SQL

步骤2:恢复

1)在sys用户下执行存储过程

SQL> @/home/oracle/FY_Recover_Data.SQL
Package created.
Package body created.

2)查看test_emp表在数据文件中的目录

SQL> select file_name from dba_data_files f, dba_tables t where t.owner='SCOTT' and t.table_name='TEST_EMP' and t.tablespace_name = f.tablespace_name;

FILE_NAME
--------------------------------------------------------------------------------
/u03/oracle/oradata/WUTONG/datafile/o1_mf_users_cx3xt940_.dbf

3)通过脚本恢复,可以用sqlplus命令行或者plsql developer执行

declare
      tgtowner varchar2(30);
      tgttable varchar2(30);
      datapath varchar2(4000);
      datadir varchar2(30);
      rects varchar2(30);
      recfile varchar2(30);
      rstts varchar2(30);
      rstfile varchar2(30);
     blksz number;
     rectab varchar2(30);
     rsttab varchar2(30);
     copyfile varchar2(30);
   begin
     tgtowner := 'SCOTT'; --table owner
     tgttable := 'TEST_EMP';  --table name
     datapath := '/u03/oracle/oradata/WUTONG/datafile/';    --必须和test.t1表所在的数据文件的目录相同
     datadir := 'FY_DATA_DIR';        --oracle中目录的名字,可以修改
     Fy_Recover_data.prepare_files(tgtowner, tgttable, datapath, datadir, rects, recfile, rstts, rstfile, blksz);
     Fy_Recover_data.fill_blocks(tgtowner, tgttable, datadir, rects, recfile, rstts, 8, tgtowner, tgtowner, rectab, rsttab, copyfile);
     Fy_Recover_data.recover_table(tgtowner, tgttable, tgtowner, rectab, tgtowner, rsttab, datadir, datadir, recfile,datadir, copyfile, blksz);
   end;

以上SQL脚本产生2个表空间(2个数据文件),还有1个copy文件。

4)切换到scott用户下查看会发现多了些不一样以test_emp的表,这时找到相关有数据的表,把数据插入原表test_emp

SQL> conn scott/tiger
Connected.
SQL> select * from tab;
TNAME TABTYPE CLUSTERID



BONUS TABLE
DEPT TABLE
EMP TABLE
SALGRADE TABLE
TEST_EMP TABLE
TEST_EMP$ TABLE
TEST_EMP$$ TABLE
7 rows selected.
SQL> insert into test_emp select from TEST_EMP$$;
14 rows created.
SQL> commit;
Commit complete.
SQL> select count(



) from test_emp;
COUNT(*)








14

当你看到这一步的时候,说明truncate的表已经完全恢复了,恭喜你数据恢复成功!

步骤3:恢复数据后,把恢复时产生的2个表空间删除,再删除对应数据文件

SQL> conn / as sysdba
Connected.
SQL> select name from v$datafile;
NAME
--------------------------------------------------------------------------------
/u03/oracle/oradata/WUTONG/datafile/o1_mf_system_cx3xt90z_.dbf
/u03/oracle/oradata/WUTONG/datafile/o1_mf_sysaux_cx3xt930_.dbf
/u03/oracle/oradata/WUTONG/datafile/o1_mf_undotbs1_cx3xt93b_.dbf
/u03/oracle/oradata/WUTONG/datafile/o1_mf_users_cx3xt940_.dbf
/u03/oracle/oradata/WUTONG/datafile/o1_mf_wutong_cx415lcj_.dbf
/u03/oracle/oradata/WUTONG/datafile/FY_REC_DATA.DAT
/u03/oracle/oradata/WUTONG/datafile/FY_RST_DATA.DAT
7 rows selected.
SQL> drop tablespace FY_REC_DATA INCLUDING CONTENTS and datafiles ;

Tablespace dropped.
SQL> drop tablespace FY_RST_DATA INCLUDING CONTENTS and datafiles;
Tablespace dropped.
SQL> select name from v$datafile;
NAME
--------------------------------------------------------------------------------
/u03/oracle/oradata/WUTONG/datafile/o1_mf_system_cx3xt90z_.dbf
/u03/oracle/oradata/WUTONG/datafile/o1_mf_sysaux_cx3xt930_.dbf
/u03/oracle/oradata/WUTONG/datafile/o1_mf_undotbs1_cx3xt93b_.dbf
/u03/oracle/oradata/WUTONG/datafile/o1_mf_users_cx3xt940_.dbf
/u03/oracle/oradata/WUTONG/datafile/o1_mf_wutong_cx415lcj_.dbf

此时已操作完成。

注意:恢复期间,原truncate表不能有操作,如果有数据生成,会影响后续的恢复。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!