Description
Oracle Database Server
Operating system: CentOS
ip:192.168.0.198
Port: 1521
Sid:orcl
Oracle Database version: ORACLE11GR2
Specific operation:
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 the Oracle database is installed)
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的系统环境变量设置, you must add, otherwise crontab task schedule cannot be executed.
# 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 #备份文件路径 need 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/[email protected] $orsid grants=y owner= $orowner file= $bakdir/$orowner/$bakdata log= $bakdir/$orowner/$ Baklog #执行备份
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
Every day 2:30 A.M., the database is automatically backed up into the/backup/oracledata/osyunwei directory, saved as a. tar.gz compressed file, and a backup of 7 days ago is deleted, leaving only the last 7 days of data.
Note: All of the above codes are 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.
Automatically backs up Oracle databases under Linux and deletes backups up to a specified number of days