扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
在数据库领域,oralce数据库系统的性能,可靠性等都是大家一致公认-非常的优秀,但是他的可操作行一直是一个弱项,
很多时候让用户退却。现在的oracle公司好像已经认识到了,oracle据库系统的发展朝着更简单的使用方法,更智能的内部管理。
关于oracle数据库的备份,oracle有许多中方法,也有许多的方式,为使大家能轻松方便的完成oracle数据库的备份
我把自己工作中写的一些自动完成数据库备份的方法,教本整理一下。发给大家,希望能给朋友们一些帮助。
需要时间,希望你有耐心等,我会一集一集送给朋友们。
oracle数据库冷备份:(windows平台)
1.建立教本管理目录C:\oracle\admin\adminscript
2.建立数据库备份的脚本
在上面建立的目录下建立如下的两个教本。
2.1建立执行备份任务的脚本(批处理文件)
文件内容如下:
@echoOFF
Rem===========================================================================
RemNAME-close_Backup_run.bat
RemFUNCTION-CreatesabackupscriptforaUserManagedbackup
RemNOTES-ThisscriptwillcreateascriptandrunOScopycommands
Remonclosed(cold)database
RemMODIFIED-Mr.TianliangGuo
Rem===========================================================================
RemForWindows,setenvironmentvariablesfortherootpath.
setORACLE_SID=TSTDB
setORACLE_CONNECTSTRING=tstdb
setORACLE_BASE=c:\oracle
setORACLE_HOME=%ORACLE_BASE%\ora81
setORACLE_DATA=%ORACLE_BASE%\oradata\%ORACLE_SID%
setORACLE_ADMIN=%ORACLE_BASE%\admin\%ORACLE_SID%
%ORACLE_HOME%\bin\sqlplus/nolog@close_backup.sql>close_backup.log
2.2建立完成备份认为的命令文件(sqlplus内执行的sql脚本)
文件内容如下:
Rem===========================================================================
RemNAME-close_backup.sql
RemFUNCTION-CreatesabackupscriptforaUserManagedbackup
RemNOTES-ThisscriptwillcreateascriptandrunOScopycommands
Remonclosed(cold)database
RemMODIFIED-ksmith
Rem-TianliangGuo
Rem===========================================================================
RemSetSQL*Plusvariablestomanipulateoutput
setheadingoff
setverifyoff
setpagesize0
setlinesize600
setfeedbackoff
RemSetSQL*Plususervariablesusedinscript
defineadm='sys'
definepwd='oracle8i'
RemWindowsUservariables
definebkdir='C:\oracle\admin\TSTDB\backup'
definefilcbc='closed_backup_commands.sql'
definecopy='ocopy'
Remlogindatabase
connect&adm/&pwdassysdba;
RemCreateafilecontainingallthefilecopycommandsneededforphysicalbackup
spool&filcbc
promptRemNAME-close_backup.sql
prompt
promptRemShutdownthedatabasecleanly
promptshutdownimmediate;;
prompt
promptRembackupdatafile
select'host?'||name||'&bkdir'fromv$datafileorderby1;
prompt
promptRembackuplogfile
select'host?'||member||'&bkdir'fromv$logfileorderby1;
prompt
promptRembackupcontrolfile
select'host?'||name||'&bkdir'fromv$controlfileorderby1;
prompt
promptRembackuptemporayfile
select'host?'||name||'&bkdir'fromv$tempfileorderby1;
prompt
promptalterdatabasebackupcontrolfiletotrace;;
prompt
promptRemStartthedatabaseagain
promptstartup;;
prompt
spooloff;
RemRunthecopyfilecommands
@&
exit;
3.修改教本定义的变量
在运行教本中定义的一些变量,将其改为本地数据库安装环境的变量。
需要修改的变量有,
在close_Backup_run.bat脚本中:
setORACLE_SID=TSTDB
setORACLE_CONNECTSTRING=tstdb
setORACLE_BASE=c:\oracle
setORACLE_HOME=%ORACLE_BASE%\ora81
在close_backup.sql脚本中:
defineadm='sys'
definepwd='oracle8i'
definebkdir='C:\oracle\admin\TSTDB\backup'
4.执行数据库备份任务
在C:\oracle\admin\adminscript目录下双击close_Backup_run.bat文件or
从命令中运行C:\oracle\admin\adminscript\close_Backup_run.bat
现在你可以端起咖啡杯休息一下了
5.如果要执行自动的备份,在windows的计划任务里添加一项定期运行教本C:\oracle\admin\adminscript\close_Backup_run.bat即可
oracle数据库冷备份:(unixorlinux平台)
1.建立教本管理目录/u01/app/oracle/admin/admin_script
2.建立数据库备份的脚本
在上面建立的目录下建立如下的两个脚本。
2.1建立执行备份任务的脚本(shellscript)
文件内容如下:
#!/bin/sh
Rem===========================================================================
RemNAME-close_Backup_run.sh
RemFUNCTION-CreatesabackupscriptforaUserManagedbackup
RemNOTES-ThisscriptwillcreateascriptandrunOScopycommands
Remonclosed(cold)database
RemMODIFIED-Mr.TianliangGuo02/08/08OriginalFile
Rem===========================================================================
RemForunix,setenvironmentvariablesfortherootpath.
ORACLE_SID=TSTDB
ORACLE_CONNECTSTRING=tstdb
ORACLE_BASE=/u01/app/oracle
ORACLE_HOME=$ORACLE_BASE/product/8.1.7
ORACLE_DATA=$ORACLE_BASE/oradata/$ORACLE_SID
ORACLE_ADMIN=$ORACLE_BASE/admin/$ORACLE_SID
exportORACLE_SIDORACLE_CONNECTSTRINGORACLE_BASEORACLE_HOMEORACLE_DATAORACLE_ADMIN
$ORACLE_HOME/bin/sqlplus/nolog@close_backup.sql>close_backup.log
2.2建立完成备份认为的命令文件(sqlplus内执行的sql脚本)
文件内容如下:
Rem===========================================================================
RemNAME-close_backup.sql
RemFUNCTION-CreatesabackupscriptforaUserManagedbackup
RemNOTES-ThisscriptwillcreateascriptandrunOScopycommands
Remonclosed(cold)database
RemMODIFIED-ksmith01/01/02OriginalFile
Rem-TianliangGuo2002/08/18
Rem===========================================================================
RemSetSQL*Plusvariablestomanipulateoutput
setheadingoff
setverifyoff
setpagesize0
setlinesize600
setfeedbackoff
RemSetSQL*Plususervariablesusedinscript
defineadm='sys'
definepwd='oracle8i'
RemLinuxorUnixUservariables
definebkdir='/u05/oracle/oradata/tstdb/backup'
definefilcbc='/tmp/closed_backup_commands.sql'
definecopy='cp'
Remlogindatabase
connect&adm/&pwdassysdba;
RemCreateafilecontainingallthefilecopycommandsneededforphysicalbackup
spool&filcbc
promptRemNAME-close_backup.sql
prompt
promptRemShutdownthedatabasecleanly
promptshutdownimmediate;;
prompt
promptRembackupdatafile
select'host?'||name||'&bkdir'fromv$datafileorderby1;
prompt
promptRembackuplogfile
select'host?'||member||'&bkdir'fromv$logfileorderby1;
prompt
promptRembackupcontrolfile
select'host?'||name||'&bkdir'fromv$controlfileorderby1;
prompt
promptRembackuptemporayfile
select'host?'||name||'&bkdir'fromv$tempfileorderby1;
prompt
promptalterdatabasebackupcontrolfiletotrace;;
prompt
promptRemStartthedatabaseagain
promptstartup;;
prompt
spooloff;
RemRunthecopyfilecommands
@&
exit;
3.修改教本定义的变量
在运行教本中定义的一些变量,将其改为本地数据库安装环境的变量。
需要修改的变量有,
在close_Backup_run.bat脚本中:
setORACLE_SID=TSTDB
setORACLE_CONNECTSTRING=tstdb
setORACLE_BASE=/u01/app/oracle/
setORACLE_HOME=$ORACLE_BASE/product/8.1.7
在close_backup.sql脚本中:
defineadm='sys'
definepwd='oracle8i'
definebkdir='/u05/oracle/oradata/TSTDB/backup'
4.执行数据库备份任务
在$ORACLE_BASE/admin/admin_script目录下运行close_Backup_run.sh
[oracle@RHTadmin_script]./close_Backup_run.sh
5.执行自动备份。
在crontab文件中添加如下的一行
003**6$ORACLE_BASE/admin/admin_script/close_Backup_run.sh
解释:每周六凌晨3点执行数据库关闭,数据库备份,数据库重启。
具体的方式自己定义。
oracle数据库在线备份:(windows平台)
1. 检查你的数据库是否运行在Archive模式下。
以system连接数据库运行以下的查询语句。
SQL> select name,log_mode from v$database;
NAME LOG_MODE
--------- ------------
DEMO ARCHIVELOG
如果你的log_mode显示为archivelog, OK你的数据库可以进行在线的备份。否则参考文档修改数据为archive mode。
2. 建立教本管理目录 C:\oracle\admin\admin script
3. 建立数据库备份的脚本
在上面建立的目录下建立如下的两个脚本。
3.1 建立执行备份任务的脚本(批处理文件)
文件内容如下:
@echo OFF
Rem ===========================================================================
Rem NAME - Open_Backup_Run.bat
Rem FUNCTION - Creates a backup script for a User Managed backup
Rem NOTES - This script will create a script and run OS copy commands
Rem on closed (cold) database
Rem MODIFIED - Tianliang Guo 02/08/12
Rem ===========================================================================
Rem For Windows, set environment variables for the root path.
set ORACLE_SID=TSTDB
set ORACLE_CONNECTSTRING=tstdb
set ORACLE_BASE=c:\oracle
set ORACLE_HOME=%ORACLE_BASE%\ora81
set ORACLE_DATA=%ORACLE_BASE%\oradata\%ORACLE_SID%
set ORACLE_ADMIN=%ORACLE_BASE%\admin\%ORACLE_SID%
%ORACLE_HOME%\bin\sqlplus /nolog @Open_Backup.sql > Open_Backup.sql.log
3.2 建立完成备份认为的命令文件(sqlplus内执行的sql脚本)
文件内容如下:
Rem =====================================================================================
Rem NAME - Open_Backup.sql
Rem FUNCTION - Creates a backup command file for a User Managed Open database backup
Rem NOTES - This script will create a script and run OS copy commands
Rem on an open (hot) database. The datafile backups will be inconsistent.
Rem MODIFIED - Mr. Tianliang Guo 2002/08/11
Rem =====================================================================================
Rem
Rem Set SQL*Plus variables to manipulate output
Rem
set heading off
set pagesize 0
set linesize 600
set verify off
set trimspool on
set feedback off
Rem Set SQL*Plus user variables used in script
define adm = 'sys'
define pwd = 'oracle8i'
Rem Windows User variables
define dir = 'C:\oracle\admin\TSTDB\backup'
define fil = 'open_backup_commands.sql'
define spo = 'open_backup_output.lst'
define ctl = '&dir\control.ctl'
define cpy = 'ocopy'
Rem login database
connect &adm/&pwd as sysdba;
Rem Create a file containing all the file copy commands needed for open user managed backup
set serveroutput on
spool &fil
prompt spool &spo
prompt archive log list;;
prompt alter system switch logfile;;
prompt alter system archive log all;;
DECLARE
CURSOR cur_tablespace IS
SELECT tablespace_name
FROM dba_tablespaces;
CURSOR cur_datafile (tn VARCHAR) 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 ('host &cpy '||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 '&ctl' REUSE;;
prompt alter database backup controlfile to trace;;
prompt archive log list;;
prompt spool off
spool off;
Rem Run the copy file commands from the operating system
@&
exit;
4. 修改教本定义的变量
在运行教本中定义的一些变量,将其改为本地数据库安装环境的变量。
需要修改的变量有,
在close_Backup_run.bat脚本中:
set ORACLE_SID=TSTDB
set ORACLE_CONNECTSTRING=tstdb
set ORACLE_BASE=c:\oracle
set ORACLE_HOME=%ORACLE_BASE%\ora81
在close_backup.sql脚本中:
define adm = 'sys'
define pwd = 'oracle8i'
define dir = 'C:\oracle\admin\TSTDB\backup'
define ctl = '&dir\control.ctl'
5. 执行数据库备份任务
在$ORACLE_BASE\admin\admin_script目录下运行open_Backup_run.bat
6. 执行自动备份
在windows系统中添加计划任务,定期运行open_Backup_run.bat
oracle数据库日志备份:(windows平台)
本片是接上次的数据库在线备份,进行数据库全备份后需要备份归档的日志文件。
1. 建立数据库备份的脚本
在上面建立的目录下建立如下的两个脚本。
1.1 建立执行备份任务的脚本(批处理文件)
文件内容如下:
@echo OFF
Rem ===========================================================================
Rem NAME - archive_backup_Run.bat
Rem FUNCTION - Creates a backup script for a User Managed backup
Rem NOTES - This script will create a script and run OS copy commands
Rem on closed (cold) database
Rem MODIFIED - ksmith 01/01/02 Original File
Rem ===========================================================================
Rem For Windows, set environment variables for the root path.
set ORACLE_SID=TSTDB
set ORACLE_CONNECTSTRING=tstdb
set ORACLE_BASE=c:\oracle
set ORACLE_HOME=%ORACLE_BASE%\ora92
set ORACLE_DATA=%ORACLE_BASE%\oradata\%ORACLE_SID%
set ORACLE_ADMIN=%ORACLE_BASE%\admin\%ORACLE_SID%
%ORACLE_HOME%\bin\sqlplus /nolog @archive_backup.sql > archive_backup.log
1.2 建立完成备份认为的命令文件(sqlplus内执行的sql脚本)
文件内容如下:
Rem =====================================================================================
Rem NAME - archive_backup.sql
Rem FUNCTION - Creates a backup command file for a User Managed Archive Log backup
Rem NOTES - This script will create a script and run OS copy commands
Rem on an open or closed database.
Rem MODIFIED - Mr. Tianliang Guo 2002/08/19
Rem =====================================================================================
Rem Set SQL*Plus variables to manipulate output
set feedback off
set pagesize 0
set heading off
set verify off
set linesize 150
set trimspool on
Rem Set SQL*Plus user variables used in script
define adm = 'sys'
define pwd = 'oracle9i'
Rem for Windows
define dir = 'C:\oracle\admin\TSTDB\backup\archive log'
define fil = 'archive_backup_commands.sql'
define spo = 'archive_backup_output.lst'
define cpy = 'copy'
Rem login database
connect &adm/&pwd as sysdba;
Rem Create a file containing all the file copy commands needed for open physical backup
spool &
prompt spool &;
prompt alter system archive log all;;
SELECT 'host &cpy '||name||' &dir'
FROM v$archived_log
WHERE completion_time >= trunc(sysdate)-1
AND completion_time < trunc(sysdate);
prompt spool off;;
spool off;
Rem Run the copy file commands from the operating system
rem @&
exit;
2. 修改教本定义的变量
在运行教本中定义的一些变量,将其改为本地数据库安装环境的变量。
需要修改的变量有,
在close_Backup_run.bat脚本中:
set ORACLE_SID=TSTDB
set ORACLE_CONNECTSTRING=tstdb
set ORACLE_BASE=c:\oracle
set ORACLE_HOME=%ORACLE_BASE%\ora81
在close_backup.sql脚本中:
define adm = 'sys'
define pwd = 'oracle8i'
define dir = 'C:\oracle\admin\TSTDB\backup'
define ctl = '&dir\control.ctl'
3. 执行数据库备份任务
在$ORACLE_BASE\admin\admin_script目录下运行open_Backup_run.bat
4. 执行自动备份
在windows系统中添加计划任务,定期运行open_Backup_run.bat
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。