oracle 手动 备份 恢复

手工备份, 我只考虑全备, 即 control file, redo log file, datafile, password file, spfile(pfile), listener.ora, tnsnames.ora 等全部备份. 这样,在数据库恢复时, 啥文件都全了, 能够很顺利的恢复到备份的时间点.

手动备份

分为全备 或部分备份, 一致性备份和 非一致性备份.

一致性全备: (一种冷备份)

操作方法:  目前我们系统使用的这种, 就是将数据库正常关闭, 然后在操作系统下, copy 所有的 control file, data file 等 到别的地方. 最好将 online redo log file , parameter file, password file 一起备份, 因为这样恢复的时候就会非常简单, 直接将文件copy 回来就可以了, 不用备份的是 archivelog file.

这种备份的好处是 : 操作简单.

这种备份的缺点是 :不支持 7 * 24 小时系统, 即数据库不能被关闭的情况. copy 数据需要很长时间, 恢复时直到最后的全备时刻. 剩下的内容可以依靠archilve log 文件操作.

非一致性备份(热备) : 可以单独备份 tablespace 或者 datafile 等内容.  (必须在 archivelog 模式下)

这种备份不好, 要不就全备, 要不就用 rman, 不适用这种非一致性备份(热备).

操作方法:

1. alter tablespace users begin backup;   -- 将要备份的内容设置成backup 模式

2. !cp /../users01.dbf /backup/users01.dbf  -- 操作系统的拷贝操作

3. alter tablespace users end backup;  -- 关闭 backup 模式

4. alter system archive log current;  -- 将日志归档

重复上边的每一步骤, 所有的表空间. (include SYSTEM tablespace and UNDO tablespace)

注意: 将数据库的某个tablespace 设置成 backup 模式后, more redo log entries may be generated because the log writer block images of changed blocks of the datafile in backup mode to the redo log instead of just the row information. This could have a significant impact on the size of redo logs and the performance of the log writer. 所以 BEGIN BACKUP to END BACKUP 之间的时间间隔应该尽量缩小, 这也就是为什么要一个一个的备份表空间, 而不是所有的表空间一起备份的原因.

好处: 可以数据库不关闭的情况下完成备份.

热备: 尽量选择在数据库不忙时进行. 不要用

控制文件(特殊)备份

很多操作都会改变控制文件的内容, 所以我们要经常备份控制文件.

2进制文件 : alter database backup controlfile to ‘control11.bkp’; -- 另外将 control file 保存在别的地方. 热备controlfile, 这种办法用来恢复, 也是2进制文件

text 文件 : alter database backup controlfile to trace;  -- 这个语句会重现创建control file (就在原来control file 的路径上)   -- 得到建立控制文件的脚本, 这种办法用来重建control file, 是脚本文件

               例如 alter database backup controlfile to trace as ‘/u01/user_backup/recreate_controlfile.txt’;

(这里先简单说一下使用 RMAN 备份 control file)

backup current controlfile;

backup database include current controlfile;

或者将 RMAN 设置成自动备份 controlfile : configure controlfile autobackup on;

备份 password file 和 parameter file (只要操作系统级别的copy 就可以了)


手动恢复

这里我只考虑恢复到全备的时间点.

操作办法很简单, 就是将之前的备份文件, 全部copy到原来的路径就可以了. 这样, 只恢复到了之前的时间点, 如果想一致性恢复, 参考下边的 recovery in Archivelog.  如果想要一致性恢复 从 手动全备 作为参考, 那么你需要保存好 最新的 redo log file, archivelog file, control file .

Recovery in Archivelog (完整恢复) (这部分内容也很重要)

完整的恢复, 即恢复到数据库损坏的时刻, 不是恢复到数据库的之前的某个时间点.

注意, 我们只考虑 Archivelog 模式的恢复.

步骤:

1. 首先之前要有全备的文件, 就是操作系统级别的 copy. (全备之后的所有的archivelog文件都要保存起来)

2. database 出现问题 , 比如一个数据文件丢失

3. 将全备的文件(与丢失的文件相同) copy 到正常的文件的路径, 比如从 /backup/user01.dbf –> /u01/oradata/ora10g/user01.dbf ( 注意, 此时的数据是不一致的, 因为是全备的 datafile )

4. 打开数据库到 mount 状态

5. recover database 这个命令即可. 注意, 这个命令之所以能自动完成, 要求它所需要的文件(redo log, archive log, control file)都在指定的路径上. 换句话说, 只要没有损坏redo log 和 archive log 和 control file,  然后使用 recover database; 就可以恢复了. 所以, 利用这条命令来recover时, 不能使用你之前备份的redo log file, control file(archive log file 不需要备份) 要用当前的 control file, redo 和 archivelog 才能实现 一致性恢复. (条件实在是太苛刻了, 要求 control file, redo log, archive 都不能有损坏)

以上恢复就完成了. 注意, 这样恢复完以后不用全备也可以, 因为恢复的是一致性的.

如果你要恢复到不同的路径, 比如原来的磁盘损坏了, 新增加了一块磁盘, 这时候就需要指定恢复的路径:

1. 首先, 加一块新的磁盘, 分区, 格式化.

2. 挂在, mount /dev/sdb1 md(目录), 将备份文件复制到md目录下, 注意路径

3. 修改 parameter file 中对应文件的路径 (spfile, 注意这步关键是将数据文件找到对应的正确的路径)

control file 非常重要!

综上恢复, 可知, 一般情况下, 可以参考我们系统这种方式, 每半个月全备, 出问题, 直接 recover database, 所以… 备份策略如下:

备份策略

小的系统, 参考我们系统, 半个月全备, 然后保存好所有的 archivelog 和 redo 信息, 有问题, 直接 recover database.

大的系统, 参考 rman

重建control file
  • All control files are lost becuase of a failure
  • The name of a database needs to be changed
  • The current settings in the control file need to be changed

control file 丢失, 代价是巨大的, 所以最好不要丢失 control file. 详情请看 control file 专题

原文地址:https://www.cnblogs.com/moveofgod/p/3598912.html