Automatically backs up the Oracle database and deletes the backup before the specified number of days by scripting under Linux _oracle

Source: Internet
Author: User
Tags mkdir oracle database

Description

Oracle Database Server
Operating system: CentOS
ip:192.168.0.198
Port: 1521
Sid:orcl
Oracle Database version: ORACLE11GR2

Specific actions:

1, root user Login server

Mkdir-p/backup/oracledata #新建Oracle数据库备份目录
Chown-r oracle:oinstall/backup/oracledata-r #设置目录权限为oinstall用户组的oracle用户 (user Oracle and user group Oinstall are set when Oracle database is installed)

System Yun-wei www.osyunwei.com warm reminder: qihang01 original content copyright, reproduced please indicate the source and the original link

2. New Oracle Database backup script

Vi/backup/oracledata/ordatabak.sh #新建文件, enter the following code

#!/bin/sh
export oracle_base=/data/oracle
export oracle_home= $ORACLE _base/product/11.2.0/db_1
Export ORACLE_SID=ORCL
export oracle_term=xterm
export path= $ORACLE _home/bin:/usr/sbin: $PATH
Export Ld_ Library_path= $ORACLE _home/lib:/lib:/usr/lib
export lang=c
export Nls_lang=american_america. Zhs16gbk

#以上代码为Oracle数据库运行账号oracle的系统环境变量设置, must be added, otherwise the CRONTAB task schedule cannot be performed.

# Oracle User's system environment variable path:/home/oracle/.bash_profile

Date= ' Date +%y_%m_%d ' #获取系统当前日期时间
Days=7 #设置删除7天之前的备份文件
Orsid= ' 192.168.0.198:1521/ORCL ' #Oracle数据库服务器IP, Port, SID
Orowner=osyunwei #备份此用户下面的数据
Bakuser=osyunwei #用此用户来执行备份, you must have permissions to the backup operation
Bakpass=osyunwei #执行备注的用户密码
Bakdir=/backup/oracledata #备份文件路径, it needs to be created in advance.
Bakdata= $orowner "_" $date. DMP #备份数据库名称
Baklog= $orowner "_" $date. Log #备份执行时候生成的日志文件名称
ordatabak= $orowner "_" $date. tar.gz #最后保存的Oracle数据库备份文件
CD $bakdir #进入备份目录
Mkdir-p $orowner #按需要备份的Oracle用户创建目录
CD $orowner #进入目录


Exp $bakuser/$bakpass @ $orsid grants=y owner= $orowner file= $bakdir/$orowner/$bakdata log= $bakdir/$orowner/$baklog # Perform a backup
TAR-ZCVF $ordatabak $bakdata $baklog #压缩备份文件和日志文件
Find $bakdir/$orowner-type f-name "*.log"-exec rm {} \; #删除备份文件
Find $bakdir/$orowner-type f-name "*.dmp"-exec rm {} \; #删除日志文件
Find $bakdir/$orowner-type f-name "*.tar.gz"-mtime + $days-exec rm-rf {} \; #删除7天前的备份 (note: {} \ has spaces in the middle)
: Wq #保存退出

chmod +x/backup/oracledata/ordatabak.sh #添加脚本执行权限

3, Vi/etc/crontab #编辑系统任务计划, add the following code to the last line

2 * * * * oracle/backup/oracledata/ordatabak.sh #每天凌晨2点30分 to perform ordatabak.sh backup files with Oracle users

: wq! #保存退出

Service Crond Restart #重启crond

2:30 A.M. every day, the database is automatically backed up to the/backup/oracledata/osyunwei directory, saved as a. tar.gz compressed file, and deleted 7 days ago, that is, only the last 7 days of data are retained.

Note: All of the above code is entered in the English half-width state.

At this point, Linux automatically backs up the Oracle database and deletes the backup tutorial completed before the specified number of days.

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.