From d23e5578e86177f53839d82fc90e8ed73c3423f7 Mon Sep 17 00:00:00 2001 From: bornav Date: Fri, 27 Jan 2023 16:02:21 +0100 Subject: [PATCH] Implemented truenas pool managment using curl Signed-off-by: bornav --- curlManageDataset.sh | 51 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100755 curlManageDataset.sh diff --git a/curlManageDataset.sh b/curlManageDataset.sh new file mode 100755 index 0000000..d250c6e --- /dev/null +++ b/curlManageDataset.sh @@ -0,0 +1,51 @@ +#!/bin/bash + +#to use this script the you need to provide host, api_token and pool, optionaly my_secret if you would like to use decryption pasword for your master password + + +#to create secret encrypted using base64 +#echo $MY_SECRET|base64 --decode|openssl enc -aes-256-cbc -a -d -salt -pbkdf2 + +host="" # ip:port ie 10.0.0.200:500 +API_TOKEN="" #api token from truenas +pool="" #example "pool_name/dataset_name" + +export MY_SECRET="" #save here the encrypted password to use password_prottected secret + +user_input(){ +read -p "$1 [l/u/Q]" +if [[ $REPLY =~ ^[Qq]$ ]] +then + exit +fi +} + +if [ -z "$API_TOKEN" ] +then + echo "API_TOKEN is missing" + exit +fi + +user_input "Do you want to lock or unlock the dataset" +if [[ $REPLY =~ ^[Uu]$ ]] +then + if [ -z "$MY_SECRET" ] + then + echo "please enter your pool secret: " + read MY_SECRET + else + MY_SECRET=$(echo -n $MY_SECRET|base64 --decode|openssl enc -aes-256-cbc -a -d -salt -pbkdf2) + fi + + echo $MY_SECRET + curl "https://$host/api/v2.0/pool/dataset/unlock" -k -X POST -H "accept: */*" -H "Content-Type: application/json" -H "Authorization: Bearer 1-$API_TOKEN" -d "{\"id\": \"$pool\",\"unlock_options\": {\"key_file\": false,\"recursive\": false,\"toggle_attachments\": true,\"datasets\": [{\"name\" : \"$pool\" , \"passphrase\" : \"$MY_SECRET\"}]}}" +elif [[ $REPLY =~ ^[Ll]$ ]] +then + curl "https://$host/api/v2.0/pool/dataset/lock" -k -X POST -H "accept: */*" -H "Content-Type: application/json" -H "Authorization: Bearer 1-$API_TOKEN" -d "{\"id\": \"$pool\"}" +else + exit +fi + + + +