上周在客户现场做了oracle数据备份保留7天的脚本,先记录起来,方便日后查阅!!!!
create or replace directory DIR_DP as 'G:\zsoms\datafile'; --DIR_DP路径名
#DIR_DP路径名
create or replace directory DIR_DP as 'G:\zsoms\datafile';
#查看是否创建成功
SELECT privilege, directory_name, DIRECTORY_PATH FROM user_tab_privs t, all_directories d WHERE t.table_name(+) = d.directory_name ORDER BY 2, 1;
#赋予导出数据的用户权限
grant read,write on directory DIR_DP to system;
-------------------------------华丽的分割线---------------------------------------------
#!/bin/bash
#载入环境变量
ORACLE_BASE=/u03/oracle;
export ORACLE_BASE
ORACLE_HOME=/u03/oracle/product/11.2.0;
export ORACLE_HOME
ORACLE_TERM=xterm;
export ORACLE_TERM
PATH=/u03/oracle/product/11.2.0/bin:/usr/lib64/qt-3.3/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/oracle/bin:/u03/oracle/product/11.2.0/bin:/u03/oracle/product/11.2.0/bin;
export PATH
#设置编码
export NLS_LANG=american_america.ZHS16GBK;
#设置实例
export ORACLE_SID=orcl
echo "Environment variables loaded successfully!"
#设置文件命名保证文件名不重复
today=$(date +%Y%m%d)
echo ${today}
#使用数据泵4导出全库
/u03/oracle/product/11.2.0/bin/expdp system/sys directory=DIR_DP dumpfile=${today}.dmp logfile=${today}.log FULL=Y
echo "Logical backup success"
#把dmp数据文件打包起来
cd /back_db
/usr/bin/zip -r ${today}.zip ${today}.dmp
/usr/bin/zip -r ${today}.zip ${today}.log
echo "Compression success!"
#干掉原来的数据文件
rm -f /back_db/${today}.dmp
rm -f /back_db/${today}.log
echo "Logical backups and logs have been deleted!"
#把创建时间超过7天的删掉
find /back_db/ *.zip -type f -mtime +7 -exec rm {} \;
echo "Reserver 7 days of backup success!"
#顺便把数据的压缩包复制到另一台服务器保存起来
scp -p /back_db/${today}.zip oracle@IP地址:/OracleBack/data
echo "Remote backup success!"
echo "(o_o) The daily tasks have been completed!"
-------------------------------华丽的分割线---------------------------------------------
注
1、数据库的载入环境要根据自己的服务器实际情况修改!!!
2、DIR_DP这个是数据库的备份路径,要先用sqlplus创建,记注要把路径的读写权限给system用户
3、载入环境变量其实可以偷懒直接使用下面
source /home/oracle/.bash_profile
不过我的服务器不生效,不知道为什么,没去具体查原因!!!
4、数据导入全部导入
impdp system/sys dumpfile =text.dmp logfile=text.log directory=DIR_DP full=y