Automatic Oracle Database Backup

Source: Internet
Author: User
Tags ftp commands ranges
 
 
 

 

I believe that many system administrators are doing the same job every day-backing up data. Once the system fails and data recovery is required, the system is powerless. It would be nice to set a fixed time every day for the system to automatically back up data! Next I will talk about the UNIX environment based on my practical experience.OracleDatabaseOfAutomatic BackupTo serve as an example.
  
We plan to makeDatabasePerform an export backup at a.m., copy the backup file to the tape at a.m., and copy the backup file to another UNIX machine at a.m, to do this, we can perform the following operations:
  
  1. ExportDatabase
The export commandDatabaseData in is backed up into a binary file, which usually has three modes: user mode, table mode, and entireDatabaseMode. This document uses the user mode. before backup, you must create a backup directory to hold backup files. For example, you can create a/backup directory. Then we canOracleCreate two files, ora-backup and tar-backup, respectively under the directory (or other directories. It must be noted that the previous file needsOracleTo facilitate the initialization, we may wish to put the initialization command in a file (the file name is set to ora-env), and then call it from the first file.
  
1. ora-env file pairOracleParameters are initialized. The content is as follows:
  
  ORACLE-HOME = $ORACLE-HOME; exportORACLE-HOME
  
  ORACLE-SID = ora73; exportORACLE-SID
  
  ORACLE-TERM = sun; exportORACLE-TERM
  
LD-LIBRARY-PATH = $ORACLE-HOME/lib; export LD-LIBRARY-PATH
  
ORA-NLS32 = $ORACLE-HOME/ocommon/nls/admin/data; export ORA-NLS
  
PATH =.:/usr/ccs/bin:/usr/ucb: $ORACLE-HOME/bin: $ PATH; export PATH
  
DISPLAY = host1: 0; export DISPLAY
  
NLS-LANG = american-america.zhs16cgb231280; export NLS-LANG
  
2. ora-backup file pairDatabaseFor export, the exported file name can be set at will. This document specifies the letter "xx" and the date of the day. That is, if the date of the day is July 22, December 10, the exported file name is "xx1210.dmp ", different from the backup files of other dates.
  
Ora-backup file content:
  
./Oracle/Ora-env
  
# InitializationOracleDatabase
  
Rq = 'date + "% m % d ″′
  
# Assign the date of the day to the variable rq
  
Rm/backup/
  
# Clearing/backup directories
  
Exp test/test file =/backup/xx $ rq. dmp log =/backup/xx $ rq. log
  
This command is used to export the data of the user test (whose password is also test) at the $ prompt. The exported files and logs are stored in the/backup directory.
  
  Ii. tape backup
The tar-backup file copies the data files exported using the export command to the tape.
  
Tar-backup file content:
  
Tar rvf/dev/rmt/0n/backup/
  
This command backs up files generated on the current day in the/backup Directory to the tape. In this file, the tar command uses three parameters. The r option indicates that a file is written to the tape without damaging the original content of the tape. The v option indicates that the file information is displayed during the copy process, the f option is followed by the tape device name, specifying where the file is copied. The n option indicates that the tape drive is not replayed. /Dev/rmt/0 indicates the first tape drive of the UNIX host. Similarly,/dev/rmt/1 indicates the second tape drive of the UNIX host, and so on.
  
After writing the ora-env, ora-backup, and tar-backup files, run the following commands:
  
Chmod 755 ora-env
  
Chmod 755 ora-backup
  
Chmod 755 tar-backup
  
In this way, all three files become executable files.
  
  Iii. Remote Backup
We know that FTP commands are usually used to transmit data between two hosts, but they are generally implemented through interaction, that is, you need to manually enter the IP address, user name, and password of the target host. Apparently, this does not matchAutomatic Backup. Fortunately, we can achieve our goal by writing a. netrc file. This file must be named. netrc and stored in the user registration directory on the machine where the FTP command is started. The file permission should be disabled for read access by the group or other users. In this way, when a user uses an FTP command, the system will find it in the user's registration directory. if you can find the netrc file, the file will be executed first. Otherwise, the user will be prompted interactively to enter the user name and password.
  
Before using the FTP command, you should first create a directory on another UNIX machine for backup to accommodate the backup files. The directory created in this document is/pub. It should be noted that, in order to speed up the backup, the transmission rate between the two hosts should be as high as possible, preferably on the same LAN.
  
The content of the. netrc file is as follows:
  
Machine host2
  
# Host2 is the host name used for backup
  
LoginOracle
  
#OracleFor a user on the backup host
  
PasswordOracle
  
#OracleThe user's password isOracle
  
Macdef init
  
# Define a macro named init, which will be executed at the end of the automatic registration process
  
Bin
  
# Set the file transmission mode to binary
  
LCD/backup
  
# Go to the local working directory/backup
  
Cd/pub
  
# Enter the backup host directory/pub
  
Mput
  
# Transfer all files in the/backup Directory to the backup host
  
Bye
  
# Exit the FTP session Process
  
After the. netrc file is compiled, run the following command:
  
Chmod 600. netrc
  
In this way, the. netrc file can only be accessed by this user.
  
  4. Start the backup process
Cron is a permanent process started and executed by/etc/rc. local. Cron checks the files in the/var/spool/cron/crontabs/directory and finds the task to be executed and the time when the task is executed.
  
Each row of the Crontab file consists of six fields (minutes, hours, day of month, month, day of week, and command). The fields are separated by spaces or tabs:
  
Minutes: minute field. The value ranges from 0 to 59.
  
Hours: small time domain. The value ranges from 0 to 23.
  
Day of month: date. The value ranges from 1 to 31.
  
Month: month. The value ranges from 1 to 12.
  
Day of week: day of the week. The value ranges from 0 to 6. The value of Sunday is 0.
  
Command: the command to be run
  
If a domain is, the command can be executed within all possible values of the domain.
  
If a field is two numbers separated by a hyphen, the command can be executed within the range of two numbers (including the two numbers themselves ).
  
If a field is composed of a series of values separated by commas, the command can be executed within the range of these values.
  
If both the date and week fields have values, these two fields are valid.
  
Now, we write a file to startAutomatic BackupProcess. It is worth noting that this file can only be stored inOracleUse the crontab-e command under the user name to edit the file. Otherwise, it will not be scheduled and the file name will be setOracleThe file will be stored in the/var/spool/cron/crontabs directory. After editing, you canOracleRun the crontab-l command at the $ prompt.
  
  OracleFile Content:
  
0 23/Oracle/Ora-backup
  
# Every dayDatabaseExecute backup
  
0 2/Oracle/Tar-backup
  
# Back Up Files to tape at 2 o'clock every day
  
0 4 ftp-I host2
  
# Back Up Files to another host at 4 o'clock every day
  
After the above operations, the system will automatically generate a backup every night, and automatically copy the backup files to the tape and the other host. What the system administrator needs to do is to change a disk of tape (the cycle of changing the tape depends on the size of the backup file and the size of the tape) and clean up the backup directory. In this way, they can free themselves from the hassle of backing up data and do other meaningful work. WhileDatabaseBoth tape backup and remote backup are implemented, and the corresponding security is greatly improved.

From: http://www.myfaq.com.cn/A/2004-07-07/85801.html

Implementation script
Rem #=================================================== ==========================================
Rem # Purpose: exp Oracle DB ZYTK30 to file ZYTK30_YYYYMMDDHHMM.dmp,
Rem # and Delete the file, 7 days ago created
Rem # Author: Comer Chu, 2006

Rem # History:
Rem # Note: erveryday excute this script by windows task schedule
Rem #=================================================== ==========================================

@ Echo off
Rem 1 # --- get the current date and the date 7 days ago, in the format of "YYYY-MM-DD "----------
Set dt = % date :~ 0, 10%
Set I = 1

: Forok
If % I % = 1 goto i01
If % I % = 2 goto i02
If % I % = 3 goto i03
If % I % = 4 goto i04
If % I % = 5 goto i05
If % I % = 6 goto i06
If % I % = 7 goto i07

: I01
Set I = 2
Goto nextok
: I02
Set I = 3
Goto nextok
: I03
Set I = 4
Goto nextok
: I04
Set I = 5
Goto nextok
: I05
Set I = 6
Goto nextok
: I06
Set I = 7
Goto nextok
: I07
Set I = 8
Goto nextok

: Nextok
Set dy = % dt :~ 0, 4%
Set dm = % dt :~ 5, 2%
Set dd = % dt :~ 8, 2%

If % dm % dd % == 0101 goto L01
If % dm % dd % == 0201 goto L02
If % dm % dd % == 0301 goto L07
If % dm % dd % == 0401 goto L02
If % dm % dd % == 0501 goto L04
If % dm % dd % == 0601 goto L02
If % dm % dd % == 0701 goto L04
If % dm % dd % == 0801 goto L02
If % dm % dd % == 0901 goto L02
If % dm % dd % == 1001 goto L05
If % dm % dd % == 1101 goto L03
If % dm % dd %== 1201 goto L06

If % dd % = 02 goto L10
If % dd % = 03 goto L10
If % dd % = 04 goto L10
If % dd % = 05 goto L10
If % dd % = 06 goto L10
If % dd % = 07 goto L10
If % dd % = 08 goto L10
If % dd % = 09 goto L10
If % dd % = 10 goto L11
Set/A dd= dd-1
Set dt = % dy %-% dm %-% dd %
Goto END
: L10
Set/A dd = % dd :~ 1, 1%-1
Set dt = % dy %-% dm %-0% dd %
Goto END
: L11
Set dt = % dy %-% dm %-09
Goto END

: L02
Set/A dm = % dm :~ 1, 1%-1
Set dt = % dy %-0% dm %-31
Goto END
: L04
Set/A dm= dm-1
Set dt = % dy %-0% dm %-30
Goto END

: L05
Set dt = % dy %-09-30
Goto END
: L03
Set dt = % dy %-10-31
Goto END
: L06
Set dt = % dy %-11-30
Goto END
: L01
Set/A dy = DY-1
Set dt = % dy %-12-31
Goto END

: L07
Set/A "dd = dy % 4"
If not % dd % = 0 goto L08
Set/A "dd = dy % 100"
If not % dd % = 0 goto L09
Set/A "dd = dy % 400"
If % dd % = 0 goto L09
: L08
Set dt = % dy %-02-28
Goto END
: L09
Set dt = % dy %-02-29
Goto END

: END
If % I % = 2 goto forok
If % I % = 3 goto forok
If % I % = 4 goto forok
If % I % = 5 goto forok
If % I % = 6 goto forok
If % I % = 7 goto forok
If % I % = 8 goto db

: Db
Rem 2 # --- back up oracle data to the local machine, and only back up the tablespace, if the local machine is correctly configured with the Oracle client, otherwise exp cannot be executed!
Rem td-like 20060827183200
Set td = % Date :~ 0, 4% % Date :~ 5, 2% % Date :~ 8, 2% % Time :~ 0, 2% % Time :~ 3,2% % Time :~ 6, 2%
E: \ oracleback
Exp user/password @ zytk30 tablespaces = (ZYTK30_AC, ZYTK30_OP, ZYTK30_ID, ZYTK30_BAK) file = e: \ OracleBack \ ZYTK30 _ % td %. dmp log = e: \ OracleBack \ expZYTK30.Log buffer = 800000
Rar a zytk30_jwtdw..rar ZYTK30 _ % td %. dmp

Rem set dt to format "yyyymmdd"
Set dt = % dt :~ 0, 4% % dt :~ 5, 2% % dt :~ 8, 2%
Rem 3 # --- Delete the backup file ---------------------------------------------------------
Del e: \ OracleBack \ ZYTK30 _ % dt % *. rar

Rem 4 # --- Delete the dmp backup file on the current day --------------------------------------------------------
Del e: \ OracleBack \ ZYTK30 _ % td % *. dmp

Scheduled task
Save the preceding content as exp_ZYTK30.bat, and then use the task plan provided by windows to schedule the program to automatically back up data every day.
Specific steps: Control Panel-\ Task Plan-\ add task plan, select daily, select exp_ZYTK30.bat to OK.

Description
The above is the data retained for the last 7 days, one per day (of course, you can also have multiple, set the task plan ). You can modify the above Code to the last 8, 9, or even n days.

For example, change to a backup retained for 8 days.
Add it under if % I % = 7 goto i07
If % I % = 8 goto i08
: I07
Set I = 8

Add under goto nextok
: I08
Set I = 9
Goto nextok

Change if % I % = 8 goto mdb
If % I % = 8 goto forok
If % I % = 9 goto mdb

And so on to n days. You can also delete the folder and make a slight change.

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.