OPNsense
  • Home
  • Help
  • Search
  • Login
  • Register

  • OPNsense Forum »
  • English Forums »
  • Tutorials and FAQs »
  • Sample bash script for backups using the API
« previous next »
  • Print
Pages: [1]

Author Topic: Sample bash script for backups using the API  (Read 3964 times)

bartjsmit

  • Hero Member
  • *****
  • Posts: 1411
  • Karma: 156
    • View Profile
Sample bash script for backups using the API
« on: December 23, 2019, 08:48:30 pm »
This is tested on CentOS but should be easily portable to other unix systems. Prerequisites are an OPNsense admin user with an API key, which does not require interactive login or a known password. It does not produce output when successful, so when called from cron it will email root with errors. Binaries needed are date, curl, gzip and find.

Bart...

#!/usr/bin/bash

# Change API key and secret, number of days to keep backups, the path to your backups and the hostname for your firewall

key="api key for backup user"
secret="api secret for backup user"
daystokeep=3
destination="/path/to/firewall/backups"
fwhost="firewall.fqdn"

date=$(date +%Y-%m-%d)

result=$(/usr/bin/curl -I -s -k -u "$key":"$secret" https://$fwhost/api/backup/backup/download | head -1)

if [[ $result != *"200"* ]]; then
   echo "Result of the HTTP request is $result"
   exit 1
fi

/usr/bin/curl -s -k -u "$key":"$secret" https://$fwhost/api/backup/backup/download > $date.xml

error=$?

if [ $error -gt 0 ]; then
   echo "Curl returned error number $error"
   exit 1
fi

/usr/bin/gzip $date.xml

mv $date.xml.gz $destination
/usr/bin/find $destination/* -mtime +$daystokeep -exec rm {} \;
Logged

elg0ch0

  • Newbie
  • *
  • Posts: 2
  • Karma: 0
    • View Profile
Re: Sample bash script for backups using the API
« Reply #1 on: May 29, 2020, 09:58:37 pm »
 Hi,

 I'm trying to back up my OPNSense instance using the REST API but when trying:

Code: [Select]
curl -s -k -u "$key":"$secret" https://$FW/api/backup/backup/download
it returns:

Code: [Select]
{"message":"controller OPNsense\\Core\\Api\\IndexController not found","status":400}
As a test, I tried

Code: [Select]
curl -k -u "$key":"$secret"  https://$FW/api/core/firmware/status
and it works as expected

Currently running on OPNsense 20.1.4-amd64

Any idea what might wrong?

Thanks!
Logged

fabian

  • Hero Member
  • *****
  • Posts: 2740
  • Karma: 195
  • OPNsense Contributor (Language, VPN, Proxy, etc.)
    • View Profile
    • Personal Homepage
Re: Sample bash script for backups using the API
« Reply #2 on: May 29, 2020, 10:59:14 pm »
You may be missing the os-backup-api plugin
Logged

elg0ch0

  • Newbie
  • *
  • Posts: 2
  • Karma: 0
    • View Profile
Re: Sample bash script for backups using the API
« Reply #3 on: June 01, 2020, 10:54:46 pm »
It worked.

Thanks!
Logged

danb35

  • Newbie
  • *
  • Posts: 30
  • Karma: 8
    • View Profile
Re: Sample bash script for backups using the API
« Reply #4 on: July 23, 2020, 03:54:02 pm »
To help this come up in search results, the name of the plugin is os-api-backup, not os-backup-api.
Logged

  • Print
Pages: [1]
« previous next »
  • OPNsense Forum »
  • English Forums »
  • Tutorials and FAQs »
  • Sample bash script for backups using the API
 

OPNsense is an OSS project © Deciso B.V. 2015 - 2022 All rights reserved
  • SMF 2.0.18 | SMF © 2021, Simple Machines
    Privacy Policy
    | XHTML | RSS | WAP2