在打开状态下备份数据库
一、备份数据文件和控制文件
1、 创建热备份脚本open_backup.sql:
set feedback off pagesize 0 heading off verify off linesize 100 trimspool on
define dir=’/oradata/practice/backup’
define fil=’/tmp/open_backup_commands.sql’
define spo=’&dir/open_backup_output.lst’
prompt *** Spooling to &fil
set serveroutput on
spool &fil
prompt spool &spo
prompt archive log list;;
prompt alter system switch logfile;;
declare
cursor cur_tablespace is select tablespace_name from dba_tablespaces where status<>’READ ONLY’;
cursor cur_datafile(tn varchar) is select file_name from dba_data_files where tablespace_name=tn;
begin
for ct in cur_tablesapce 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(‘host 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/backup.ctl’’ reuse;’
prompt archive log list;;
prompt spool off;
spool off;
@&fil;
该脚本将会自动搜索数据库中表空间信息,然后逐个将表空间置为备份状态,再通过表空间名称索索表空间所包含的数据文件,然后调用操作系统命令将数据文件进行拷贝备份。最后还要备份控制文件,这些命令最终会输入到变量fil所指定的文件中,然后通过执行该命令文件来实现热备份。在执行备份过程中,可以通过另一个SQL*PLUS会话查询V$backup视图,来检查那个文件现在处于备份模式。也可以在运行备份脚本之后,通过如下方式察看备份状态:
①alter tablespace users begin backup;将用户表空间置于备份模式
②select d.tablespace_name,b.file#,b.status,b.change#,b.time
from dba_data_files d,v$backup b
where b.file#=d.file_id
order by d.tablespace_name;
运行该查询察看数据文件备份情况
③alter tablespace users end backup;结束用户表空间备份状态
二、备份归档日志文件:
1、创建备份脚本archive_backup.sql:
define dir=’/oradata/practice/backup’
define fil=’/tmp/archive_backup_commands.sql’
spool &fil
prompt archive log next;;
select ‘host mv ‘||name||’ &dir’ from v$archived_log
where completion_time>=trunk(sysdate)-1 and completion_time<trunk(sysdate);
spool off;
@&fil
2、通过SQL*PLUS以SYS用户身份登录practice数据库,运行该脚本,对归档文件进行备份。