Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> 關於 Linux

利用 iptables 配置檔與腳本實現主備防火牆規則自動同步備份

編輯:關於 Linux

生產環境常需將主控機的 iptables 規則自動備份至備份機,以便故障切換。本文提供基於 cron 與 expect 的自動化方案。

主控機操作

  1. 建立目錄:
    mkdir -p /bak/iptables/
    mkdir /root/script/
  2. 建立備份腳本 /root/script/backup.sh:
    \cp /etc/sysconfig/iptables /bak/iptables/iptables_$(date +%Y%m%d%H)
  3. 設定定時任務:
    crontab -e
    10 * * * * sh /root/script/backup.sh

備份機操作

  1. 建立目錄:
    mkdir -p /bak/iptables/{lt,yd}
    mkdir /root/script/
  2. 建立 SCP 取回腳本 /root/script/scp_lt.sh (使用 expect 自動輸入密碼):
    #!/usr/bin/expect -f
    set password gst
    spawn scp -P 22 root@202.105.135.52:/etc/sysconfig/iptables /bak/iptables/lt/iptables
    set timeout 300
    expect "root@202.105.135.52's password:"
    send "$password\r"
    expect eof
  3. 同樣加入 crontab 定時執行。

此方案實現異地防火牆規則自動同步,提升系統可用性。

Copyright © Windows教程網 All Rights Reserved