数据库备份分为:
物理备份(physical backup)对数据库的物理文件备份,包括:数据文件、归档文件、控制文件等.(不关注数据库里面的东西表什么的)
逻辑备份(logical backup)利用oracle的导出工具(data pump export)到处数据库的逻辑对象(如表,存储过程等)
把它们放在二进制文件中,当发生数据丢失时,导入导出的数据。逻辑被封会导致数据丢失,除非想备份历史数据,否则不要使用逻辑备份。
物理备份又分为:
冷备份
冷备份又叫脱机备份或者离线备份,数据库关闭下,对数据库进行备份,需要DBA备份所有数据文件,控制文件和重做日志文件。(关闭数据库,使用较少)
热备份
热备份又叫联机备份,指数据库运行时进行的备份,需要数据库运行在归档模式。
两种备份数据库工具:
用户管理的备份与恢复(user-managed backup and recover)
RMAN(恢复管理器)
逻辑备份:
-exp/imp
expdp/impdp
oracle恢复
实例恢复(非正常关闭)自动
介质恢复()手动命令
数据恢复文件:Data files ;Redo log files;Control files
实例恢复(实例崩溃后进行的恢复)可以是自动的
######################################################################
######################################################################
crosscheck backupset;
backup tablesapce users;//备份表空间
backup datafile 4;//备份文件
backup archivelog all;
//恢复表空间
restore tablespace test_backup;
recover tablespace test_backup;
RMAN:
有oracle enterprise manager(企业管理器)
1.通道(channel)
表示制定设备的一个数据流,一个通道对应一个服务器会话。
用户可以手动分配通道,如ALLOCATE CHANNEL c1 DEVICE TYPE sbt,也可以用CONFIGURE CHANNEL命令配置自动分配的通道。
Oracle的物理结构主要有三种文件。
.dbf数据文件,是用于存储数据库数据的文件,例如表中的记录,索引,数据字典信息等,可以通过系统数据字典DBA_DATA_FILES查看相关信息。与逻辑角度的表空间(并不是真正的文件)对应,一个表空间可以有多个数据文件,但一个数据文件只能属于一个表空间。
.log重做日志文件,用于记录对数据库的修改信息(查询操作不会产生)。日志文件是备份与恢复的重要手段。
.ctl控制文件,用于描述数据库的物理结构。存放有数据文件和日志文件等信息。
.ora参数文件,记录数据库名,控制文件路径、进程等信息。
上面三种文件任意一种出错,数据库都可能不能正常运行。参数文件可以修改数据库某些参数,但是需要重启数据库才生效。
######################################################################
######################################################################
当数据库在ARCHIVELOG模式运行时归档重做日志的复制位置可以采用两种配置方法,实际上也可以同时采用两种方式。
第一种选择是配置ARVHIVELOG目标目录,第二种选择是配置oracle闪回恢复区(Flash Recover Area,FRA)。
create tablespace test_backup datafile '/home/oracle/app/oracle/oradata/ORCL/datafile/test_backup.dbf' size 10m autoextend on;
create table test_backup (aa number,bb varchar2(10) ) tablespace test_backup;
insert into test_backup values (1,'test_back1');
alter tablespace test_backup begin backup;//beifenmoshi
select *from test_backup;
查看数据库有多少tablesapce
select * from dba_tablespaces;
###################################################
准备
oracle@Linx:~/app/oracle/product/12.1.0/dbhome_1/bin$ ./sqlplus / as sysdba
SQL*Plus: Release 12.1.0.2.0 Production on Mon Jul 4 13:52:17 2016
Copyright (c) 1982, 2014, Oracle. All rights reserved.
Connected to an idle instance.
SQL> shutdown immediate;
ERROR:
ORA-01034: ORACLE not available
ORA-27101: shared memory realm does not exist
Linux-x86_64 Error: 2: No such file or directory
Additional information: 3640
Additional information: -1046699685
Process ID: 0
Session ID: 0 Serial number: 0
SQL> startup mount;
ORACLE instance started.
Total System Global Area 838860800 bytes
Fixed Size 2929936 bytes
Variable Size 578816752 bytes
Database Buffers 251658240 bytes
Redo Buffers 5455872 bytes
Database mounted.
SQL> alter database archivelog;
Database altered.
SQL> alter database open;
Database altered.
查看
oracle@Linx:~/app/oracle/product/12.1.0/dbhome_1/bin$ ./rman target /
Recovery Manager: Release 12.1.0.2.0 - Production on Mon Jul 4 13:55:43 2016
Copyright (c) 1982, 2015, Oracle and/or its affiliates. All rights reserved.
connected to target database: ORCL (DBID=1442990981)
RMAN> list backup
2> ;
using target database control file instead of recovery catalog
specification does not match any backup in the repository
RMAN> delete backup;
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=63 device type=DISK
specification does not match any backup in the repository
RMAN> list backup;
specification does not match any backup in the repository
清空backupset
cd app/oracle/fast_recovery_area/ORCL/
mkdir backupset
//cd backupset
//rm -rf *
备份
backup database;
查看
RMAN> list backup;
List of Backup Sets
===================
BS Key Type LV Size Device Type Elapsed Time Completion Time
1 Full 1.15G DISK 00:00:24 08-JUL-16
BP Key: 1 Status: AVAILABLE Compressed: NO Tag: TAG20160708T130812
Piece Name: /home/oracle/app/oracle/fast_recovery_area/ORCL/backupset/2016_07_08/o1_mf_nnndf_TAG20160708T130812_cqyf9wxo_.bkp
List of Datafiles in backup set 1
File LV Type Ckp SCN Ckp Time Name
1 Full 1911229 08-JUL-16 /home/oracle/app/oracle/oradata/ORCL/datafile/o1_mf_system_cpno29v4_.dbf
3 Full 1911229 08-JUL-16 /home/oracle/app/oracle/oradata/ORCL/datafile/o1_mf_sysaux_cpno16o1_.dbf
4 Full 1911229 08-JUL-16 /home/oracle/app/oracle/oradata/ORCL/datafile/o1_mf_undotbs1_cpno3g6b_.dbf
6 Full 1911229 08-JUL-16 /home/oracle/app/oracle/oradata/ORCL/datafile/o1_mf_users_cpno3f1w_.dbf
BS Key Type LV Size Device Type Elapsed Time Completion Time
2 Full 661.36M DISK 00:00:08 08-JUL-16
BP Key: 2 Status: AVAILABLE Compressed: NO Tag: TAG20160708T130812
Piece Name: /home/oracle/app/oracle/fast_recovery_area/ORCL/35DB5039BA0B2913E0530101007F2DF9/backupset/2016_07_08/o1_mf_nnndf_TAG20160708T130812_cqyfc06k_.bkp
List of Datafiles in backup set 2
Container ID: 3, PDB Name: ORACLP
File LV Type Ckp SCN Ckp Time Name
8 Full 1749265 22-JUN-16 /home/oracle/app/oracle/oradata/ORCL/35DB5039BA0B2913E0530101007F2DF9/datafile/o1_mf_system_cpnojvwb_.dbf
9 Full 1749265 22-JUN-16 /home/oracle/app/oracle/oradata/ORCL/35DB5039BA0B2913E0530101007F2DF9/datafile/o1_mf_sysaux_cpnojvwg_.dbf
10 Full 1749265 22-JUN-16 /home/oracle/app/oracle/oradata/ORCL/35DB5039BA0B2913E0530101007F2DF9/datafile/o1_mf_users_cpnok9qm_.dbf
BS Key Type LV Size Device Type Elapsed Time Completion Time
3 Full 660.27M DISK 00:00:11 08-JUL-16
BP Key: 3 Status: AVAILABLE Compressed: NO Tag: TAG20160708T130812
Piece Name: /home/oracle/app/oracle/fast_recovery_area/ORCL/35DB3A4768B4262CE0530101007FCB17/backupset/2016_07_08/o1_mf_nnndf_TAG20160708T130812_cqyfch93_.bkp
List of Datafiles in backup set 3
Container ID: 2, PDB Name: PDB$SEED
File LV Type Ckp SCN Ckp Time Name
5 Full 1746799 22-JUN-16 /home/oracle/app/oracle/oradata/ORCL/datafile/o1_mf_system_cpno4f0o_.dbf
7 Full 1746799 22-JUN-16 /home/oracle/app/oracle/oradata/ORCL/datafile/o1_mf_sysaux_cpno4dvz_.dbf
BS Key Type LV Size Device Type Elapsed Time Completion Time
4 Full 17.20M DISK 00:00:00 08-JUL-16
BP Key: 4 Status: AVAILABLE Compressed: NO Tag: TAG20160708T130918
Piece Name: /home/oracle/app/oracle/fast_recovery_area/ORCL/autobackup/2016_07_08/o1_mf_s_916664958_cqyfcywx_.bkp
SPFILE Included: Modification time: 08-JUL-16
SPFILE db_unique_name: ORCL
Control File Included: Ckp SCN: 1911257 Ckp time: 08-JUL-16
这时在backupset目录下会看到
oracle@Linx:~/app/oracle/fast_recovery_area/ORCL/backupset$ ls
2016_07_04
RMAN> sql 'alter system archive log current';
sql statement: alter system archive log current
模拟删除数据文件
oracle@Linx:~/app/oracle/oradata/ORCL$ ls
35DB5039BA0B2913E0530101007F2DF9 controlfile datafile onlinelog
oracle@Linx:~/app/oracle/oradata/ORCL$ cd 35DB5039BA0B2913E0530101007F2DF9/
oracle@Linx:~/app/oracle/oradata/ORCL/35DB5039BA0B2913E0530101007F2DF9$ ls
datafile
oracle@Linx:~/app/oracle/oradata/ORCL/35DB5039BA0B2913E0530101007F2DF9$ cd datafile/
oracle@Linx:~/app/oracle/oradata/ORCL/35DB5039BA0B2913E0530101007F2DF9/datafile$ ls -ltr
total 825228
-rw-r----- 1 oracle oinstall 20979712 Jun 22 17:11 o1_mf_temp_cpnojvwh_.dbf
-rw-r----- 1 oracle oinstall 5251072 Jun 22 17:18 o1_mf_users_cpnok9qm_.dbf
-rw-r----- 1 oracle oinstall 262152192 Jun 22 17:18 o1_mf_system_cpnojvwb_.dbf
-rw-r----- 1 oracle oinstall 576724992 Jun 22 17:18 o1_mf_sysaux_cpnojvwg_.dbf
oracle@Linx:~/app/oracle/oradata/ORCL/35DB5039BA0B2913E0530101007F2DF9/datafile$ rm -rf *.dbf
oracle@Linx:~/app/oracle/oradata/ORCL/35DB5039BA0B2913E0530101007F2DF9/datafile$ ls - ltr
ls: cannot access -: No such file or directory
ls: cannot access ltr: No such file or directory
oracle@Linx:~/app/oracle/oradata/ORCL/35DB5039BA0B2913E0530101007F2DF9/datafile$ ls -ltr
total 0
oracle@Linx:~/app/oracle/oradata/ORCL/35DB5039BA0B2913E0530101007F2DF9/datafile$ ls
oracle@Linx:~/app/oracle/oradata/ORCL/35DB5039BA0B2913E0530101007F2DF9/datafile$ cd ..
oracle@Linx:~/app/oracle/oradata/ORCL/35DB5039BA0B2913E0530101007F2DF9$ cd ..
oracle@Linx:~/app/oracle/oradata/ORCL$ cd datafile/
oracle@Linx:~/app/oracle/oradata/ORCL/datafile$ ls
o1_mf_sysaux_cpno16o1_.dbf o1_mf_temp_cpno49xx_.tmp
o1_mf_sysaux_cpno4dvz_.dbf o1_mf_undotbs1_cpno3g6b_.dbf
o1_mf_system_cpno29v4_.dbf o1_mf_users_cpno3f1w_.dbf
o1_mf_system_cpno4f0o_.dbf pdbseed_temp012016-06-22_05-04-40-PM.dbf
oracle@Linx:~/app/oracle/oradata/ORCL/datafile$ rm -rf *.dbf
oracle@Linx:~/app/oracle/oradata/ORCL/datafile$ ls
o1_mf_temp_cpno49xx_.tmp
这个时候创建表格会报错
SQL> create table test_backup(aa varchar2(10));
create table test_backup(aa varchar2(10))
ERROR at line 1:
ORA-00604: error occurred at recursive SQL level 1
ORA-01116: error in opening database file 1
ORA-01110: data file 1:
'/home/oracle/app/oracle/oradata/ORCL/datafile/o1_mf_system_cpno29v4_.dbf'
ORA-27041: unable to open file
Linux-x86_64 Error: 2: No such file or directory
Additional information: 3
恢复
startup mount;
//还原数据文件
restore database;
//
RMAN> recover database;
Starting recover at 04-JUL-16
using channel ORA_DISK_1
starting media recovery
media recovery complete, elapsed time: 00:00:01
Finished recover at 04-JUL-16
RMAN> alter database open;
Statement processed
备份完成后再测试
SQL> create table test_backup (AA NUMBER);
Table created.
这时到目录下查看,发现目录已经恢复
oracle@Linx:~/app/oracle/oradata/ORCL/datafile$ ls
o1_mf_sysaux_cqn0920y_.dbf o1_mf_temp_cpno49xx_.tmp
o1_mf_sysaux_cqn0b5k6_.dbf o1_mf_undotbs1_cqn0921n_.dbf
o1_mf_sysaux_cqn0bycj_.dbf o1_mf_users_cqn09238_.dbf
o1_mf_system_cqn09207_.dbf o1_mf_users_cqn0b5lp_.dbf
o1_mf_system_cqn0b5l6_.dbf pdbseed_temp012016-06-22_05-04-40-PM.dbf
o1_mf_system_cqn0bycw_.dbf
删除所有ORCL下文件
select * from table_1;
select * from table_1
*
ERROR at line 1:
ORA-01116: error in opening database file 1
ORA-01110: data file 1:
'/home/oracle/app/oracle/oradata/ORCL/datafile/o1_mf_system_cpno29v4_.dbf'
ORA-27041: unable to open file
Linux-x86_64 Error: 2: No such file or directory
Additional information: 3
SQL> create table test_backup(aa varchar2(10));
create table test_backup(aa varchar2(10))
ERROR at line 1:
ORA-00604: error occurred at recursive SQL level 1
ORA-01116: error in opening database file 1
ORA-01110: data file 1:
'/home/oracle/app/oracle/oradata/ORCL/datafile/o1_mf_system_cpno29v4_.dbf'
ORA-27041: unable to open file
Linux-x86_64 Error: 2: No such file or directory
Additional information: 3
rman会报错
./rman target /
Recovery Manager: Release 12.1.0.2.0 - Production on Fri Jul 8 09:57:05 2016
Copyright (c) 1982, 2015, Oracle and/or its affiliates. All rights reserved.
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-00554: initialization of internal recovery manager package failed
RMAN-06003: ORACLE error from target database:
ORA-00210: cannot open the specified control file
ORA-00202: control file: '/home/oracle/app/oracle/oradata/ORCL/controlfile/o1_mf_cpno44mv_.ctl'
ORA-27041: unable to open file
Linux-x86_64 Error: 2: No such file or directory
Additional information: 3
ls
o1_mf_sysaux_cqyfxq1v_.dbf o1_mf_temp_cpno49xx_.tmp
o1_mf_sysaux_cqyfz46g_.dbf o1_mf_undotbs1_cqyfxq2p_.dbf
o1_mf_system_cqyfxpz4_.dbf o1_mf_users_cqyfxq3m_.dbf
o1_mf_system_cqyfz46r_.dbf pdbseed_temp012016-06-22_05-04-40-PM.dbf