---
# System prepended metadata

title: Foritgate 自動備份 成功or失敗通知
tags: [Foritgate, linux shell script, 備份]

---

[name=LEON.LIN]
###### tags: `linux shell script` `Foritgate` `備份`
# Foritgate 自動備份 成功or失敗通知

Fortigate 配置
启用 SCP

使用 CLI 启用 Fortigate SCP
``` bash=
config system global
set admin-scp enable
end
```

``` bash=
#!/bin/bash

SERVERS=10.10.18.253
USR=admin
PWD=FG.8ki9m90#^
timestamp=`date +%Y%m%d`
FG=FG800d.conf
BACKUP_FILENAME="$timestamp"_"$FG"

for host in $SERVERS
do

sshpass -p $PWD scp -o StrictHostKeyChecking=no $USR@$host:sys_config /data/FG_conf/"$BACKUP_FILENAME"

done

if [ -f $BACKUP_FILENAME ]
then
        MSG_SUBJECT="Backup $SERVERS OK ($BACKUP_FILENAME)"
        #壓縮
        tar -zvcf "$BACKUP_FILENAME.tar" "$BACKUP_FILENAME"
        #移除未壓縮的檔案
        rm -rf "$BACKUP_FILENAME"
else
        MSG_SUBJECT="Backup $SERVERS FAIL!!"
fi

#30 天前的備份移除
find /data/FG_conf -mtime +30 -name "*.conf.tar" -exec rm -rf {} \;

curl -s -X POST https://api.telegram.org/bot826950629:AAGNfDPnoXLcGfZfY-S_465ukQSjraEMuwA/sendMessage -d chat_id=-1001238056322 -d text=FG800D_"$MSG_SUBJECT" &>/dev/null

exit

```