以前、同じ FreeBSD 系のFreeNAS(現TrueNAS)で、同じような 設定ファイル の バックアップ 自動化 を実現しましたが、 pfSense でもほぼ同じ要領で 設定ファイル バックアップ の 自動化 が可能です。もう何年も使って来たこの仕組みを社内における pfSense 退役を前に、まとめておきます。
設定ファイルへのパス
FreeNASでは /data に設定ファイルが有りましたが、pfSenseでは /conf にxml形式で収納されています。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[2.4.4-RELEASE]/root: cd /conf [2.4.4-RELEASE]/conf: ls -l drwxr-xr-x 2 root wheel 1536 May 24 16:06 backup -rw-r--r-- 1 root wheel 85414 May 24 16:06 config.xml -rw-r--r-- 1 root wheel 14 Sep 28 2018 copynotice_version -rw-r--r-- 1 root wheel 1897 May 24 11:09 copyright -rw-r--r-- 1 root wheel 0 Sep 3 2018 installer_copied_config -rw-r--r-- 1 root wheel 70271 Sep 3 2018 pkg_log_pfSense-pkg-net-snmp.json -rw-r--r-- 1 root wheel 2216 Sep 3 2018 pkg_log_pfSense-pkg-net-snmp.txt -rw-r--r-- 1 root wheel 264363 Sep 3 2018 pkg_log_pfSense-pkg-ntopng.json -rw-r--r-- 1 root wheel 8576 Sep 3 2018 pkg_log_pfSense-pkg-ntopng.txt -rw-r--r-- 1 root wheel 262278 Sep 28 2018 upgrade_log.latest.txt -rw-r--r-- 1 root wheel 800 May 27 19:28 upgrade_log.txt |
同じ場所にある backup と言うサブディレクトリが気になったので覗いてみると、過去30世代分の設定ファイルが保存されていました。設定を変更したりするとここへ変更前の設定がバックアップされるので、もしもの際の巻き戻しにも使えそうです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
[2.4.4-RELEASE]/conf/backup: ls -la -rw-r--r-- 1 root wheel 4998 May 24 16:06 backup.cache -rw-r--r-- 1 root wheel 83780 Apr 8 2019 config-1554712803.xml -rw-r--r-- 1 root wheel 83772 Apr 8 2019 config-1554712832.xml -rw-r--r-- 1 root wheel 83788 Apr 8 2019 config-1554717781.xml -rw-r--r-- 1 root wheel 83788 Apr 30 2019 config-1554717791.xml -rw-r--r-- 1 root wheel 83967 May 15 2019 config-1556605304.xml -rw-r--r-- 1 root wheel 84008 May 15 2019 config-1557890113.xml -rw-r--r-- 1 root wheel 83997 Sep 13 2019 config-1557890247.xml -rw-r--r-- 1 root wheel 84032 Dec 23 2019 config-1568352167.xml -rw-r--r-- 1 root wheel 84044 Dec 23 2019 config-1577077267.xml -rw-r--r-- 1 root wheel 84029 Aug 5 2020 config-1577077275.xml -rw-r--r-- 1 root wheel 84433 Aug 5 2020 config-1596611721.xml -rw-r--r-- 1 root wheel 84685 Aug 5 2020 config-1596611787.xml -rw-r--r-- 1 root wheel 84685 Aug 5 2020 config-1596611801.xml -rw-r--r-- 1 root wheel 85594 Aug 5 2020 config-1596611974.xml -rw-r--r-- 1 root wheel 85586 Sep 22 2020 config-1596616334.xml -rw-r--r-- 1 root wheel 85601 Dec 4 11:35 config-1600740507.xml -rw-r--r-- 1 root wheel 85620 Dec 16 14:31 config-1607052953.xml -rw-r--r-- 1 root wheel 85496 Dec 16 14:41 config-1608100305.xml -rw-r--r-- 1 root wheel 85628 Jan 8 17:30 config-1608100893.xml -rw-r--r-- 1 root wheel 85658 Jan 29 22:41 config-1610098241.xml -rw-r--r-- 1 root wheel 85629 Jan 29 23:04 config-1611931308.xml -rw-r--r-- 1 root wheel 85595 Jan 29 23:04 config-1611932647.xml -rw-r--r-- 1 root wheel 85561 Jan 29 23:05 config-1611932688.xml -rw-r--r-- 1 root wheel 85530 Jan 29 23:05 config-1611932717.xml -rw-r--r-- 1 root wheel 85499 Jan 29 23:05 config-1611932728.xml -rw-r--r-- 1 root wheel 85467 Jan 29 23:06 config-1611932750.xml -rw-r--r-- 1 root wheel 85433 Apr 22 17:23 config-1611932791.xml -rw-r--r-- 1 root wheel 85454 May 12 18:47 config-1619083402.xml -rw-r--r-- 1 root wheel 85486 May 24 16:04 config-1620816422.xml -rw-r--r-- 1 root wheel 85414 May 24 16:06 config-1621843487.xml |
バックアップスクリプト
予め公開鍵を渡してある別のNASへscpコマンドで設定ファイルをコピーするだけの簡単なスクリプトですが、実は複数台のpfSenseが稼働しているため、そのいずれにも使えるようにホストネームを取得して、保存先のサブディレクトリを振り分けるのに使用しています。
1 2 3 |
#!/bin/sh hname=`/bin/hostname | /usr/bin/cut -d. -f1` /usr/bin/scp /conf/config.xml root@192.168.2.31:/mnt/md0/config/$hname/config_`date +%Y%m%d`.xml |
ちなみにバックアップ先に使うのは、こちらの記事で登場したAlt-Fが載ったD-Link DNS-323 NASです。
cronスケジューラ設定
バックアップスクリプトを適当な場所に配置したら、pfSenseのWebUIでメニューを Services → Cron と進み、cronスケジューラを開きます。
+Addボタンを押して新しいタスクを以下の要領で登録します。
保存先を確認
保存先のNASには次のように、ホストネーム毎のサブフォルダ内に保存されていました。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# ls -l nac1/ -rw-r--r-- 1 root root 85454 Apr 29 23:34 config_20210429.xml -rw-r--r-- 1 root root 85454 Apr 30 23:34 config_20210430.xml -rw-r--r-- 1 root root 85454 May 1 23:34 config_20210501.xml -rw-r--r-- 1 root root 85454 May 2 23:34 config_20210502.xml -rw-r--r-- 1 root root 85454 May 4 23:34 config_20210504.xml -rw-r--r-- 1 root root 85454 May 5 23:34 config_20210505.xml -rw-r--r-- 1 root root 85454 May 6 23:34 config_20210506.xml -rw-r--r-- 1 root root 85454 May 7 23:34 config_20210507.xml -rw-r--r-- 1 root root 85454 May 8 23:34 config_20210508.xml -rw-r--r-- 1 root root 85454 May 9 23:34 config_20210509.xml -rw-r--r-- 1 root root 85454 May 10 23:34 config_20210510.xml -rw-r--r-- 1 root root 85454 May 11 23:34 config_20210511.xml -rw-r--r-- 1 root root 85486 May 12 23:34 config_20210512.xml -rw-r--r-- 1 root root 85486 May 13 23:34 config_20210513.xml -rw-r--r-- 1 root root 85486 May 14 23:34 config_20210514.xml -rw-r--r-- 1 root root 85486 May 15 23:34 config_20210515.xml -rw-r--r-- 1 root root 85486 May 16 23:34 config_20210516.xml -rw-r--r-- 1 root root 85486 May 17 23:34 config_20210517.xml -rw-r--r-- 1 root root 85486 May 18 23:34 config_20210518.xml -rw-r--r-- 1 root root 85486 May 19 23:34 config_20210519.xml -rw-r--r-- 1 root root 85486 May 20 23:34 config_20210520.xml -rw-r--r-- 1 root root 85486 May 21 23:34 config_20210521.xml -rw-r--r-- 1 root root 85486 May 22 23:34 config_20210522.xml -rw-r--r-- 1 root root 85486 May 23 23:34 config_20210523.xml -rw-r--r-- 1 root root 85414 May 24 23:34 config_20210524.xml -rw-r--r-- 1 root root 85414 May 25 23:34 config_20210525.xml -rw-r--r-- 1 root root 85414 May 26 23:34 config_20210526.xml -rw-r--r-- 1 root root 85414 May 27 23:34 config_20210527.xml -rw-r--r-- 1 root root 85414 May 28 23:34 config_20210528.xml -rw-r--r-- 1 root root 85414 May 29 23:34 config_20210529.xml |
小さなファイルとは言え、放っておくと無限に増え続けるので、以前作ったfile rotateの仕組みを使って、直近30個を保持するよう、NAS側にタスクを設定しています。