侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

Linux中Oracle数据库备份

linux  /  管理员 发布于 7年前   318

先来介绍一些不使用脚本我们直接使用命令备份与还原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. 执行以下语句,恢复用户数据。
复制代码 代码如下:
 impdp xx/xx@数据库实例名 directory=bak_dir table_exists_action=truncate dumpfile=expdb_xx.dmp logfile=impdb_xx.log

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;

先写一个导出的脚本文件:
复制代码 代码如下:
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@TEST file=/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
 
前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列要运行的命令
[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
--完毕,RYOHEI,2010-08-04。
 


  • 上一条:
    Linux下的Oracle启动脚本及其开机自启动
    下一条:
    linux自动运行rman增量备份脚本
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在Linux系统中使用Iptables实现流量转发功能流程步骤(0个评论)
    • vim学习笔记-入门级需要了解的一些快捷键(0个评论)
    • 在centos7系统中实现分区并格式化挂载一块硬盘到/data目录流程步骤(0个评论)
    • 在Linux系统种查看某一个进程所占用的内存命令(0个评论)
    • Linux中grep命令中的10种高级用法浅析(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-11
    • 2017-07
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-03
    • 2022-04
    • 2022-08
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-06
    • 2023-07
    • 2023-10
    • 2023-12
    • 2024-01
    • 2024-04
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客