廢話不多說了,直接給大家貼代碼了,具體代碼如下所示:
#!/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 $USER if [ $USER != root ] then echo "檢測到安裝用戶不是root用戶,請用root用戶登錄再執行安裝文件" exit 1 fi echo "開始安裝oracle數據庫備份工具.........................." echo "請輸入備份程序的安裝目錄:" read installdir mkdir -p $installdir result=$? while [ $result -ne 0 ] do echo "無法創建目錄,請重新輸入或退出安裝。輸入y重新輸入,輸入n退出安裝。" read redo if [ $redo != y ] then echo "用戶退出安裝。" exit 0 fi echo "請輸入備份程序的安裝目錄:" read installdir mkdir $installdir result=$? done echo "安裝目錄創建完成。" echo "請輸入執行備份用戶(一般為oracle用戶):" read execuser id $execuser result=$? while [ $result -ne 0 ] do echo "不存在該用戶,是否更換其它用戶或者退出安裝新建用戶,輸入y更換用戶,輸入n退出安裝" read redo if [ $redo != y ] then echo "用戶退出安裝" exit 0 fi echo "請輸入執行備份的用戶(一般為tnmsdb2用戶)" read execuser id $execuser result=$? done echo "你希望建立備份的數據庫數量(一般為1)" read backnum if [ $backnum -eq 0 ] then echo "無備份數據庫,退出安裝" exit 0 fi scount=0 while [ $scount -lt $backnum ] do echo "請輸入第"$(($scount+1))"個備份登錄登錄oracle數據庫的用戶名和密碼,以及本地服務名" echo "用戶名" read username[$scount] echo "密碼" read password[$scount] echo "服務名" read sname[$scount] su -l -c"sqlplus /nolog" $execuser<<insert whenever sqlerror exit sql.sqlcode; connect ${username[$scount]}/${password[$scount]}@${sname[$scount]} exit insert result=$? if [ $result -ne 0 ] then echo "輸入數據庫信息無法被連接,是否重新輸入?輸入y重新輸入,輸入n退出安裝。" read redo if [ $redo != y ] then echo "用戶退出安裝" exit 0 fi else scount=$(($scount+1)) fi done echo "輸入值守時間,值守時間的格式為 mm hh dd MM E 。" echo "m代表分鐘,h代表小時,d代表日期,M代表月份,E代表星期" echo "不輸入則為任意時間" scount=0 iscontinue=y while [ $iscontinue == y ] do echo "輸入第$(($scount+1))個值守時間" echo "分鐘" read min echo "小時" read hour echo "日期" read day if [ ! $day ] then day="*" fi echo "月份" read mouth if [ ! $mouth ] then mouth="*" fi week="*" backtime[$scount]="$min $hour $day $mouth $week" echo -e "${backtime[$scount]}" echo "是否輸入更多值守時間?輸入y繼續,輸入n進入下一步。" read iscontinue scount=$(($scount+1)) done echo "是否要進行遠程備份?輸入y為進行遠程備份,輸入n進入下一步" scount=0 read iscontinue while [ $iscontinue == y ] do echo "請選擇遠程備份類型,輸入s為sftp備份類型,否則為ftp備份類型" read iss if [ $iss == s ] then rtype[$scount]="sftp" else rtype[$scount]="ftp" fi echo "請輸入遠程備份地址,可以為域名,計算機名或者ip地址" read rurl[$scount] echo "請輸入遠程備份登錄名" read rname[$scount] echo "請輸入遠程備份登錄密碼" read rpass[$scount] echo "是否輸入更多遠程備份地址?輸入y重新輸入,n進入下一步" read iscontinue scount=$(($scount+1)) done # installdir # execuser # username password sname # backtime # rtype rurl rname rpass echo "您所作的備份配置如下:" sresult=$(echo -n "$installdir" | grep '^/') if [ ! $sresult ] then installdir="$PWD/$installdir" else installdir=$installdir fi echo "本地備份目錄為$installdir" echo "執行備份系統用戶為$execuser" echo "系統備份數據庫:" allcount=${#username[@]} scount=0 while [ $scount -lt $allcount ] do echo "${username[$scount]}/${password[$scount]}@${sname[$scount]}" scount=$(($scount+1)) done echo "執行備份的值守時間表,*代表任意" scount=0 backnum=${#backtime[@]} while [ $scount -lt $backnum ] do echo "${backtime[$scount]}" scount=$(($scount+1)) done rnum=${#rtype[@]} if [ $rnum -ne 0 ] then echo "遠程備份的服務地址為:" scount=0 while [ $scount -lt $rnum ] do echo "連接方式:${rtype[$scount]},連接地址:${rurl[$scount]},用戶名:${rname[$scount]},密碼:${rpass[$scount]}" scount=$(($scount+1)) done fi echo "是否確認安裝?輸入y繼續,輸入n退出安裝" read isgoon if [ $isgoon != y ] then exit 0; fi echo "正在生產目錄結構................................"; mkdir -p $installdir/logs mkdir -p $installdir/temp mkdir -p $installdir/zip chown -Rvf $execuser $installdir echo "目錄結構生成完成................................" echo "生成備份執行文件................................" echo "#!/bin/bash">$installdir/runback.sh echo "source ~/.bash_profile">>$installdir/runback.sh echo "nowdate=\$(date +%Y-%m-%d_%k.%M.%S)">>$installdir/runback.sh echo "nowmouth=\$(date +%Y-%m)">>$installdir/runback.sh echo "echo \"\$nowdate開始備份.......................................\">>$installdir/logs/\$nowmouth.log">>$installdir/runback.sh scount=0 snum=${#username[@]} while [ $scount -lt $snum ] do echo "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.sh scount=$(($scount+1)) done echo "echo \"\$(date +%Y-%m-%d_%k.%M.%S)完成備份.......................................\">>$installdir/logs/\$nowmouth.log">>$installdir/runback.sh echo "echo \"\$(date +%Y-%m-%d_%k.%M.%S)開始打包.......................................\">>$installdir/logs/\$nowmouth.log">>$installdir/runback.sh echo "gzip $installdir/zip/\$nowdate.zip $installdir/temp/>>$installdir/logs/\$nowmouth.log 2>&1">>$installdir/runback.sh echo "rm -Rvf $installdir/temp/*>>$installdir/logs/\$nowmouth.log 2>&1">>$installdir/runback.sh echo "echo \"\$(date +%Y-%m-%d_%k.%M.%S)打包完成.......................................\">>$installdir/logs/\$nowmouth.log">>$installdir/runback.sh scount=0 if [ ${#rtype[@]} -ne 0 ] then echo "echo \"\$(date +%Y-%m-%d_%k.%M.%S)進行遠程備份.......................................\">>$installdir/logs/\$nowmouth.log">>$installdir/runback.sh snum=${#rtype[@]} while [ $scount -lt $snum ] do echo "lftp -u ${rname[$scount]},${rpass[$scount]} ${rtype[$scount]}://${rurl[$scount]}<<remote">>$installdir/runback.sh echo "mkdir remotedbback">>$installdir/runback.sh echo "cd remotedbback">>$installdir/runback.sh echo "mput $installdir/zip/\$nowdate.zip">>$installdir/runback.sh echo "exit">>$installdir/runback.sh echo "remote">>$installdir/runback.sh scount=$(($scount+1)) done echo "echo \"\$(date +%Y-%m-%d_%k.%M.%S)完成遠程備份.......................................\">>$installdir/logs/\$nowmouth.log">>$installdir/runback.sh fi echo "exit 0">>$installdir/runback.sh chmod 775 $installdir/runback.sh chown $execuser $installdir/runback.sh echo "生成備份執行文件完成............................" echo "開始定制值守時間................................" scount=0 snum=${#backtime[@]} while [ $scount -lt $snum ] do echo "${backtime[$scount]} $installdir/runback.sh">>$installdir/planlist scount=$(($scount+1)) done su -l -c"crontab $installdir/planlist" oracle echo "完成值守時間定制................................" 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必須有更改權限,否則無法備份。