废话不多说了,直接给大家贴代码了,具体代码如下所示:
#!/bin/sh#******************************************************************# File: oraclebak.sh# Creation Date: 2014/1/22 17:57:32# Last Modified: 2014/1/22 17:57:34# 脚本功能:oracle备份脚本# 执行方法:1、第一次执行需要root用户执行,脚本会以询问的方式创建备份目录和相关参数# 2、脚本会自动写入crontab调度里面定时执行,crontab设置是在第一次执行的时候自动添加的#******************************************************************echo $USERif [ $USER != root ]thenecho "检测到安装用户不是root用户,请用root用户登录再执行安装文件"exit 1fiecho "开始安装oracle数据库备份工具.........................."echo "请输入备份程序的安装目录:"read installdirmkdir -p $installdirresult=$?while [ $result -ne 0 ]doecho "无法创建目录,请重新输入或退出安装。输入y重新输入,输入n退出安装。"read redoif [ $redo != y ]thenecho "用户退出安装。"exit 0fiecho "请输入备份程序的安装目录:"read installdirmkdir $installdirresult=$?doneecho "安装目录创建完成。"echo "请输入执行备份用户(一般为oracle用户):"read execuserid $execuserresult=$?while [ $result -ne 0 ]doecho "不存在该用户,是否更换其它用户或者退出安装新建用户,输入y更换用户,输入n退出安装"read redoif [ $redo != y ]thenecho "用户退出安装"exit 0fiecho "请输入执行备份的用户(一般为tnmsdb2用户)"read execuserid $execuserresult=$?doneecho "你希望建立备份的数据库数量(一般为1)"read backnumif [ $backnum -eq 0 ]thenecho "无备份数据库,退出安装"exit 0fiscount=0while [ $scount -lt $backnum ]doecho "请输入第"$(($scount+1))"个备份登录登录oracle数据库的用户名和密码,以及本地服务名"echo "用户名"read username[$scount]echo "密码"read password[$scount]echo "服务名"read sname[$scount]su -l -c"sqlplus /nolog" $execuser<<insertwhenever sqlerror exit sql.sqlcode;connect ${username[$scount]}/${password[$scount]}@${sname[$scount]}exitinsertresult=$?if [ $result -ne 0 ]thenecho "输入数据库信息无法被连接,是否重新输入?输入y重新输入,输入n退出安装。"read redoif [ $redo != y ]thenecho "用户退出安装"exit 0fielsescount=$(($scount+1))fidoneecho "输入值守时间,值守时间的格式为 mm hh dd MM E 。"echo "m代表分钟,h代表小时,d代表日期,M代表月份,E代表星期"echo "不输入则为任意时间"scount=0iscontinue=ywhile [ $iscontinue == y ]doecho "输入第$(($scount+1))个值守时间"echo "分钟"read minecho "小时"read hourecho "日期"read dayif [ ! $day ]thenday="*"fiecho "月份"read mouthif [ ! $mouth ]thenmouth="*"fiweek="*"backtime[$scount]="$min $hour $day $mouth $week"echo -e "${backtime[$scount]}"echo "是否输入更多值守时间?输入y继续,输入n进入下一步。"read iscontinuescount=$(($scount+1))doneecho "是否要进行远程备份?输入y为进行远程备份,输入n进入下一步"scount=0read iscontinuewhile [ $iscontinue == y ]doecho "请选择远程备份类型,输入s为sftp备份类型,否则为ftp备份类型"read issif [ $iss == s ]thenrtype[$scount]="sftp"elsertype[$scount]="ftp"fiecho "请输入远程备份地址,可以为域名,计算机名或者ip地址"read rurl[$scount]echo "请输入远程备份登录名"read rname[$scount]echo "请输入远程备份登录密码"read rpass[$scount]echo "是否输入更多远程备份地址?输入y重新输入,n进入下一步"read iscontinuescount=$(($scount+1))done# installdir# execuser# username password sname# backtime# rtype rurl rname rpassecho "您所作的备份配置如下:"sresult=$(echo -n "$installdir" | grep '^/')if [ ! $sresult ]theninstalldir="$PWD/$installdir"elseinstalldir=$installdirfiecho "本地备份目录为$installdir"echo "执行备份系统用户为$execuser"echo "系统备份数据库:"allcount=${#username[@]}scount=0while [ $scount -lt $allcount ]doecho "${username[$scount]}/${password[$scount]}@${sname[$scount]}"scount=$(($scount+1))doneecho "执行备份的值守时间表,*代表任意"scount=0backnum=${#backtime[@]}while [ $scount -lt $backnum ]doecho "${backtime[$scount]}"scount=$(($scount+1))donernum=${#rtype[@]}if [ $rnum -ne 0 ]thenecho "远程备份的服务地址为:"scount=0while [ $scount -lt $rnum ]doecho "连接方式:${rtype[$scount]},连接地址:${rurl[$scount]},用户名:${rname[$scount]},密码:${rpass[$scount]}"scount=$(($scount+1))donefiecho "是否确认安装?输入y继续,输入n退出安装"read isgoonif [ $isgoon != y ]thenexit 0;fiecho "正在生产目录结构................................";mkdir -p $installdir/logsmkdir -p $installdir/tempmkdir -p $installdir/zipchown -Rvf $execuser $installdirecho "目录结构生成完成................................"echo "生成备份执行文件................................"echo "#!/bin/bash">$installdir/runback.shecho "source ~/.bash_profile">>$installdir/runback.shecho "nowdate=/$(date +%Y-%m-%d_%k.%M.%S)">>$installdir/runback.shecho "nowmouth=/$(date +%Y-%m)">>$installdir/runback.shecho "echo /"/$nowdate开始备份......................................./">>$installdir/logs//$nowmouth.log">>$installdir/runback.shscount=0snum=${#username[@]}while [ $scount -lt $snum ]doecho "exp ${username[$scount]}/${password[$scount]}@${sname[$scount]} file=$installdir/temp/${username[$scount]}_${sname[$scount]}/$nowdate.dmp compress=N>>$installdir/logs//$nowmouth.log 2>&1">>$installdir/runback.shscount=$(($scount+1))doneecho "echo /"/$(date +%Y-%m-%d_%k.%M.%S)完成备份......................................./">>$installdir/logs//$nowmouth.log">>$installdir/runback.shecho "echo /"/$(date +%Y-%m-%d_%k.%M.%S)开始打包......................................./">>$installdir/logs//$nowmouth.log">>$installdir/runback.shecho "gzip $installdir/zip//$nowdate.zip $installdir/temp/>>$installdir/logs//$nowmouth.log 2>&1">>$installdir/runback.shecho "rm -Rvf $installdir/temp/*>>$installdir/logs//$nowmouth.log 2>&1">>$installdir/runback.shecho "echo /"/$(date +%Y-%m-%d_%k.%M.%S)打包完成......................................./">>$installdir/logs//$nowmouth.log">>$installdir/runback.shscount=0if [ ${#rtype[@]} -ne 0 ]thenecho "echo /"/$(date +%Y-%m-%d_%k.%M.%S)进行远程备份......................................./">>$installdir/logs//$nowmouth.log">>$installdir/runback.shsnum=${#rtype[@]}while [ $scount -lt $snum ]doecho "lftp -u ${rname[$scount]},${rpass[$scount]} ${rtype[$scount]}://${rurl[$scount]}<<remote">>$installdir/runback.shecho "mkdir remotedbback">>$installdir/runback.shecho "cd remotedbback">>$installdir/runback.shecho "mput $installdir/zip//$nowdate.zip">>$installdir/runback.shecho "exit">>$installdir/runback.shecho "remote">>$installdir/runback.shscount=$(($scount+1))doneecho "echo /"/$(date +%Y-%m-%d_%k.%M.%S)完成远程备份......................................./">>$installdir/logs//$nowmouth.log">>$installdir/runback.shfiecho "exit 0">>$installdir/runback.shchmod 775 $installdir/runback.shchown $execuser $installdir/runback.shecho "生成备份执行文件完成............................"echo "开始定制值守时间................................"scount=0snum=${#backtime[@]}while [ $scount -lt $snum ]doecho "${backtime[$scount]} $installdir/runback.sh">>$installdir/planlistscount=$(($scount+1))donesu -l -c"crontab $installdir/planlist" oracleecho "完成值守时间定制................................"exit 0
接下来给大家介绍linux oracle自动备份脚本
1、备份脚本:
#!/bin/sh export ORACLE_BASE=/home/oracle export ORACLE_HOME=$ORACLE_BASE/product/10.2.0/db_1 export ORACLE_SID=orcl export PATH=$PATH:$HOME/bin:$ORACLE_HOME/bin export DATA_DIR=/home/bakup/data export LOGS_DIR=/home/bakup/logs export DELTIME=`date -d "7 days ago" +%Y%m%d` export BAKUPTIME=`date +%Y%m%d%H%M%S` mkdir -p $DATA_DIR mkdir -p $LOGS_DIR echo "Starting bakup..." echo "Bakup file path $DATA_DIR/$BAKUPTIME.dmp" exp shop/lyisABC0987@orcl file=$DATA_DIR/$BAKUPTIME.dmp log=$LOGS_DIR/$BAKUPTIME.log echo "Delete the file bakup before 7 days..." rm -rf $DATA_DIR/$DELTIME*.dmp rm -rf $LOGS_DIR/$DELTIME*.log echo "Delete the file bakup successfully. " echo "Bakup completed."
2、添加到任务调度
crontab -u oracle -e
* 3 * * * /home/bakup/bakup.sh
即每天凌晨3点进行备份
如需每天备份多次,可设置不同时间段备份:
例如:* 3,13,18 * * * /home/bakup/bakup.sh,即每天3点、13点、18点进行备份。
说明:文件备份目录,用户oracle必须有更改权限,否则无法备份。