所谓热备就是数据库处于open状态并且运行在archive模式下。
如何设置数据库为archive模式,请看下面例子。
- SQL> shutdown immediate
- Database closed.
- Database dismounted.
- ORACLE instance shut down.
- SQL> startup mount
- ORACLE instance started.
- Total System Global Area 236000356 bytes
- Fixed Size 451684 bytes
- Variable Size 201326592 bytes
- Database Buffers 33554432 bytes
- Redo Buffers 667648 bytes
- Database mounted.
- SQL> alter database archivelog;
- Database altered.
- SQL> alter system archive log start;
- System altered.
- SQL> alter database open;
- Database altered.
- SQL> archive log list
- Database log mode Archive Mode
- Automatic archival Enabled
- Archive destination /zwc/oracle/dbs/arch
- Oldest online log sequence 4
- Next log sequence to archive 6
- Current log sequence 6
- SQL>
简单的介绍下操作
1. 干净的关闭数据库。
2. 把数据库启动到mount状态,不打开数据库。
3. 设置数据库为归档模式。
4. 启动系统自动归档(Oracle10g以后的版本包括10g,设置为归档模式后系统会自动开启自动归档,也就是Automatic archival Enabled,我这里用的是9i版本)。
5. 打开数据库。
6. 查看当前归档信息。
执行以下的脚本就可以对你想备份的表空间进行copy了。
- SQL> alter tablespace tbs_zhong begin backup;
- Tablespace altered.
- SQL> alter tablespace tbs_zhong end backup;
- Tablespace altered.
如果是noarchive模式执行上面命令会出现什么呢?
- SQL> alter database noarchivelog;
- Database altered.
- SQL> alter database open;
- Database altered.
- SQL> alter tablespace tbs_zhong begin backup;
- alter tablespace tbs_zhong begin backup
- *
- ERROR at line 1:
- ORA-01123: cannot start online backup; media recovery not enabled
看它报错了,意思是你无法在线备份。
我们还是把数据库设置为归档模式,命令就不演示了。
我们下面开始备份数据文件,和控制文件,redo log file是不要备份的,因为他一直在写入。
我的Oracle服务器是在Linux环境下,由于我们是学习环境,数据文件和控制文件可能不会很多,在这里我只新建了一个表空间。但是在测试环境或者生产环境下,表空间可能会很多,所以我们一个个的敲命令肯定是不现实的,所以在这里我写了一个脚本,大家看下。
图片
代码脚本
- /*设置些头参数,生成的sql脚本没有分页,排版整齐,去掉左右空格,屏幕输出*/
- set feedback off pagesize 0 heading off verify off linesize 32767 trimspool on termout off serveroutput on
- remark 备份目录要先建立好,并赋给ORACLE用户所有,确保有足够的空间
- define dir='/backup'
- define fil='&dir/open_backup_commands.sql'
- define spo='&dir/open_backup_output.log'
- spool &fil
- prompt connect sys/go@zhong as sysdba;
- prompt alter system switch logfile;
- /*tablespace是read only和临时的不需要备份*/
- declare
- cursor cur_tablespace is
- select tablespace_name from dba_tablespaces where status <> 'READ ONLY' and contents <> 'TEMPORARY';
- cursor cur_datafile (tn varchar2) is
- select file_name from dba_data_files where tablespace_name=tn;
- begin
- for ct in cur_tablespace loop
- dbms_output.put_line('alter tablespace '||ct.tablespace_name||' begin backup;');
- for cd in cur_datafile (ct.tablespace_name) loop
- dbms_output.put_line('!cp '||cd.file_name||' &dir;');
- end loop;
- dbms_output.put_line('alter tablespace '||ct.tablespace_name||' end backup;');
- end loop;
- end;
- /
- prompt alter system switch logfile;
- prompt alter database backup controlfile to '&dir/open_backup.ctl' reuse;
- prompt alter database backup controlfile to trace;
- spool off;
- remark 写好脚本先别加@&fil执行,看看输出是否正确,确认无误在加上@&执行,控制文件的reuse表示如果目标文件存在则覆盖
- spool &spo
- @&fil
- spool off
我的脚本是放在$ORACLE_HOME目录下,所以以sys用户执行open_backup.sql这个文件就可以。
下面我们开始执行这个脚本开始手工热备份。
好,脚本执行结束,我们看下backup目录,有没有我们想要的东西。
我们可以看到除了创建好数据库默认创建的表空间都备份过来了,还有控制文件backup.ctl,刚刚自己创建的数据文件tbs_zhong.dbf,以及open_backup_commands.sql脚本文件和open_backup_output.log日志文件。
我们看下它带我们生成的脚本文件。
代码脚本
- SQL> prompt connect sys/go@zhong as sysdba;;
- connect sys/go@zhong as sysdba;
- SQL> prompt alter system switch logfile;;
- alter system switch logfile;
- SQL> declare
- 2 cursor cur_tablespace is
- 3 select tablespace_name from dba_tablespaces where status <> 'READ ONLY' and contents <> 'TEMPORARY';
- 4 cursor cur_datafile (tn varchar2) is
- 5 select file_name from dba_data_files where tablespace_name=tn;
- 6 begin
- 7 for ct in cur_tablespace loop
- 8 dbms_output.put_line('alter tablespace '||ct.tablespace_name||' begin backup;');
- 9 for cd in cur_datafile (ct.tablespace_name) loop
- 10 dbms_output.put_line('!cp '||cd.file_name||' &dir;');
- 11 end loop;
- 12 dbms_output.put_line('alter tablespace '||ct.tablespace_name||' end backup;');
- 13 end loop;
- 14 end;
- 15 /
- alter tablespace SYSTEM begin backup;
- !cp /zwc/oradata/zhong/system01.dbf /backup;
- alter tablespace SYSTEM end backup;
- alter tablespace UNDOTBS1 begin backup;
- !cp /zwc/oradata/zhong/undotbs01.dbf /backup;
- alter tablespace UNDOTBS1 end backup;
- alter tablespace CWMLITE begin backup;
- !cp /zwc/oradata/zhong/cwmlite01.dbf /backup;
- alter tablespace CWMLITE end backup;
- alter tablespace DRSYS begin backup;
- !cp /zwc/oradata/zhong/drsys01.dbf /backup;
- alter tablespace DRSYS end backup;
- alter tablespace EXAMPLE begin backup;
- !cp /zwc/oradata/zhong/example01.dbf /backup;
- alter tablespace EXAMPLE end backup;
- alter tablespace INDX begin backup;
- !cp /zwc/oradata/zhong/indx01.dbf /backup;
- alter tablespace INDX end backup;
- alter tablespace ODM begin backup;
- !cp /zwc/oradata/zhong/odm01.dbf /backup;
- alter tablespace ODM end backup;
- alter tablespace TOOLS begin backup;
- !cp /zwc/oradata/zhong/tools01.dbf /backup;
- alter tablespace TOOLS end backup;
- alter tablespace USERS begin backup;
- !cp /zwc/oradata/zhong/users01.dbf /backup;
- alter tablespace USERS end backup;
- alter tablespace XDB begin backup;
- !cp /zwc/oradata/zhong/xdb01.dbf /backup;
- alter tablespace XDB end backup;
- alter tablespace TBS_ZHONG begin backup;
- !cp /zwc/oradata/zhong/tbs_zhong.dbf /backup;
- alter tablespace TBS_ZHONG end backup;
- SQL> prompt alter system switch logfile;;
- alter system switch logfile;
- SQL> prompt alter database backup controlfile to '&dir/backup.ctl' reuse;;
- alter database backup controlfile to '/backup/backup.ctl' reuse;
- SQL> prompt alter database backup controlfile to trace;;
- alter database backup controlfile to trace;
- SQL> spool off;
至此 手工热备演示完毕。