Home Database Mysql Tutorial 用RedHat Linux完成Oracle自动物理备份

用RedHat Linux完成Oracle自动物理备份

Jun 07, 2016 pm 04:58 PM
oracle redhat

数据库参数文件通常存储在ldquo;ORACLE/ADMIN/数据库名/pfile/rdquo;目录下,其文件命名规则为ldquo;init+数据库名.orar

Oracle数据库提供了几种数据备份方法,但用得最多的还是使用exp进行逻辑备份,而物理备份(包括联机备份和脱机备份)过程用得并不多。其主要原因是物理备份过程比较复杂,不易掌握。本文以Oracle数据库在Linux下自动物理备份(脱机备份)的实现为例描述物理备份的操作步骤,希望能帮助更多的Oracle数据库管理员了解这一物理备份过程,避免不必要的数据丢失。
本文的目标是通过执行一Shell脚本来完成Oracle数据库自动备份的全过程,而且能够在当前目录下生成其压缩文件。具体实现步骤如下:   

  1.以Oracle用户身份登录到Linux系统下。   

  2.建立database.srcipt文件,以便生成备份数据库的一些参数信息并保存在文件database.parm中。

  这些信息对于以后恢复数据库具有重要的参考作用,所以在物理备份的过程中,需要保存这些信息,也可以把其他一些重要的信息写在这个脚本里。   

  $vi database.srcipt

  spool database.parm

  --这是当前备份数据库的系统参数列表

  select * from v$parameter;

  --这是当前备份数据库的字符集部分参数

  select * from props$;

  --这是当前备份数据库数据文件存储位置及名称

  select * from v$datafile;

  --这是当前备份数据库控制文件存储位置及名称

  select * from v$controlfile;

  --这是当前备份数据库日志文件存储位置及名称

  select * from v$logfile;

  --可以在这里添加其他一些重要信息

  --开始生成备份shell文件,可参考backup.sh

  spool off

  spool backup.sh

  select 'cp '  name    ' backup/' from v$datafile ;

  select 'cp '  name    ' backup/' from v$controlfile ;

  select 'cp '  member    ' backup/' from v$logfile;

  spool off

  shutdown immediate

  exit

  !  

  3.修改上一步中生成的backup.sh文件,并执行它来完成数据库文件的操作系统备份。为清晰起见,将这段脚本命名为文件alterbackup.sh。   

  $vi alterbackup.sh

  echo “该脚本完成把数据库数据文件、控制文件、日志文件的复制到当前目录的过程”

  cat backup.sh grep ‘cp /’>c.sh

  #该语句把backup.sh中所有以“cp /”开头的语句提取出来生成新的文件c.sh

  rm backup.sh

  mv c.sh backup.sh

  chmod +x backup.sh

  . backup.sh

  #注意:点号“.”与backup.sh之间有一空格  

  4.建立数据库启动脚本,以便完成备份之后启动数据库,将这段脚本命名为startup.script。 

  $vi startup.script

  spool StartStatus.readme

  --开始启动数据库

  startup

  --数据库启动完成,可以查看StartStatus.readme文件检查数据库启动情况

  spool off

  exit

  !  

  5.为节省磁盘空间和复制到其他存储位置,建立Shell文件gzip.script来完成备份数据文件的压缩。注意生成备份文件时,在文件名中注明时间。   

  $vi gzip.sh

  echo “开始进行备份文件的压缩过程”

  thedate=‘date + %Y.%m.%d.%H.%M’

  #注意:字符串前后都有一个反引号,不是单引号

  outfile=$thedate

  tar -cvf backup$outfile.tar backup/*

  #将备份到backup目录下的所有文件生成一档案文件

  gzip backup$outfile.tar

  #把档案文件进行压缩,以节省硬盘空间

  rm -R backup #删除那些没有压缩的文件  

  6.把上面第2步到第5步生成的内容组织成一个Shell文件begin.sh,但这之前还需要先运行以下命令:  

  $chmod +x alterbackup.sh

  $chmod +x gzip.sh

  $vi begin.sh

  echo “开始进行数据库的自动物理备份过程,该Shell将在当前目录下生成backup.gz文件,

  该文件中包含数据库的一些参数信息及数据库的物理文件……”

  mkdir backup

  sqlplus internal/oracle

  . alterbackup.sh

  sqlplus internal/oracle

  . gzip.sh

  echo “数据库自动进行物理备份过程结束,请在当前目录下检查backup.tar.gz”  

  7.增加数据库参数文件的备份语句。

  数据库参数文件通常存储在“ORACLE/ADMIN/数据库名/pfile/”目录下,,其文件命名规则为“init+数据库名.ora”,数据库名缺省名称为orcl,可根据数据库安装名称来进行修改。如果不知道该文件存储在什么位置,可使用下面命令进行查找:   

  $find $ORACLE_HOME -name ‘initorcl.ora’  

  该语句可能显示出/u01/app/oracle/product/8.1.7/dbs/initorcl.ora,由于在Linux中有一种链接文件,所以要查看显示出的文件是不是链接文件,如果是,还需要进一步查看其原始文件。  

  $ll /u01/app/oracle/product/8.1.7/dbs/initorcl.ora  

  本例中显示出该文件是一个链接文件,它指向/u01/app/oracle/admin/orcl/pfile/initorcl.ora。为此,可以修改第3步的alterbackup.sh,修改结果如下(粗体显示,该语句需要根据数据库安装情况进行修改):  

  ……

  chmod +x backup.sh

  cp /u01/app/oracle/admin/orcl/pfile/initorcl.ora backup/initorcl.ora

  . backup.sh

  #注意:点号“.”与backup.sh之间有一空格 

  8.在准备进行备份时,先使用“ls -l(或ll)”命令检查当前目录下,此时应该有这样几个文件:alterbackup.Sh,begin.sh,database.script,gzip.sh,startup.script。

  此后,还应执行命令:   

  $chmod +x begin.sh  

  如果一切完成,就可以执行begin.sh来完成备份过程了:  

  . begin.sh

  #注意begin.sh与前面点号之前有一空格。  

  以后每次需要做备份时,只需运行begin.sh即可。这里也可以使用crontab自动完成按计划备份,有关如何使用crontab,请参考相关资料,也可在网上查询,本文不再做介绍。

  使用此方法进行物理备份过程,不仅备份了数据库的数据文件,也同时记录了数据库的一些重要信息(第2步的database.parm文件中),这对于以后恢复数据是非常重要的。最后要提醒读者注意的是,本文提供的方法要求有足够大的剩余磁盘空间(尽管最后只保留了备份文件的压缩文件),这个缺撼留给读者去弥补。   

  注:该文在RedHat 7.3、Oracle 8i Enterprise Edition Release 8.1.7 for Linux 下使用/bin/bash下调试通过。

linux

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What to do if the oracle can't be opened What to do if the oracle can't be opened Apr 11, 2025 pm 10:06 PM

Solutions to Oracle cannot be opened include: 1. Start the database service; 2. Start the listener; 3. Check port conflicts; 4. Set environment variables correctly; 5. Make sure the firewall or antivirus software does not block the connection; 6. Check whether the server is closed; 7. Use RMAN to recover corrupt files; 8. Check whether the TNS service name is correct; 9. Check network connection; 10. Reinstall Oracle software.

How to solve the problem of closing oracle cursor How to solve the problem of closing oracle cursor Apr 11, 2025 pm 10:18 PM

The method to solve the Oracle cursor closure problem includes: explicitly closing the cursor using the CLOSE statement. Declare the cursor in the FOR UPDATE clause so that it automatically closes after the scope is ended. Declare the cursor in the USING clause so that it automatically closes when the associated PL/SQL variable is closed. Use exception handling to ensure that the cursor is closed in any exception situation. Use the connection pool to automatically close the cursor. Disable automatic submission and delay cursor closing.

How to delete all data from oracle How to delete all data from oracle Apr 11, 2025 pm 08:36 PM

Deleting all data in Oracle requires the following steps: 1. Establish a connection; 2. Disable foreign key constraints; 3. Delete table data; 4. Submit transactions; 5. Enable foreign key constraints (optional). Be sure to back up the database before execution to prevent data loss.

How to paginate oracle database How to paginate oracle database Apr 11, 2025 pm 08:42 PM

Oracle database paging uses ROWNUM pseudo-columns or FETCH statements to implement: ROWNUM pseudo-columns are used to filter results by row numbers and are suitable for complex queries. The FETCH statement is used to get the specified number of first rows and is suitable for simple queries.

How to create cursors in oracle loop How to create cursors in oracle loop Apr 12, 2025 am 06:18 AM

In Oracle, the FOR LOOP loop can create cursors dynamically. The steps are: 1. Define the cursor type; 2. Create the loop; 3. Create the cursor dynamically; 4. Execute the cursor; 5. Close the cursor. Example: A cursor can be created cycle-by-circuit to display the names and salaries of the top 10 employees.

How to stop oracle database How to stop oracle database Apr 12, 2025 am 06:12 AM

To stop an Oracle database, perform the following steps: 1. Connect to the database; 2. Shutdown immediately; 3. Shutdown abort completely.

How to create oracle dynamic sql How to create oracle dynamic sql Apr 12, 2025 am 06:06 AM

SQL statements can be created and executed based on runtime input by using Oracle's dynamic SQL. The steps include: preparing an empty string variable to store dynamically generated SQL statements. Use the EXECUTE IMMEDIATE or PREPARE statement to compile and execute dynamic SQL statements. Use bind variable to pass user input or other dynamic values ​​to dynamic SQL. Use EXECUTE IMMEDIATE or EXECUTE to execute dynamic SQL statements.

What steps are required to configure CentOS in HDFS What steps are required to configure CentOS in HDFS Apr 14, 2025 pm 06:42 PM

Building a Hadoop Distributed File System (HDFS) on a CentOS system requires multiple steps. This article provides a brief configuration guide. 1. Prepare to install JDK in the early stage: Install JavaDevelopmentKit (JDK) on all nodes, and the version must be compatible with Hadoop. The installation package can be downloaded from the Oracle official website. Environment variable configuration: Edit /etc/profile file, set Java and Hadoop environment variables, so that the system can find the installation path of JDK and Hadoop. 2. Security configuration: SSH password-free login to generate SSH key: Use the ssh-keygen command on each node

See all articles