Shell 腳本封裝 ftp -i -n 實現無人值守文件傳輸,結合 Oracle 管理流程,覆蓋**異地備份推送、跨節點同步導入、報表自動郵件**三大高頻場景。
auto_ftp.sh#!/usr/bin/sh
# 參數化:目標IP、用戶、密碼、源文件、目標路徑
TARGET_IP=192.168.0.253
FTP_USER=username
FTP_PASS=password
ftp -i -n $TARGET_IP << FTPIT
user $FTP_USER $FTP_PASS
bin
put /oracle_backup/exp/user1.dmp.Z /bk/oracle_bak/dmp/user1.dmp.Z
put /oracle_backup/exp/user2.dmp.Z /bk/oracle_bak/dmp/user2.dmp.Z
quit
FTPIT
關鍵選項: -i 關閉交互提示,-n 禁用自動登入(由 here-document 提供認證)。
auto_ftp.shauto_import.sh 定時拉取並 impcrontab 錯峰編排,避免網絡衝突# 源端 crontab
0 1 * * * /scripts/exp_tables.sh # 導出核心表
30 1 * * * /scripts/auto_ftp_tables.sh # 推送至目標
# 目標端 crontab
0 3 * * * /scripts/drop_tables.sh # 清理舊數據
30 3 * * * /scripts/imp_tables.sh # 導入新數據
# auto_mail.sh
(cat mail.txt; uuencode report1.txt.Z report1.txt; uuencode report2.txt.Z report2.txt) | mail -s "Daily Report" admin@corp.com
mail.txt:郵件正文uuencode:二進制附件編碼(需 sharutils 包)mutt -a attachment -s "Subject" -- to@domain < body.txtmd5sum / cksum 比對lftp -c "mirror -R -c ..." 或 rsync -avz --partialfor i in 1 2 3; do ftp ... && break || sleep 60; done>> /var/log/auto_ftp_$(date +%Y%m%d).log 2>&1700,密碼不硬編碼(讀取 $HOME/.ftp_credentials 600)