Linux-based Oracle database management PART6 (backup-related scripts)

Source: Internet
Author: User
Tags file copy textout sqlplus

Here are just a few ways to introduce backup methods

Backup: Logical backup, cold backup, hot backup

Logical backups are also known as import, export, which is the role of transferring data between different Oracle databases

Physical backup, which is the OS-level file copy

Rman Backup is the primary backup method for Oracle, and the Rman backup principle is similar to physical backup, but more powerful, with the following key features:

1) command line mode

2) backing up only non-empty chunks in the data file, rather than backing up the entire data file, obviously saves storage space and backup time

3) Incremental backup that backs up only blocks of data that have changed since the last backup operation

4) can set up automatic backup control files and SPFile

Cold backup: The database is fully backed up in a normal shutdown state because the transaction ends normally, so cold backups can be used to directly recover the database without having to apply the redo log.

Hot backup: Is backed up in a state where the database is open, the data of the backup file is inconsistent, so the archive redo log or online redo log must be applied to the consistent state when you do a warm-up.

Full recovery: Borrows the Redo log and archive log to restore the database to a consistent state.

Incomplete recovery: Due to the loss of archived logs or online log files, another scenario is that the user performed an error operation and needs to revert to the previous state of the database, similar to flashback

Restore: The process of copying a backup file back to the original directory

Recovery (recover): After a restore, apply an archived redo log or online redo log to the backup file to keep its data up to date.

Full-Library cold standby shell
#!/bin/bashBackup_dir=/oracle/backlog_file = $ORACLE _base/admin/$ORACLE _sid/cold_backup_$oracle_sid.logecho"Begin Cold Backup"> $log _filedate >> $log _filesqlplus-s/as sysdba << EOF > $log _fileset pagesize 0set linesize 1000spool File_copy_$oracle_sid.shselect ' CP ' | | name | | ' $backup _dir/' from v\ $controlfile; Select ' CP ' | | filename | | ' $backup _dir/' from Dba_data_files;select ' cp ' | | Member | | ' $backup _dir/' from v\ $logfile; spool off# Prepare finish, now start shutdown DB and move the fileShutdown Immediate!bash file_copy_$oracle_sid.shstartup;exit;eof# Clean up the battlefieldRm-f file_copy_$oracle_sid.shif[-E $ORACLE _home/dbs/init$oracle_sid.ora]then cp $ORACLE _home/dbs/init$oracle_sid.ora $backup _DIR/FIif[-E $ORACLE _home/dbs/spfile$oracle_sid.ora]then# cp $ORACLE _home/dbs/spfile$oracle_sid.ora $backup _dir/Fiif[-E $ORACLE _home/dbs/orapw$oracle_sid]then# cp $ORACLE _home/dbs/prapw$oracle_sid $backup _dir/Fiecho"Cold backup finished.">> $log _filedate >> $log _file
Full-Library hot-standby shell

Physical hot standby, only one table space can be backed up at a time, and the table space to be backed up is placed in the backup state, and the backup state is ended after the hot spare is complete. Using hot standby recovery is sure to use archived logs, so if you want to use hot spares, the database must use archive mode

Alter TABLESPACE TABLESPACE_NAME BEGIN backup

Alter TABLESAPCE Tablespace_name End Backup

