在Linux中Oracle数据库备份的方法有很多,就像mysql一样可以使用不同方法进行备份oracle数据库

先来介绍一些不使用脚本我们直接使用命令备份与还原oracle数据库

Oracle数据备份:

步骤 1 备份用户数据。

1.使用linux系统下的数据库管理员账号连接linux终端。

2. 执行以下语句,创建“bak_dir”文件夹。

mkdir bak_dir

3.

执行以下语句,为“bak_dir”文件夹赋予读、写和执行权限。

chmod 777 bak_dir

4.

执行以下语句,以sysdba用户登录oracle数据库服务器。

sqlplus 数据库管理员账号/密码@数据库实例名 as

sysdba

5.

执行以下语句,将“bak_dir”指定为“/opt/oracle/bak_dir”。

create or replace directory bak_dir as

'/opt/oracle/bak_dir'

commit

6.

执行以下语句,将“bak_dir”的读、写和执行权限赋给xx(数据用户名)用户。

grant all on directory bak_dir to

xx

commit

7. 执行以下语句,退出oracle数据库服务器。

quit

执行以下语句,将sysdb用户的表备份到“bak_dir”目录下。

8.

expdp xx(要导出表所在的数据库用户名)/xx(密码)@数据库实例名

directory=bak_dir dumpfile=expdb_xx.dmp logfile=expdb_xxlog

tables=表名

Oracle数据还原:

1.在命令行输入:

sqlplus "/as sysdba"

2.

执行以下语句登录xx数据库,用户名:xx,密码:xx(请输入当地实际密码)

conn xx/xx;

3.如果恢复的表中有涉及到触发器的请停止触发器,例:A表

alter table A disable all

triggers;

commit;

4. 执行以下语句退出当前用户

quit;

5. 执行以下语句,恢复用户数据。

[mw_shl_code=php,true] impdp xx/xx@数据库实例名 directory=bak_dir

table_exists_action=truncate dumpfile=expdb_xx.dmp

logfile=impdb_xx.log[/mw_shl_code]

5.在命令行输入:sqlplus "/as sysdba"

6.

执行以下语句登录xx数据库,用户名:xx,密码:xx(请输入当地实际密码)

conn xx/xx;

7. 执行以下语句打开被禁止的触发器

alter table A enable all

triggers;

commit;

上面方法是可以实现我们想要的,但但大型WEB服务器肯定是要自动定时进行备份的。

1.--创建数据表空间

2.create tablespace test_data

3.logging

4.datafile

'/u01/app/oradata/test/TEST.dbf'

5.size 32m

6.autoextend on

7.next 32m maxsize 2048m

8.extent management local;

9.

10.--创建用户并指定表空间

11.create user TEST identified by

123

12.default tablespace test_data

13.temporary tablespace temp;

14.

15.--给用户授予权限

16.grant connect,resource to

TEST;

用Test用户登录,创建一个表,并插入两条数据:

create table t1(

Id varchar(50) primary key,

title varchar(50)

);

insert into t1

values(sys_guid(),'t1');

insert into t1

values(sys_guid(),'t2');

commit;

先写一个导出的脚本文件:

[mw_shl_code=php,true]export

ORACLE_BASE=/u01/app

export

ORACLE_HOME=/u01/app/oracle

export ORACLE_SID=TEST

export

PATH=$ORACLE_HOME/bin:$PATH

d=$(date ' %Y%m%d')

exp TEST/123@testfile=/home/oracle/backup/$d.dmp

log=/home/oracle/backup/$d.log owner=TEST

zip -m /home/oracle/backup/$d.zip

/home/oracle/backup/$d.dmp

/home/oracle/backup/$d.log[/mw_shl_code]

前4句是设置环境变量,因为crontab定时调用时,不会使用oracle用户的环境变量,所以要先把它们导进来。第6行以当前日期作为导出的文件名,例如20120626.dmp和20120626.log。第7行把这两个文件打成一个zip包,并删掉这两个文件。

要用chmod命令把这个sh标记为可执行:

chmod x backup.sh

用oracle用户,输入crontab

