Configuration backup from terminal within the server?

sretalla

Powered by Neutrality
Moderator
Joined
Jan 1, 2016
Messages
9,703
Have a look at how it's done here:


A bit over 100 lines in you have the code for config backup.

I guess there's also the API call...

midclt call config.save (but I have no idea how that really works as you don't seem to have the option to specify the filename).
 
Last edited:

Daisuke

Contributor
Joined
Jun 23, 2011
Messages
1,041
I always forget to look at the API. They say to download the file, you need call a download job and pass one of few required keys, example:
Code:
# midclt call("core.download", ["config.save", [{ "secretseed": true }], "config.tar.gz"])
zsh: number expected

I'm missing something small... the parenthesis in zsh expect a version or number.

1673720071194.png
 
Last edited:

KristijanL

Dabbler
Joined
Jun 28, 2013
Messages
13
there is a bug in truenas scale API, if you call without any parameters, you will get a backup, but with add any parameter you will get an error

works:
Code:
curl -X 'POST' \
  'http://192.168.1.200/api/v2.0/config/save' \
  -H 'accept: */*' \
  -H 'Authorization: Basic AUTH' \
  -H 'Content-Type: application/json' \
  -d '{}' --output file


does not work:
Code:
curl -X 'POST' \
  'http://192.168.1.200/api/v2.0/config/save' \
  -H 'accept: */*' \
  -H 'Authorization: Basic AUTH' \
  -H 'Content-Type: application/json' \
  -d '{"secretseed": "true" }' --output file


there is an issue with evaluating values, as they are not converted to boolean

error log:
Code:
[2023/01/14 22:50:50] (ERROR) middlewared.job.run():438 - Job <bound method accepts.<locals>.wrap.<locals>.nf of <middlewared.plugins.config.ConfigService object at 0x7fde85d0d2e0>> failed
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/middlewared/job.py", line 426, in run
    await self.future
  File "/usr/lib/python3/dist-packages/middlewared/job.py", line 461, in __run_body
    rv = await self.method(*([self] + args))
  File "/usr/lib/python3/dist-packages/middlewared/schema.py", line 1283, in nf
    args, kwargs = clean_and_validate_args(args, kwargs)
  File "/usr/lib/python3/dist-packages/middlewared/schema.py", line 1277, in clean_and_validate_args
    raise verrors
middlewared.service_exception.ValidationErrors: [EINVAL] configsave.secretseed: true - DEBUG Not a boolean
 
Joined
Oct 22, 2019
Messages
3,641
I might be reading the original post incorrectly, so forgive me in advance.

Are you trying to automate the creation of a backup config file, and have it saved to a particular folder on the server itself? The original post makes no mention of "downloading" the file.

If so, this is what I came up with on TrueNAS Core (FreeBSD-based), but the same principle should technically apply to SCALE (Debian-based). You'll likely have to change the syntax for the date timestamp, due to FreeBSD vs Linux.

It's a single line that can be called with a timer or trigger. Needless to say, it must be run with root privileges.

backup-config-to-tarball.sh
Code:
#!/bin/bash

# Create tarball (.tar) of current config database and secret seed.

tar -c -f /mnt/mypool/configs/`cat /etc/version | cut -f1 -d " "`-$(date "+%Y%m%d%H%M%S").tar -C /data freenas-v1.db pwenc_secret


This will create a tarball in the directoy /mnt/mypool/configs/ named TrueNAS-13.0-U3.1-20230116124328.tar (the timestamp will be different each time.) Within this tarball (.tar archive), it will contain the config database and the password secret seed.

As I'm only on Core, there could be changes in paths, filenames, and the syntax for a SCALE system.

For SCALE, consider and adjust if needed:
  • date command syntax
  • Existence and output of /etc/version
  • Location and filenames under the /data/ directory
 
Last edited:

danb35

Hall of Famer
Joined
Aug 16, 2011
Messages
15,504
The config database is at /data/freenas-v1.db, so by far the simplest way to take a backup of it from the CLI is to just make a copy of that file. Slightly less simple, but more complete, is what's posted above. An improvement on both of them would be to figure out what incantation of sqlite3 would check for/guarantee the consistency of that .db file before creating the tarball.
 

Daisuke

Contributor
Joined
Jun 23, 2011
Messages
1,041
@anodos, any idea what am I missing on my post? That command should generate a tarball that I can push it to a private Git repo.
It's a single line that can be called with a timer or trigger.
The correct way to do it is through the midclt command I posted, see the explanation into above linked post. We are missing something small, one of the devs will open our eyes.
 

Samuel Tai

Never underestimate your own stupidity
Moderator
Joined
Apr 24, 2020
Messages
5,399
@Daisuke, the correct syntax for the API-driven configuration save is midclt call core.download "config.save" '[ {} ]' "config.tar". This will result in output

[<job id>, "/_download/<job id>?auth_token=<auth token string>"]

You can then curl --output "config.tar" https://<TrueNAS server IP>/_download/<job id>?auth_token=<auth token string> to retrieve the saved config.
 

Samuel Tai

Never underestimate your own stupidity
Moderator
Joined
Apr 24, 2020
Messages
5,399
Hmm. I don't have a Scale system, but on Core, the backup is a plain TAR file.
 

Samuel Tai

Never underestimate your own stupidity
Moderator
Joined
Apr 24, 2020
Messages
5,399
Top