您好,登錄后才能下訂單哦!
需求:備份oracle數(shù)據(jù)庫(kù)并將數(shù)據(jù)備份的文件上傳到ftp所在服務(wù)器
ftp服務(wù)器IP地址:192.168.6.9
ftp服務(wù)器賬戶/密碼:test/test2019
1、oracle用戶創(chuàng)建備份腳本路徑:
#su - oracle
$ mkdir /u0/app/backup/bin
$ cd /u01/app/backup/bin
2、創(chuàng)建備份腳本:
$ vi /u01/app/backup/bin/bak.sh
#!/bin/sh
#自己指定環(huán)境變量(適合多實(shí)例的數(shù)據(jù)庫(kù))
export ORACLE_SID=orcl
export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=$ORACLE_BASE/product/11.2.0/dbhome_1
export PATH=$ORACLE_HOME/bin:$PATH:/sbin/
expot.UTF-8
export NLS_LANG='AMERICAN_AMERICA.AL32UTF8'
#或使用oracle原先.bash_profile文件里環(huán)境變量
source /home/oracle/.bash_profile
#上面環(huán)境變量二選一即可
timedate=$(date +%Y%m%d%H%M%S)
bakdbname=hb
bakdbpasswd=h2234
dumpbame=hd
bakdbhome=/u01/app/backup
expdp $bakdbname/$bakdbpasswd directory=backDir dumpfile=$dumpbame-$timedate.dmp logfile=$bakdbname-$timedate.log
#方法一、壓縮dmp文件,會(huì)連接bakdbhome變量的目錄及文件一并壓縮
zip -r $bakdbhome/$dumpbame-$timedate.zip $bakdbhome/$dumpbame-$timedate.dmp $bakdbhome/$bakdbname-$timedate.log????? #壓縮dmp文件及日志文件
#或用方法二
#若是不加項(xiàng)目路徑目錄名就切換到存放dmp文件的目錄進(jìn)行壓縮即可
cd $bakdbhome
zip -r $dumpbame-$timedate.zip $dumpbame-$timedate.dmp $bakdbname-$timedate.log
find $bakdbhome/*.log -mtime +10 -exec rm -rf {} \;
find $bakdbhome/*.zip -mtime +10 -exec rm -rf {} \;
find $bakdbhome/*.dmp -exec rm -rf {} \;? #前面壓縮后,刪除原dmp文件
cd $bakdbhome?? #切換到備份文件目錄下(這里如果用的方法二的就不在需要在切換目錄的了)
ftp -v -n 192.168.6.9? << EOF?????? #執(zhí)行ftp命令
或
ftp -v -n 192.168.6.9 >>${bakdbname}_${timedate}.log << EOF
將ftp上傳日志信息一起追加到備份日志里面
user test test2018?????????? #用戶名和密碼
binary???????????????????? #切換傳輸模式為二進(jìn)制模式,以字節(jié)傳輸(除文字文件外皆用此模式)
hash?????????????????????? #切換#字號(hào)的出現(xiàn),每一個(gè)#字號(hào)表示傳送了1024/8192BYTES
put $dumpbame-$timedate.zip $dumpbame-$timedate.zip
bye
EOF
#將日志文件的GB2312轉(zhuǎn)為UTF-8編碼,這樣腳本執(zhí)行的log日志文件中文不會(huì)亂碼
iconv -f gb2312? -t utf8 $bakdbname-$timedate.log? -o $bakdbname-$timedate.log
3、實(shí)例腳本,備份好庫(kù)并異地上傳
#!/bin/sh
source /home/oracle/.bash_profile
timedate=$(date +%Y%m%d%H%M%S)
bakdbname=re
bakdbpasswd=123
dumpdbname=re
bakdbhome=/u01/databack
expdp $bakdbname/$bakdbpasswd directory=backDir dumpfile=$dumpdbname'_'$timedate.dmp logfile=$dumpdbname'_'$timedate.log
cd /u01
zip -r databack/$dumpdbname'_'$timedate.zip databack/$dumpdbname'_'$timedate.dmp databack/$dumpdbname'_'$timedate.log
find $bakdbhome/*.log -mtime +10 -exec rm -rf {} \;
find $bakdbhome/*.zip -mtime +10 -exec rm -rf {} \;
find $bakdbhome/*.dmp -exec rm -rf {} \;
cd $bakdbhome
ftp -v -n 192.168.6.9 >>${dumpdbname}_${timedate}.log << EOF
user test test2018
binary
hash
put ${dumpdbname}_${timedate}.zip ${dumpdbname}_${timedate}.zip
bye
EOF
#iconv -f gb2312? -t utf8 ${bakdbname}_${timedate}.log? -o ${bakdbname}_${timedate}.log
FTP的命令行格式為:
ftp -v -d -i -n -g [主機(jī)名] ,其中
-v 顯示遠(yuǎn)程服務(wù)器的所有響應(yīng)信息;
-n 限制ftp的自動(dòng)登錄,即不使用;
hash:每傳輸1024字節(jié),顯示一個(gè)hash符號(hào)(#);
.n etrc文件;
-d 使用調(diào)試方式;
-g 取消全局文件名;
bye:退出ftp會(huì)話過(guò)程;
get下傳文件;
mget批量下傳文件,需配合萬(wàn)用字元,例如:MGET*.GZ;
put上傳文件;
mput批量上傳文件,需配合萬(wàn)用字元;
recv相當(dāng)于GET(RECV為RECEIVE的簡(jiǎn)寫(xiě)):
send相當(dāng)于PUT。
給bak.sh腳本執(zhí)行權(quán)限,做計(jì)劃任務(wù)即可
$ crontab -l
10 0 * * * /bin/bash /u01/app/backup/bin/bak.sh
示例:
#/bin/bash
echo "=======expdp-oracle-backup========"
source /home/oracle/.bash_profile
timedate=$(date +%Y%m%d%H%M%S)
bakdbname=system
bakdbpasswd=oracle
dumpbame=oracle_bak
bakdbhome=/opt/backup
expdp $bakdbname/$bakdbpasswd@10.32.10.30/orcl directory=ORACLE_BACKUP dumpfile=$dumpbame-$timedate.dmp logfile=$bakdbname-$timedate.log full=y
#壓縮dmp文件,會(huì)連接bakdbhome變量的目錄及文件一并壓縮;
zip -r $bakdbhome/$dumpbame-$timedate.zip $bakdbhome/$dumpbame-$timedate.dmp $bakdbhome/$bakdbname-$timedate.log????? #壓縮dmp文件及日志文件;
cd $bakdbhome
zip -r $dumpbame-$timedate.zip $dumpbame-$timedate.dmp $bakdbname-$timedate.log
find $bakdbhome/*.log -mtime +10 -exec rm -rf {} \;
find $bakdbhome/*.zip -mtime +10 -exec rm -rf {} \;
find $bakdbhome/*.dmp -exec rm -rf {} \;????????????? #前面壓縮后,刪除原dmp文件
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。