** Environment description **
Oracle Database Server
Operating system: CentOS
IP: 192.168.150.214
Port 1521
SID: orcl
Oracle Database version: Oracle11gR2
User: ts password: ts
** The detailed procedure is as follows **
1. Log on to the server as the root user.
{{{
Mkdir-p/backup/oracledata # Create an Oracle database backup directory
Chown-R oracle: oinstall/backup/oracledata-R # set the directory permission to oracle users in the oinstall User Group (oracle users and user group oinstall are set when Oracle database is installed)
}}}
2. Create an Oracle database backup script
Vi/backup/oracledata/ordatabak. sh # Create a file and enter the following code
{{{
#! /Bin/sh
ORACLE_BASE =/u01/app/oracle; export ORACLE_BASE
ORACLE_HOME = $ ORACLE_BASE/product/11.2.0/db_1; export ORACLE_HOME
LD_LIBRARY_PATH = $ ORACLE_HOME/lib; export LD_LIBRARY_PATH
ORACLE_SID = orcl; export ORACLE_SID
ORA_NLS33 = $ ORACLE_HOME/nls/admin/data; export ORA_NLS33
NLS_LANG = american_america.zhs16gbk; export NLS_LANG
PATH = $ ORACLE_HOME/bin: $ PATH; export PATH
** (# The above code is set for the system environment variables of the Oracle database running account oracle. It must be added; otherwise, the crontab task plan cannot be executed. Copy it directly.
# Path to the system environment variables of oracle users:/home/oracle/. bash_profile )**
Date = 'date + % Y _ % m _ % d' # obtain the current date and time of the system
Days = 7 # Delete the backup file 7 days ago
Orsid = '1970. 168.150.214: 192/orcl '# Oracle database server IP address, port, SID
Orowner = ts # back up data under this user
Bakuser = ts # use this user to perform backup. You must have the backup operation permission.
Bakpass = ts # User password for executing remarks
Bakdir =/backup/oracledata # backup file path, which must be created in advance
Bakdata = $ orowner "_" $ date. dmp # backup database name
Baklog = $ orowner "_" $ date. log # Name of the log file generated during backup execution
Ordatabak1_1_orowner1__1_1_date.tar.gz # Last saved Oracle database backup file
Cd $ bakdir # enter the backup directory
Mkdir-p $ orowner # Create a directory for Oracle users who back up data as needed
Cd $ orowner # enter the Directory
Exp $ bakuser/$ bakpass @ $ orsid grants = y owner = $ orowner file = $ bakdir/$ orowner/$ bakdata log = $ bakdir/$ orowner/$ baklog # execute backup
Tar-zcvf $ ordatabak $ bakdata $ baklog # compress backup files and log files
Find $ bakdir/$ orowner-type f-name "*. dmp"-exec rm {}\; # Delete the backup file
Find $ bakdir/$ orowner-type f-name "*. log"-exec rm {}\; # delete a log file
Find $ bakdir/$ orowner-type f-name "* .tar.gz"-mtime + $ days-exec rm-rf {}\; # Delete the backup seven days ago (note: {} \ There is a space in the middle)
: Wq # save and exit
Chmod + x/backup/oracledata/ordatabak. sh # add the script execution permission
}}}
3. vi/etc/crontab # Edit the System Task plan and add the following code to the last line.
{{{
30 2 * root/backup/oracledata/ordatabak. sh # At 02:30 every day, the root user executes ordatabak. sh to back up the file
Note: 30 16 ** 5 root/backup/oracledata/ordatabak. sh
Meaning minute hour date month week
Number range: 0-59 0-23 1-31 1-12 0-6
: Wq! # Save and exit
Service crond restart # restart crond
}}}
** At 02:30 every day, the database is automatically backed up to the/backup/oracledata/tsdirectory, saved as a .tar.gz compressed file, and deleted seven days ago, that is, only the data of the last seven days is retained. **
Note: All the above code is entered in the English half-width state.