Oracle补习班第九天

Better to light one candle than to curse the darkness.

与其诅咒黑暗,不如点亮烛光!

 

 

1,用SQL备份数据库数据文件

  sqlplus / as sysdba

  archive log list; #查看归档日志状态

  show parameter dest_1 #查看归档日志文件路径

  alter system set log_archive_dest_1='location=/data/archive'; #更改归档日志路径,写在spfile里

 

  shutdown immediate 

  startup mount #开启归档功能要在mount状态下

  alter database archivelog; #开启

  alter database open;

  archive log list; #查看

  alter system switch logfile; #手动切换redo日志组,会生成归档日志文件

 

  alter tablespace users begin backup; #备份users表空间

  set lines 300

  set pages 999

  col name for a45

  select checkpoint_change#,name from v$datafile; #users表空间检查点不同于其他

  alter system checkpoint; #更新检查点,只有users表空间不变,可以用系统命令拷贝文件

  cp users01.dbf /tmp/users01.dbf.bak

  create table scott.test002 tablespace users as select * from dba_objects;

  alter system switch logfile; /; /;

  insert into scott.test002 select * from scott.test002;

  commit;

  alter system switch logfile; /; /; /;

  alter tablespace users end backup; #结束备份并删除文件

 

  startup force #缺少数据文件报错

  cp /tmp/users01.dbf.bak /data/orcl/users01.dbf

   select checkpoint_change#,name from v$datafile;

  recover datafile 4; #利用归档日志回复数据文件缺失部分

  alter database open; 

  select count(*) from scott.test002; #查看数据文件是否被恢复

总结:此种方法生产不实用。备份恢复必须开启归档日志,利用备份的静态文件+归档日志将数据库恢复到当前状态。

 

2,使用RMAN工具备份数据库

备份恢复参数文件

  rman target / #使用sys登录rman

  show all;

  report schema; #全备份大小根据所有表空间大小

  list backup; #查看备份

  backup spfile format '/data/backup/%U_%t.spfileora'; #备份参数文件

  mv spfileorcl.ora /tmp/spfileorcl.ora #模拟动态参数文件损坏

  mv initorcl.ora /tmp/initorcl.ora.bak #模拟静态参数文件损坏

  startup force #缺少参数文件,sql中无法启动

  startup #在rman中启动不使用参数文件  

  restore spfile from '/data/backup/Osrogned_1_1_931683789.spfileora'; #恢复参数文件

  注意:当数据库状态发生改变时,rman必须重新连接

备份恢复控制文件

  rman target /

  backup current controlfile format '/data/backup/%U_%t.controlctl'; #备份控制文件

  mv control01.ctl /tmp/control01.ctl.bak #模拟控制文件失效

  mv control02.ctl /tmp/control02.ctl.bak 

  restore controlfile from '/data/backup/Otrogpec_1_1_931685836.controlctl';

  startup force

  alter database open resetlogs;

  rman target /

  recover database;

  注意:只要database状态发生变化,rman就要重新连接

备份数据库

  backup database format '/data/backup/%U_%t.datafiledbf';

  alter system switch logfile;

  rm -rf *.dbf #模拟数据文件丢失

  restore database;

  recover datafile 1;

  recover datafile 2;

  recover datafile 3;

  ..

  注意:包含了参数文件及控制文件

backup datafile 1 format '/data/backup/%U_%t.datafile1dbf'; #备份数据文件1号

backup tablespace users format '/data/backup/%U_%t.usersdbf'; #备份用户表空间

backup archivelog all format '/data/backup/%U_%t.archivelogdbf'; #备份归档日志

 

3,使用脚本全备份数据库

  vim rmanfull.scr

    run{

    allocate channel d01 type disk format '/data/backup/%U%tfull.dbf';

    backup incremental level 0 database include current controlfile;

    release channel d01;

    }

  vim rmanfull.sh

    source /home/oracle/.bash_profile

    rman target / cmdfile=/home/oracle/rmanfull.scr log=/home/oracle/rmanscr.log

  chmod 755 ./rmanfull.sh

  crontab -e #定时任务

    * * * * * /home/oracle/rmanfull.sh

 

4,全量备份+增量备份

  crontab -e

    * * * * 0 /home/oracle/rmanfull.sh

    * * * * 3 /home/oracle/rmanzl1.sh

    * * * * 1,2,4,5,6 /home/oracle/rmanzl2.sh

    * * * * * /home/oracle/archive.sh

  其他不变

    backup archivelog all;

    backup incremental level 1 database include current controlfile;

    backup incremental level 2 database include current controlfile;

  

 

  

  

  

  

  

  

 

  

  

posted on 2016-12-22 12:06  花名八戒  阅读(91)  评论(0编辑  收藏  举报