-e命令,编辑oracle用户的任务计划:

代码如下

1.[oracle@localhost backup]$ crontab

-e

2.42 13 * * * /home/oracle/backup/backup.sh

这样就添加了一个计划,在每天的13点42分运行/home/oracle/backup/backup.sh。

这样就可以了,利用linux计划任务就实现了。

备份策略:

星期天 0 级

星期一,二,四,五,六 2 级

星期三 1 级

--创建本地管理路径

mkdir -p

/dinglp/ora_managed/backup

mkdir -p

/dinglp/ora_managed/backup

mkdir -p

/dinglp/ora_managed/backup/export-

mkdir -p /dinglp/ora_managed/backup/log

mkdir -p /dinglp/ora_managed/backup/rman_backup

mkdir -p /dinglp/ora_managed/scripts

--创建rman表空间和rman用户

create tablespace rman_tbs datafile '/oradata/luke/rman_tbs01.dbf'

size 1024M;

create user rman_dlp identified by dlp default tablespace rman_tbs

temporary tablespace temp;

grant connect,resource ,recovery_catalog_owner to rman;

--注册catalog 数据库

rman catalog rman_dlp/dlp

create catalog tablespace rman_tbs;

connect target sys/dg@priamry

register database;

report schema;

--设置备份参数

configure retention policy to redundancy 2;

configure retention policy to recovery window of 7 days;

--以下是备份脚本(可以通过vi进行编辑)

dlp-> touch exp_rman.par

dlp-> touch exp_rman.sh

dlp-> touch rman_bk_LEVEL0.rcv (数据库0级备份)

dlp-> touch rman_bk_LEVEL0.sh

dlp-> touch rman_bk_LEVEL1.rcv (数据库1级备份)

dlp-> touch rman_bk_LEVEL1.sh

dlp-> touch rman_bk_LEVEL2.rcv (数据库2级备份www.linuxidc.com)

dlp-> touch rman_bk_LEVEL2.sh

--倒出RMAN用户数据脚本exp_rman.par

##################################################

### exp_rman.par ###

##################################################

userid=rman_dlp/dlp

file=/dinglp/ora_managed/backup/export/rman.dmp

log=/dinglp/ora_managed/backup/log/rman.log

--倒出RMAN数据SHELL脚本exp_rman.sh

##################################################

### exp_rman.sh ###

##################################################

#!/bin/bash

source /home/Oracle/.bash_profile

cd /dinglp/ora_managed/scripts

exp parfile=exp_rman.par

--零级备份RMAN脚本rman_bk_LEVEL0.rcv

connect catalog rman_dlp/dlp

connect target sys/dg@primary

run {

allocate channel d1 type disk;

allocate channel d2 type disk;

backup incremental level 0 database format

'/dinglp/ora_managed/backup/rman_backup/level0_%d_%s_%p_%u.bak'

tag='level 0' include current controlfile;

sql 'alter system archive log current';

backup archivelog all format

'/dinglp/ora_managed/backup/rman_backup/log_%d_%s_%p_%u.bak' delete

all input;

release channel d2;

release channel d1;

}

crosscheck backup;

delete noprompt expired backup;

delete noprompt obsolete;

resync catalog;

exit;

--零级备份SHELL脚本的rman_bk_LEVEL0.sh

#####################################################################

### rman_bk_LEVEL0.sh ###

#####################################################################

#!/bin/bash

source /home/Oracle/.bash_profile

cd /dinglp/ora_managed/scripts

rman cmdfile=rman_bk_LEVEL0.rcv

msglog=$HOME/backup/log/rman_bk_LEVEL0.log

./dinglp/ora_managed/script/exp_rman.sh

--一级差异增量备份RMAN脚本rman_bk_LEVEL1.rcv

connect catalog rman_dlp/dlp

connect target sys/dg@primary

run {

allocate channel d1 type disk;

backup incremental level 1 format

'/dinglp/ora_managed/backup/rman_backup/level1_%d_%s_%p_%u.bak' tag

= 'level 1' database;

sql 'alter system archive log current';

backup archivelog all format

'/dinglp/ora_managed/backup/rman_backup/log_%d_%s_%p_%u.bak' delete

all input;

release channel d1;

}

