主頁(yè) > 知識(shí)庫(kù) > Oracle自動(dòng)備份腳本

Oracle自動(dòng)備份腳本

熱門(mén)標(biāo)簽:圖吧網(wǎng)站地圖標(biāo)注 哪個(gè)400外呼系統(tǒng)好 地圖標(biāo)注養(yǎng)老院 開(kāi)發(fā)外呼系統(tǒng) 百度地圖標(biāo)注途經(jīng)點(diǎn) 愛(ài)客外呼系統(tǒng)怎么樣 山東crm外呼系統(tǒng)軟件 慧營(yíng)銷(xiāo)crm外呼系統(tǒng)丹丹 哈爾濱電話機(jī)器人銷(xiāo)售招聘

廢話不多說(shuō)了,直接給大家貼代碼了,具體代碼如下所示:

#!/bin/sh
#******************************************************************
# File: oraclebak.sh
# Creation Date: 2014/1/22 17:57:32
# Last Modified: 2014/1/22 17:57:34
# 腳本功能:oracle備份腳本
# 執(zhí)行方法:1、第一次執(zhí)行需要root用戶執(zhí)行,腳本會(huì)以詢問(wèn)的方式創(chuàng)建備份目錄和相關(guān)參數(shù)
# 2、腳本會(huì)自動(dòng)寫(xiě)入crontab調(diào)度里面定時(shí)執(zhí)行,crontab設(shè)置是在第一次執(zhí)行的時(shí)候自動(dòng)添加的
#******************************************************************
echo $USER
if [ $USER != root ]
then
echo "檢測(cè)到安裝用戶不是root用戶,請(qǐng)用root用戶登錄再執(zhí)行安裝文件"
exit 1
fi
echo "開(kāi)始安裝oracle數(shù)據(jù)庫(kù)備份工具.........................."
echo "請(qǐng)輸入備份程序的安裝目錄:"
read installdir
mkdir -p $installdir
result=$?
while [ $result -ne 0 ]
do
echo "無(wú)法創(chuàng)建目錄,請(qǐng)重新輸入或退出安裝。輸入y重新輸入,輸入n退出安裝。"
read redo
if [ $redo != y ]
then
echo "用戶退出安裝。"
exit 0
fi
echo "請(qǐng)輸入備份程序的安裝目錄:"
read installdir
mkdir $installdir
result=$?
done
echo "安裝目錄創(chuàng)建完成。"
echo "請(qǐng)輸入執(zhí)行備份用戶(一般為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 "請(qǐng)輸入執(zhí)行備份的用戶(一般為tnmsdb2用戶)"
read execuser
id $execuser
result=$?
done
echo "你希望建立備份的數(shù)據(jù)庫(kù)數(shù)量(一般為1)"
read backnum
if [ $backnum -eq 0 ]
then
echo "無(wú)備份數(shù)據(jù)庫(kù),退出安裝"
exit 0
fi
scount=0
while [ $scount -lt $backnum ]
do
echo "請(qǐng)輸入第"$(($scount+1))"個(gè)備份登錄登錄oracle數(shù)據(jù)庫(kù)的用戶名和密碼,以及本地服務(wù)名"
echo "用戶名"
read username[$scount]
echo "密碼"
read password[$scount]
echo "服務(wù)名"
read sname[$scount]
su -l -c"sqlplus /nolog" $execuserinsert
whenever sqlerror exit sql.sqlcode;
connect ${username[$scount]}/${password[$scount]}@${sname[$scount]}
exit
insert
result=$?
if [ $result -ne 0 ]
then
echo "輸入數(shù)據(jù)庫(kù)信息無(wú)法被連接,是否重新輸入?輸入y重新輸入,輸入n退出安裝。"
read redo
if [ $redo != y ]
then
echo "用戶退出安裝"
exit 0
fi
else
scount=$(($scount+1))
fi
done
echo "輸入值守時(shí)間,值守時(shí)間的格式為 mm hh dd MM E 。"
echo "m代表分鐘,h代表小時(shí),d代表日期,M代表月份,E代表星期"
echo "不輸入則為任意時(shí)間"
scount=0
iscontinue=y
while [ $iscontinue == y ]
do
echo "輸入第$(($scount+1))個(gè)值守時(shí)間"
echo "分鐘"
read min
echo "小時(shí)"
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 "是否輸入更多值守時(shí)間?輸入y繼續(xù),輸入n進(jìn)入下一步。"
read iscontinue
scount=$(($scount+1))
done
echo "是否要進(jìn)行遠(yuǎn)程備份?輸入y為進(jìn)行遠(yuǎn)程備份,輸入n進(jìn)入下一步"
scount=0
read iscontinue
while [ $iscontinue == y ]
do
echo "請(qǐng)選擇遠(yuǎn)程備份類(lèi)型,輸入s為sftp備份類(lèi)型,否則為ftp備份類(lèi)型"
read iss
if [ $iss == s ]
then
rtype[$scount]="sftp"
else
rtype[$scount]="ftp"
fi
echo "請(qǐng)輸入遠(yuǎn)程備份地址,可以為域名,計(jì)算機(jī)名或者ip地址"
read rurl[$scount]
echo "請(qǐng)輸入遠(yuǎn)程備份登錄名"
read rname[$scount]
echo "請(qǐng)輸入遠(yuǎn)程備份登錄密碼"
read rpass[$scount]
echo "是否輸入更多遠(yuǎn)程備份地址?輸入y重新輸入,n進(jì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 "執(zhí)行備份系統(tǒng)用戶為$execuser"
echo "系統(tǒng)備份數(shù)據(jù)庫(kù):"
allcount=${#username[@]}
scount=0
while [ $scount -lt $allcount ]
do
echo "${username[$scount]}/${password[$scount]}@${sname[$scount]}"
scount=$(($scount+1))
done
echo "執(zhí)行備份的值守時(shí)間表,*代表任意"
scount=0
backnum=${#backtime[@]}
while [ $scount -lt $backnum ]
do
echo "${backtime[$scount]}"
scount=$(($scount+1))
done
rnum=${#rtype[@]}
if [ $rnum -ne 0 ]
then
echo "遠(yuǎn)程備份的服務(wù)地址為:"
scount=0
while [ $scount -lt $rnum ]
do
echo "連接方式:${rtype[$scount]},連接地址:${rurl[$scount]},用戶名:${rname[$scount]},密碼:${rpass[$scount]}"
scount=$(($scount+1))
done
fi
echo "是否確認(rèn)安裝?輸入y繼續(xù),輸入n退出安裝"
read isgoon
if [ $isgoon != y ]
then
exit 0;
fi
echo "正在生產(chǎn)目錄結(jié)構(gòu)................................";
mkdir -p $installdir/logs
mkdir -p $installdir/temp
mkdir -p $installdir/zip
chown -Rvf $execuser $installdir
echo "目錄結(jié)構(gòu)生成完成................................"
echo "生成備份執(zhí)行文件................................"
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開(kāi)始備份.......................................\">>$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)開(kāi)始打包.......................................\">>$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)進(jìn)行遠(yuǎn)程備份.......................................\">>$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)完成遠(yuǎn)程備份.......................................\">>$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 "生成備份執(zhí)行文件完成............................"
echo "開(kāi)始定制值守時(shí)間................................"
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 "完成值守時(shí)間定制................................"
exit 0

接下來(lái)給大家介紹linux oracle自動(dòng)備份腳本

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、添加到任務(wù)調(diào)度

crontab -u oracle -e
* 3 * * * /home/bakup/bakup.sh
即每天凌晨3點(diǎn)進(jìn)行備份
如需每天備份多次,可設(shè)置不同時(shí)間段備份:
例如:* 3,13,18 * * * /home/bakup/bakup.sh,即每天3點(diǎn)、13點(diǎn)、18點(diǎn)進(jìn)行備份。
說(shuō)明:文件備份目錄,用戶oracle必須有更改權(quán)限,否則無(wú)法備份。

您可能感興趣的文章:
  • Oracle數(shù)據(jù)庫(kù)自動(dòng)備份腳本分享(超實(shí)用)
  • Linux中Oracle服務(wù)啟動(dòng)和停止腳本與開(kāi)機(jī)自啟動(dòng)
  • Oracle數(shù)據(jù)庫(kù)執(zhí)行腳本常用命令小結(jié)
  • Linux下通過(guò)腳本自動(dòng)備份Oracle數(shù)據(jù)庫(kù)并刪除指定天數(shù)前的備份
  • oracle自動(dòng)巡檢腳本生成html報(bào)告的方法

標(biāo)簽:和田 開(kāi)封 周口 甘肅 固原 武漢 承德 青島

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Oracle自動(dòng)備份腳本》,本文關(guān)鍵詞  Oracle,自動(dòng),備份,腳本,Oracle,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Oracle自動(dòng)備份腳本》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于Oracle自動(dòng)備份腳本的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章