物理备份 -- 数据文件,控制文件,日志文件,参数文件
数据库备份
冷备 -- 归档和非归档均可以
什么时候必须用冷备?
1. 数据库的模式为非归档的
2. 用于现场保护
冷备的过程:
1. 首先查看备份文件的位置:
数据文件、控制文件、日志文件
select '!cp -v '||name|| ' /u01/app/oracle/backup/coldbk' from v$datafile
union all
select '!cp -v '||name|| ' /u01/app/oracle/backup/coldbk' from v$controlfile
union all
select '!cp -v '||member|| ' /u01/app/oracle/backup/coldbk' from v$logfile;
参数文件:
show parameter spfile;
$ORACLE_HOME/dbs/spfileorcl.ora
口令文件:
$ORACLE_HOME/dbs/orapworcl
2. 创建备份目录
mkdir -p /u01/app/oracle/backup
mkdir -p /u01/app/oracle/backup/coldbk
mkdir -p /u01/app/oracle/backup/hotbk
mkdir -p /u01/app/oracle/backup/archivelog
3. 停止数据库:
shutdown immediate
4. 拷贝以上的文件到备份目录:
!cp -v /u01/app/oracle/oradata/orcl/*dbf /u01/app/oracle/backup/coldbk
!cp -v /u01/app/oracle/oradata/orcl/*log /u01/app/oracle/backup/coldbk
!cp -v /u01/app/oracle/oradata/orcl/control01.ctl /u01/app/oracle/backup/coldbk
!cp -v /u01/app/oracle/fra/orcl/control02.ctl /u01/app/oracle/backup/coldbk
!cp -v /u01/app/oracle/product/11.2.0/db/dbs/spfileorcl.ora /u01/app/oracle/backup/coldbk
!cp -v /u01/app/oracle/product/11.2.0/db/dbs/orapworcl /u01/app/oracle/backup/coldbk
5. 启动数据库:
startup
在非归档模式下,如果从备份以来,数据库经历了日志切换,使用冷备份只能进行不完全恢复, 数据库只能恢复到备份点,从备份点以后的数据都会丢失
实验:非归档模式下的恢复
模拟损坏:
conn scott/tiger
create table e as select * frm emp;
将所有的数据文件删除:
cd $ORACLE_BASE/oradata/orcl
rm -rf *dbf
select * from e;---还可以访问,因为e表在内存中
清除内存:
conn /as sysdba
alter system flush buffer_cache;
conn scott/tiger--报错,杀掉dbw0进程
ps -ef | grep dbw
kill -9
执行恢复:
将数据文件、控制文件、日志文件的备份进行还原:
cd /u01/backup/coldbk
cp *dbf /u01/app/oracle/oradata/orcl
cp *ctl /u01/app/oracle/oradata/orcl
cp *log /u01/app/oracle/oradata/orcl
启动数据库
sqlplus / as sysdba
startup
conn scott/tiger
select * from tab;--发现e表丢失
热备 -- 只能归档
热备:是指数据库在打开的状态下进行的备份,只有数据库是归档模式时,才能使用
对整个数据库进行热备的过程
1. 查看数据文件位置
select '! cp -v '||name|| ' /u01/backp/hotbk' from v$datafile
! cp -v /u01/app/oracle/oradata/orcl/system01.dbf /u01/app/oracle/backup/hotbk
! cp -v /u01/app/oracle/oradata/orcl/undotbs01.dbf /u01/app/oracle/backup/hotbk
! cp -v /u01/app/oracle/oradata/orcl/sysaux01.dbf /u01/app/oracle/backup/hotbk
! cp -v /u01/app/oracle/oradata/orcl/users01.dbf /u01/app/oracle/backup/hotbk
! cp -v /u01/app/oracle/oradata/orcl/example01.dbf /u01/app/oracle/backup/hotbk
2. 对数据文件进行备份:
alter database begin backup;
! cp -v /u01/app/oracle/oradata/orcl/system01.dbf /u01/app/oracle/backup/hotbk
! cp -v /u01/app/oracle/oradata/orcl/undotbs01.dbf /u01/app/oracle/backup/hotbk
! cp -v /u01/app/oracle/oradata/orcl/sysaux01.dbf /u01/app/oracle/backup/hotbk
! cp -v /u01/app/oracle/oradata/orcl/users01.dbf /u01/app/oracle/backup/hotbk
! cp -v /u01/app/oracle/oradata/orcl/example01.dbf /u01/app/oracle/backup/hotbk
alter database end backup;
begin backup的时候:oracle将执行检查点,通知数据库写进程写脏块,然后更新数据文件的文件头和控制文件的检查点scn, 同时要冻结文件头的检查点scn,当发出end backup的时候才会改变
3. 对控制文件进行备份:
有两种方式:
alter database backup controlfile to '/u01/app/oracle/backup/hotbk/control.bak' reuse; -- 快照式备份
alter database backup controlfile to trace as '/u01/app/oracle/backup/hotbk/control.tra' reuse;
4. 对归档日志文件进行备份:
日志文件已经归档,不需要备份;
将当前日志归档后备份归档即可
alter system archive log current;
alter system switch logfile;
alter system switch logfile;
alter system switch logfile;
alter system switch logfile;
select NAME from v$archived_log;
!cp /u01/app/oracle/archivelog/* /u01/app/oracle/backup/archivelog
5. 对参数文件进行备份:
create pfile='/u01/app/oracle/backup/hotbk/initorcl.ora' from spfile;
6. 对口令文件的备份:
!cp -v $ORACLE_HOME/dbs/orapworcl /u01/app/oracle/backup/hotbk
热备脚本:
vi /u01/userbak.sh写入以下内容:
sqlplus / as sysdba<<eof
alter database begin backup;
!cp -v /u01/app/oracle/oradata/orcl/*dbf /u01/app/oracle/backup/hotbk
alter database end backup;
alter database backup controlfile to trace as '/u01/app/oracle/backup/hotbk/control.trace' reuse;
alter database backup controlfile to '/u01/app/oracle/backup/hotbk/control.bak' reuse;
alter system archive log current;
alter system switch logfile;
alter system switch logfile;
alter system switch logfile;
alter system switch logfile;
create pfile='/u01/app/oracle/backup/hotbk/initorcl.ora' from spfile;
!cp $ORACLE_HOME/dbs/orapworcl /u01/app/oracle/backup/hotbk
!cp /u01/app/oracle/archivelog/* /u01/app/oracle/backup/archivelog
exit
eof
echo "backup is ok"
保存退出
chmod +x /u01/userback.sh
cd /u01
./userbak.sh
备份表空间
对表空间实施热备:
select file_name from dba_data_files where tablespace_name='USERS';
alter tablespace users begin backup;
!cp /u01/app/oracle/oradata/orcl/users01.dbf /u01/app/oracle/backup/hotbk/
alter tablespace users end backup;
对表空间实施冷备:
alter tablespace users offline normal;
!cp /u01/app/oracle/oradata/orcl/users01.dbf /u01/app/oracle/backup/coldbk/
alter tablespace users online;