Once we have automatic security copies of our data set up. First from our servers or computers to our TrueNAS with some SMB, Nextcloud, Windows backup or whatever. Then from our TrueNAS to our other TrueNAS, replication, synchronization, Syncthing or whatever, because you need to have your data in two places. Only one thing often escapes us in this. That is the copies of our TrueNAS configurations. Sure, everyone does it at least once after configuring it for the first time. Then after every change as well, right? You always remember that, right? Well, I envy it. I find that the source of most problems is generally the protein between the chair and the keyboard so.... no, I don't always remember to back up the configuration after each change.
This is what this material will be about. How to set up automatic configuration backup. First, I will very briefly tell you what the API is and what we need it for. Then I will show how to do a manual configuration backup. Then we will generate a token for the API. I will show how the backup works in TrueCommand and finally I will show how to backup the configuration from the command line using a simple script without going to the TrueNAS website.
What is it and why do we need this API?
We as white users need an interface like a website. We go to such a site we log in, go through the tabs and click on the appropriate place to download the configuration backup. And it works but it doesn't sound like something that could be automated or at least in a simple way. And this is where the API or application programming interface comes in on the white horse. It's a method of communication used in machine-to-machine communication. It is an XML-based communication protocol, which works on the principle of request-response. This is also the case with TrueNAS. Each service provider supporting the API mechanism provides instructions on how to use it. In a nutshell, the instruction says send a query with such and such content such and such a task will be performed. This is how we will create security copies of TrueNAS configurations when responding to a specific query. In addition to the ability to create backup copies through the API, we can manage TrueNAS but this time only backup the configuration.
API key (token) generation
Just like when we log in to our TrueNAS management site and use login password and preferably two-component authentication. So in the case of API for authentication is used for API token. There may be more such API tokens, each system user should have his own, so that if necessary such a key can be canceled or reset.
TrueNAS configuration backup - TrueCommand
One of the best ideas not only for backup configurations but also for managing TrueNAS in general is TrueCommand. Especially if you have more TrueNAS instances. For those interested in the details, I recommend the material [TrueCommand - all TrueNAS in one place]
TrueNAS configuration backup - API script
OK TrueCommand is a nice solution. But if you have at your place not only TrueNAS but also routers, firewalls, servers or other devices. And you are not convinced by maintaining a dedicated supervisor for each of these types of devices. In addition, you are determined to automate the creation of backup configurations. That leaves the next option. We will configure a script that from the command line will download the backup to us from TrueNAS using the API.
The script that creates the backup configuration backup.sh:
#!/bin/bash
# Backup folder
BACKUP_FOLDER=./backups
DEVICE_IP=$1
API_TOKEN=$2
# Create backup folder, if not exist
mkdir -p "$BACKUP_FOLDER"
timestamp=$(date +%Y%m%d%H%M)
filename="${BACKUP_FOLDER}/${DEVICE_IP}_${timestamp}"
echo "Creating backup configuration for device IP $DEVICE_IP"
curl -k -L -X 'POST' https://$DEVICE_IP/api/v2.0/config/save -H \
'Authorization: Bearer '$API_TOKEN -H 'accept: */*' -H \
'Content-Type: application/json' \
--output "${filename}.db"
Remember that the file must be executable:
chmod +x truenas-bkp.sh
An example of a script call:
./truenas-bkp.sh x.x.x.x api_key
Backup automation
We should mention the automation possibilities. As in the case of TrueCommand the matter is settled, but in the case of the above script you need to make more effort and either use an already used automation platform. Here as many administrators as many concepts. Or in the simplest version, add an entry to the cron file in the form:
0 0 * * * user /path/to/file/backup.sh [ip TrueNAS] [API token]
Summary
In a word of summary, configuration backup is also important data that cannot be in one place so we create backups of them. We are lazy and aware of our frailties so we create automations to create these copies. Well, and remember not to keep those copies on that TrueNAS what's that you won't have access to it if you don't have that particular configuration.