crosscheck backup;

delete noprompt expired backup;

delete noprompt obsolete;

resync catalog;

exit;

--一级差异增量备份SHELL脚本rman_bk_LEVEL1.sh

#####################################################################

### rman_bk_LEVEL1.sh ###

#####################################################################

#!/bin/bash

source /home/Oracle/.bash_profile

cd /dinglp/ora_managed/scripts

rman cmdfile=rman_bk_LEVEL1.rcv

msglog=/dinglp/ora_managed/backup/log/rman_bk_LEVEL1.log

. /dinglp/ora_managed/scripts/exp_rman.sh

--二级差异增量备份RMAN脚本rman_bk_LEVEL2.rcv

connect catalog rman_dlp/dlp

connect target sys/dg@primary

run {

allocate channel d1 type disk;

backup incremental level 2 format

'/dinglp/ora_managed/backup/rman_backup/level2_%d_%s_%p_%u.bak' tag

= 'level 2' database;

sql 'alter system archive log current';

backup archivelog all format

'/dinglp/ora_managed/backup/rman_backup/log_%d_%s_%p_%u.bak' delete

all input;

release channel d1;

}

crosscheck backup;

delete noprompt expired backup;

delete noprompt obsolete;

resync catalog;

exit;

--二级差异增量备份SHELL脚本rman_bk_LEVEL2.sh

#####################################################################

### rman_bk_LEVEL2.sh ###

#####################################################################

#!/bin/bash

source /home/Oracle/.bash_profile

cd /dinglp/ora_managed/scripts

rman cmdfile=rman_bk_LEVEL2.rcv

msglog=/dinglp/ora_managed/backup/log/rman_bk_LEVEL2.log

. /dinglp/ora_managed/scripts/exp_rman.sh

--提高RMAN增量备份性能

alter database enable block change tracking using file

'/u01/app/Oracle/admin/devdb/bdump/luke.log';

desc v$block_change_tracking;

--RMAN 动态视图

V$ARCHIVED_LOG 显示在数据库中已经创建、备份或清除的归档文件。

V$BACKUP_CORRUPTION 显示在备份集的备份过程中找到的损坏块。

V$COPY_CORRUPTION 显示映像复制过程中找到的损坏块。

V$BACKUP_DATAFILE 用于通过确定各数据文件中的块数来创建大小相同的备份集。通过它也可以找出数据文件中已损坏的块数。 V$BACKUP_REDOLOG 显示在备份集中存储的归档日志。

V$BACKUP_SET 显示已经创建的备份集。

V$BACKUP_PIECE 显示为备份集创建的备份片。

--如何监视复制进程

使用 SET COMMAND ID 命令可将服务器会话与通道联系起来。

查询 V$PROCESS 和 V$SESSION,可以确定会话与哪些 RMAN 通道对应。

查询 V$SESSION_LONGOPS,可以监视备份和复制的进度。

--linux下自动运行备份脚本

crontab格式简介

第1列分钟1~59

第2列小时1~23(0表示子夜)

第3列日1~31

第4列月1~12

第5列星期0~6(0表示星期天)

第6列要运行的命令

[mw_shl_code=html,true][root@dlp ~]# vi /etc/crontab

SHELL=/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin

MAILTO=root

HOME=/

# run-parts

01 * * * * root run-parts /etc/cron.hourly

02 4 * * * root run-parts /etc/cron.daily

22 4 * * 0 root run-parts /etc/cron.weekly

42 4 1 * * root run-parts /etc/cron.monthly

00 22 * * 0 root

/dinglp/ora_managed/scripts/rman_bk_LEVEL0.sh

00 22 * * 3 root

/dinglp/ora_managed/scripts/rman_bk_LEVEL1.sh

00 22 * * 1,2,4,5,6 root

/dinglp/ora_managed/scripts/rman_bk_LEVEL2.sh

Logo

一站式 AI 云服务平台

更多推荐