对oracle不太了解,只会简单的startup、shutdown、lsnrctl start等命令,不喜欢oracle,不好用
请按实际情况修改username、password、ORACLE_SID、/PATH/TO/DIR等
expdp全备
注意expdp只能全备,不能进行增量备份
创建备份文件存放目录-- 创建备份存放目录(目录要已存在)
create or replace directory DMP_DIR as '/PATH/TO/DIR';
-- 对用户授权,给读写权限
grant read,write on directory DMP_DIR to USERNAME;
-- 查看目录
select * from all_directories;
-- 或
select * dba_directories;
-- 查看ORACLE_SID
select instance from v$thread;
expdp全备数据库脚本#!/bin/bash
source /home/oracle/.bash_profile
export ORACLE_BASE=/PATH/TO/ORACLE/BASE
export ORACLE_HOME=$ORACLE_BASE/product/12.1.0/dbhome_1
export ORACLE_SID=${ORACLE_SID}
export PATH=$PATH:$HOME/bin:$ORACLE_HOME/bin
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/usr/lib
DATE=`date +%Y%m%d`;
expdp UESRNAME/PASSWORD@ORACLE_SID directory=DUMP_DIR dumpfile=FILENAME_$DATE.dmp logfile=expdp_FILENAME_$DATE.log
参考文档:expdp介绍 非常详细
impdp数据恢复-- 创建表空间,并设置自增长(注意修改TABLESPACENAME和路径)(因为一个表空间文件的大小是有限制的,100多G的样子,所以添加多个表空间文件)
create tablespace TABLESPACENAME datafile '/PATH/TO/TABLESPACE/TABLESPACENAME.dbf' size 2g autoextend on;
alter tablespace TABLESPACENAME add datafile '/PATH/TO/TABLESPACE/TABLESPACENAME01.dbf' size 2g autoextend on;
-- 创建用户(环境和备份时要一样)
create user USERNAME identified by PASSWORD default tablespace TABLESPACENAME;
-- 授权用户
grant dba to USERNAME;
--如果认为dba权限太大,就只赋予需要的权限
grant execute on SYS.DBMS_LOCK to USERNAME;
grant execute on SYS.DBMS_PIPE to USERNAME;
grant execute on SYS.DBMS_CRYPTO to USERNAME;
-- 创建备份文件存放目录(目录要已存在)
create directory DUMP_DIR as '/PATH/TO/DIR';
-- 目录读写授权
Grant read,write on directory DUMP_DIR to USERNAME;
-- 恢复数据,如果没有修改tablespace的名字, remap_tablespace就不需要
impdp USERNAME/PASSWORD@${ORACLE_SID} directory=DUMP_DIR dumpfile=FILENAME_$DATE.dmp logfile=imp_FILENAME_$DATE.log remap_tablespace=OLD_TABLESPACENAME:TABLESPACENAME
transform=segment_attributes:n 如果不知道原来的表空间名可以用该选项去掉表空间和存储子句,将数据导入当前默认表空间
remap_tablespace 原来的表空间:新的表空间
remap_schema 原来的schema :新的schema(字符集)
RMAN增量备份
理解差距太大,就不献丑了
以下为RMAN增量备份脚本(需要改ORACLE_SID和DUMP_DIR)
增量备份等级
0:相当于full backup,备份整个datafile blocks,可以作为增量备份的起点;
1-4:该等级有2个备份模式
差异性增量备份:备份包括从level n or lower的改变块,n可指定;lower为大数字
累积性增量备份:备份包括从level n-1 or lower的改变块,n可指定
例如差异性增量备份是备份每天改变的块,而累积性增量备份从起点开始多天改变的块。
备份效果为:
以周为一个周期,星期天为起始时间,星期天就行一次全备,星期一、二、三就行累积性增量备份,星期四进行差异性增量备份,星期五、六进行累积性增量备份,每个月的1号会进行一次清除
#!/bin/bash
source /home/oracle/.bash_profile
#ORACLE_SID=ORACLE_SID #改ORACLE_SID
#ORACLE_HOME= #ORACLE_HOME
echo "ORACLE_SID = ${ORACLE_SID},ORACLE_HOME = $ORACLE_HOME"
LOGFILE=`dirname $0`/orabkup.log
echo "see logfile in "$LOGFILE
date=`date +%Y-%m-%d`
echo "----------------------------start backup ----------------------------------" >> $LOGFILE
date >>$LOGFILE
#every 1th day of month truncate the logfile
dy=`date +%d`
if [ $dy -eq 1 ]; then
cat /dev/null > $LOGFILE
fi
unset dy
WEEK_DAILY=`date +%a`
case "$WEEK_DAILY" in
"Mon")
BAK_LEVEL=2;;
"Tue")
BAK_LEVEL=2;;
"Wed")
BAK_LEVEL=2;;
"Thu")
BAK_LEVEL=1;;
"Fri")
BAK_LEVEL=2;;
"Sat")
BAK_LEVEL=2;;
"Sun")
BAK_LEVEL=0;;
"*")
BAK_LEVEL=error
esac
echo "Today is $WEEK_DAILY ,backup level is $BAK_LEVEL" >> $LOGFILE
$ORACLE_HOME/bin/rman target / 1>>$LOGFILE <
run{ allocate channel c1 device type disk;
allocate channel c2 device type disk;
allocate channel c3 device type disk;
allocate channel c4 device type disk;
crosscheck backup;
backup as compressed backupset
incremental level $BAK_LEVEL tag '${ORACLE_SID}_lev_$BAK_LEVEL'
database include current controlfile format 'DUMP_DIR/${ORACLE_SID}_%U_%T.bak';
release channel c1;
release channel c2;
release channel c3;
release channel c4;
sql 'alter system archive log current';
delete noprompt expired backup;
delete noprompt obsolete device type disk;
crosscheck archivelog all;
delete noprompt expired archivelog all;
delete noprompt archivelog all completed before 'sysdate-7';
#exit
}
EOF
date >>$LOGFILE
echo "---------------------------end backup ----------------------------------" >> $LOGFILE