备份数据库,并删除七天前的备份文件
export ORACLE_SID=bdyz
export ORACLE_BASE=/u01/oracle11g_R2
export ORACLE_HOME=$ORACLE_BASE/11g
export LD_LIBRARY_PATH=$ORACLE_HOME/lib
export NLS_LANG="simplified chinese"_china.zhs16gbk;
export PATH=$PATH:$ORACLE_HOME/bin
rq=$(date +%Y%m%d)
user_name='lltf'
user_passwd='lltf_2017'
bak_dir=/u01/oradata/${user_name}/$rq
if [ ! -s $bak_dir ];then
mkdir -p $bak_dir
fi
sqlplus / as sysdba <<EOF
drop directory dump_dir;
create directory dump_dir as '${bak_dir}';
grant read,write on directory dump_dir to ${user_name};
quit
EOF
expdp ${user_name}/${user_passwd} directory=dump_dir dumpfile=${user_name}_%U.dmp schemas=${user_name} logfile=${user_name}_${rq}_exp.log compression=ALL filesize=1g parallel=8
cd $bak_dir
tar -zcf lltf_dmp.tar.gz *.dmp ${user_name}_${rq}_exp.log && rm *.log *.dmp
cd ..
dir_name=$(date -d '-7 days' +%Y%m%d)
find . -type d -name $dir_name | xargs rm -rf
如果要定时备份,只需将该脚本放在cronjob中
假设每天凌晨3:00定时备份
0 3 * * * oracle sh <该脚本绝对路径>