{"id":1171,"date":"2024-03-01T17:00:07","date_gmt":"2024-03-01T16:00:07","guid":{"rendered":"https:\/\/eonea.pl\/?p=1171"},"modified":"2024-04-05T10:52:30","modified_gmt":"2024-04-05T08:52:30","slug":"truenas-automatyczny-backup-konfiguracji-api-skrypty-truecommand","status":"publish","type":"post","link":"https:\/\/eonea.pl\/en\/truenas-automatyczny-backup-konfiguracji-api-skrypty-truecommand\/","title":{"rendered":"TrueNAS - automatic configuration backup - APIs, scripts, TrueCommand"},"content":{"rendered":"<div data-elementor-type=\"wp-post\" data-elementor-id=\"1171\" class=\"elementor elementor-1171\" data-elementor-post-type=\"post\">\n\t\t\t\t<div class=\"elementor-element elementor-element-cd8088f e-flex e-con-boxed e-con e-parent\" data-id=\"cd8088f\" data-element_type=\"container\">\n\t\t\t\t\t<div class=\"e-con-inner\">\n\t\t\t\t<div class=\"elementor-element elementor-element-c1940d5 elementor-widget elementor-widget-video\" data-id=\"c1940d5\" data-element_type=\"widget\" data-settings=\"{&quot;youtube_url&quot;:&quot;https:\\\/\\\/youtu.be\\\/XcpzKbAcbW0&quot;,&quot;video_type&quot;:&quot;youtube&quot;,&quot;controls&quot;:&quot;yes&quot;}\" data-widget_type=\"video.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"elementor-wrapper elementor-open-inline\">\n\t\t\t<div class=\"elementor-video\"><\/div>\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t<div class=\"elementor-element elementor-element-87aae78 e-flex e-con-boxed e-con e-parent\" data-id=\"87aae78\" data-element_type=\"container\">\n\t\t\t\t\t<div class=\"e-con-inner\">\n\t\t\t\t<div class=\"elementor-element elementor-element-4eb1c22 elementor-widget elementor-widget-text-editor\" data-id=\"4eb1c22\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p style=\"text-align: justify;\"><a>TrueNAS configuration backup. Because in addition to the drip of the data itself, we must also have a copy of the TrueNAS configuration. Remember that even if we have data disks and we lose the system disk, at best we are facing a lot of configuration and at worst we may find that we have lost, for example, the data encryption keys on our disks so.... de facto we have lost all data. That you need to backup TrueNAS configurations we already know, but how to do it? How to do it in the PRO version and in addition automatically? There are several ways. I invite you to see the whole material.S<\/a><\/p><div><div><div><!-- [if !supportAnnotations]--><\/div><p><!--[endif]--><\/p><\/div><\/div>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-778751a elementor-widget elementor-widget-text-editor\" data-id=\"778751a\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>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.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-35f2d01 elementor-widget elementor-widget-text-editor\" data-id=\"35f2d01\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>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.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-c351cf0 elementor-widget elementor-widget-text-editor\" data-id=\"c351cf0\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p><strong>What is it and why do we need this API?<\/strong><\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-260ef3a elementor-widget elementor-widget-text-editor\" data-id=\"260ef3a\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>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.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-533b296 elementor-widget elementor-widget-text-editor\" data-id=\"533b296\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p><strong>API key (token) generation<\/strong><\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-ee8fe53 elementor-widget elementor-widget-text-editor\" data-id=\"ee8fe53\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>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.\u00a0<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-f7b7a29 elementor-widget elementor-widget-text-editor\" data-id=\"f7b7a29\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>\u00a0<strong>TrueNAS configuration backup - TrueCommand<\/strong><\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-4b21caa elementor-widget elementor-widget-text-editor\" data-id=\"4b21caa\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>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 [<a href=\"https:\/\/youtu.be\/rClX7jDS_4c\">TrueCommand - all TrueNAS in one place<\/a>]\u00a0<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-55e2a52 elementor-widget elementor-widget-text-editor\" data-id=\"55e2a52\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p><strong>TrueNAS configuration backup - API script<\/strong><\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-e713c24 elementor-widget elementor-widget-text-editor\" data-id=\"e713c24\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>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.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-57231dc elementor-widget elementor-widget-text-editor\" data-id=\"57231dc\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>The script that creates the backup configuration backup.sh:<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-c6aa7aa elementor-widget elementor-widget-code-highlight\" data-id=\"c6aa7aa\" data-element_type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-default copy-to-clipboard\">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-javascript line-numbers\">\n\t\t\t\t<code readonly=\"true\" class=\"language-javascript\">\n\t\t\t\t\t<xmp>#!\/bin\/bash\r\n# Backup folder\r\nBACKUP_FOLDER=.\/backups\r\nDEVICE_IP=$1\r\nAPI_TOKEN=$2\r\n# Create backup folder, if not exist\r\nmkdir -p \"$BACKUP_FOLDER\"\r\ntimestamp=$(date +%Y%m%d%H%M)\r\nfilename=\"${BACKUP_FOLDER}\/${DEVICE_IP}_${timestamp}\"\r\n\r\necho \"Creating backup configuration for device IP $DEVICE_IP\"\r\n\r\ncurl -k -L -X 'POST' https:\/\/$DEVICE_IP\/api\/v2.0\/config\/save -H \\\r\n'Authorization: Bearer '$API_TOKEN -H 'accept: *\/*' -H \\\r\n'Content-Type: application\/json' \\\r\n--output \"${filename}.db\"\r\n<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-5d54952 elementor-widget elementor-widget-text-editor\" data-id=\"5d54952\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Remember that the file must be executable:<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-7b86eb1 elementor-widget elementor-widget-code-highlight\" data-id=\"7b86eb1\" data-element_type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-default copy-to-clipboard\">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-javascript line-numbers\">\n\t\t\t\t<code readonly=\"true\" class=\"language-javascript\">\n\t\t\t\t\t<xmp>chmod +x truenas-bkp.sh <\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-09fc61e elementor-widget elementor-widget-text-editor\" data-id=\"09fc61e\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>An example of a script call:<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-634b0d7 elementor-widget elementor-widget-code-highlight\" data-id=\"634b0d7\" data-element_type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-default copy-to-clipboard\">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-javascript line-numbers\">\n\t\t\t\t<code readonly=\"true\" class=\"language-javascript\">\n\t\t\t\t\t<xmp>.\/truenas-bkp.sh x.x.x.x  api_key <\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-1efcd7a elementor-widget elementor-widget-text-editor\" data-id=\"1efcd7a\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p><strong>Backup automation <\/strong><\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-4f30f6a elementor-widget elementor-widget-text-editor\" data-id=\"4f30f6a\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>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:<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-ce99a73 elementor-widget elementor-widget-code-highlight\" data-id=\"ce99a73\" data-element_type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-default copy-to-clipboard\">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-javascript line-numbers\">\n\t\t\t\t<code readonly=\"true\" class=\"language-javascript\">\n\t\t\t\t\t<xmp>0 0 * * * user \/path\/to\/file\/backup.sh [ip TrueNAS] [API token] <\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-efa820f elementor-widget elementor-widget-text-editor\" data-id=\"efa820f\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p><strong>Summary <\/strong><\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-013e76c elementor-widget elementor-widget-text-editor\" data-id=\"013e76c\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>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.\u00a0\u00a0<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>","protected":false},"excerpt":{"rendered":"<p>Backup of TrueNAS configuration. How to do it in the PRO version and in addition automatically? There are several ways. I invite you to see the whole material.<\/p>","protected":false},"author":3,"featured_media":1305,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[8,12],"tags":[296,293,276,81,85,295,297,92,298,294,93,86],"class_list":["post-1171","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-truenas","category-vlog","tag-api","tag-data-loss-prevention","tag-dlp","tag-ochrona-danych","tag-truenas","tag-truenas-api-backup","tag-truenas-automatic-backup","tag-truenas-backup","tag-truenas-backup-script","tag-truenas-configuration-backup","tag-truenas-core","tag-truenas-scale"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/eonea.pl\/en\/wp-json\/wp\/v2\/posts\/1171","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eonea.pl\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eonea.pl\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eonea.pl\/en\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/eonea.pl\/en\/wp-json\/wp\/v2\/comments?post=1171"}],"version-history":[{"count":32,"href":"https:\/\/eonea.pl\/en\/wp-json\/wp\/v2\/posts\/1171\/revisions"}],"predecessor-version":[{"id":1312,"href":"https:\/\/eonea.pl\/en\/wp-json\/wp\/v2\/posts\/1171\/revisions\/1312"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/eonea.pl\/en\/wp-json\/wp\/v2\/media\/1305"}],"wp:attachment":[{"href":"https:\/\/eonea.pl\/en\/wp-json\/wp\/v2\/media?parent=1171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eonea.pl\/en\/wp-json\/wp\/v2\/categories?post=1171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eonea.pl\/en\/wp-json\/wp\/v2\/tags?post=1171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}