#!/bin/bashBackup_dir =/oracle/backuplog_file = $ORACLE _base/admin/$ORACLE _sid/hot_bakcup_$oracle_sid.logecho"Begin hot backup ..."> $log _filedate >> $log _filesqlplus-s/as sysdba << EOF >> $log _filealter Database backup Controlfil E to '/$backup _dir/backup_controlfile.ctl '; set pagesize 0set linesize 1000set feedback offcolumn tablespace_name Noprincolumn sortorder noprincolumn textout format a120spool hot_backup_$oracle_sid.sqlselect tablespace_name,' 1 'SortOrder' Alter Tablespace '|| Tablespace_name | |' begin backup; 'Textoutfrom Dba_data_filesunionselect Tablespace_name,' 2 'SortOrder' host CP '|| file_name | |"'||' $backup _dir 'Textoutfrom Dba_data_filesunionselect Tablespace_name,' 3 'SortOrder' Alter Tablespace '|| Tablespace_name | |' end backup; 'Textoutfrom Dba_data_filesorder by Tablespace_name, SortOrder, Textout;select' alter system archivelog current; 'From Dual;spool off@hot_backup_$oracle_sid.sqlexiteofrm-f Hot_backup_$oracle_sid.sqlecho"Hot Bakcup finished.">> $log _file
RMAN Backup Shell Program

Before you perform an Rman backup, set up the database archive and configure the directory where the Rman backup files are located and the automatic backup control files and SPFile.

Using Rman to back up the database, depending on where the backup information is stored (note that not the backup set, but the information (metadata) of the backup set, can be divided into two ways: one is to save the backup set information using the control file of the backup database (the database to be backed up as the target database), The second is to use another database to save the backup set information, the database is generally a directory (catalog) database), it is clear that the latter is more secure, but first configure the catalog database. In the first way, no configuration is required, except for Rman.

Below is the 1th approach, because the main reason is to introduce the Rman shell program

First, confirm whether it is an archive mode:

Sqlplus/nolog, Conn/as SYSDBA

Archive log list;

--------------------------------------------------------------------------------

If non-archive mode, alter system set log_archive_dest_1 = ' Location=/oracle/arch ';

Then restart the database to mount state to set the run archive mode:

Startup Mount

ALTER DATABASE Archivelog;

ALTER DATABASE open;

Archive log list; --Confirm whether the modification was successful

--------------------------------------------------------------------------------

Configuring Rman

By default, the backup file generated by Rman is created under the path specified by the Flash_recovery_area parameter of the database, and the following first configures the Flash_recovery_area size and path, as follows:

Sqlplus/nolog, Conn/as SYSDBA

alter system set DB_RECOVERY_FILE_DEST_SIZE=2G;

Alter system set db_recovery_file_dest= '/oracle/flash_recovery_area ';

Then, you can start Rman and connect to the database as a SYS user, backing up the data file 4:

Rman Target/

Backup DataFile 4;

Then, in the directory we just set up, you'll see the backup file.

Settings, the control file and SPFile are backed up each time the Rman backup is backed up:

Connect Target/

Configure Controlfile Autobackup on

#!/bin/bash  if  [$]then backup_level = $ 1else  backup_level = 0fibackup_user = SYSBACKUP_USER_PW = Oracle# Caltalog _user = Rman  # catalog_user_pw = Rman  log_file=/oracle/admin/$ORACLE _sid/rman_ Backup.logecho  "Begin Rman backup ..."  > $log _filedate >> $log _filerman Target $backup _user/$backup _user_pw << EOF > $log _file# catlog $catlog _user/[email protected]  Bakcup Incremental level = $backup _level database;quit; Eofecho  "Rman backup finished."  >> $log _filedate >> $log _file 

Logical Backup Shell
#!/bin/bash  log_file =/oracle/admin/$ORACLE _sid/full_exp_$ Oracle_sid.logexp_arguments= "userid=system/oracle buffer = 10485760 Owner = $"  if  [$]then exp_arguments = " $exp _arguments file = $ " else
     exp_arguments =  "$exp _arguments file ="  $ORACLE _sid fiecho " Begin Full db Export ... " > $log _fileecho " Exporting with folling arguments: $exp _arguments " >> $log _fileexp $exp _arguments >> $log _file 2>&A Mp;1echo  "Export finished."  >> $log _filedate >> $log _fileexp_result = $?if  [Exp_result! = 0]then mail-s  "Errors in xeporting $ORACLE _sid."  [email protected] < $log _filefi 

Linux-based Oracle database management PART6 (backup-related scripts)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.