Advertisement
Guest User

TrueNAS SCALE Config Backup Script

a guest
Mar 15th, 2023
700
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. # Script to backup TrueNAS SCALE configuration file
  2. # WARNING: DOES NOT CHECK IF A VALID BACKUP IS ACTUALLY CREATED
  3. #
  4. #
  5. # Thanks to 'NasKar' and 'engedics' from this thread: https://www.truenas.com/community/threads/best-way-to-get-auto-config-backup.94537/
  6. #
  7. #
  8. # If a valid backup file is not created then there is something wrong with the API call
  9. # Check that your URL and API Key are both correct
  10. #
  11.  
  12.  
  13. # # # # # # # # # # # # # # # #
  14. # USER CONFIGURABLE VARIABLES #
  15. # # # # # # # # # # # # # # # #
  16.  
  17.  
  18. # Server IP or URL (include http(s)://)
  19. serverURL="https://<SERVER IP OR URL>"
  20.  
  21. # TrueNAS API key (Generate from 'User Icon' -> 'API Keys' in TrueNAS WebGUI)
  22. apiKey="<API KEY>"
  23.  
  24. # Include Secret Seed (true| false)
  25. secSeed=true
  26.  
  27. # Path on server to store backups
  28. backuploc="<PATH TO STORE BACKUPS>"
  29.  
  30. # Max number of backups to keep (set as 0 to never delete anything)
  31. maxnrOfFiles=5
  32.  
  33.  
  34. # # # # # # # # # # # # # # # # # #
  35. # END USER CONFIGURABLE VARIABLES #
  36. # # # # # # # # # # # # # # # # # #
  37.  
  38.  
  39. echo
  40. echo "Backing up current TrueNAS config"
  41.  
  42. # Check current TrueNAS version number
  43. versiondir=`cat /etc/version | cut -d' ' -f1`
  44.  
  45. # Set directory for backups to: 'path on server' / 'current version number'
  46. backupMainDir="${backuploc}/${versiondir}"
  47.  
  48. # Create directory for for backups (Location/Version)
  49. mkdir -p $backupMainDir
  50.  
  51.  
  52. # Use appropriate extention if we are exporting the secret seed
  53. if [ $secSeed = true ]
  54. then
  55. fileExt="tar"
  56. echo "Secret Seed will be included"
  57. else
  58. fileExt="db"
  59. echo "Secret Seed will NOT be included"
  60. fi
  61.  
  62. # Generate file name
  63. fileName=$(hostname)-TrueNAS-$(date +%Y%m%d%H%M%S).$fileExt
  64.  
  65.  
  66. # API call to backup config and include secret seed
  67. curl --no-progress-meter \
  68. -X 'POST' \
  69. $serverURL'/api/v2.0/config/save' \
  70. -H 'Authorization: Bearer '$apiKey \
  71. -H 'accept: */*' \
  72. -H 'Content-Type: application/json' \
  73. -d '{"secretseed": '$secSeed'}' \
  74. --output $backupMainDir/$fileName
  75.  
  76. echo
  77. echo "Config saved to ${backupMainDir}/${fileName}"
  78.  
  79. #
  80. # The next section checks for and deletes old backups
  81. #
  82. # Will not run if $maxnrOfFiles is set to zero (0)
  83. #
  84.  
  85. if [ ${maxnrOfFiles} -ne 0 ]
  86. then
  87. echo
  88. echo "Checking for old backups to delete"
  89. echo "Number of files to keep: ${maxnrOfFiles}"
  90.  
  91. # Get number of files in the backup directory
  92. nrOfFiles="$(ls -l ${backupMainDir} | grep -c "^-.*")"
  93.  
  94. echo "Current number of files: ${nrOfFiles}"
  95.  
  96. # Only do something if the current number of files is greater than $maxnrOfFiles
  97. if [ ${maxnrOfFiles} -lt ${nrOfFiles} ]
  98. then
  99. nFileToRemove="$((nrOfFiles - maxnrOfFiles))"
  100. echo "Removing ${nFileToRemove} file(s)"
  101. while [ $nFileToRemove -gt 0 ]
  102. do
  103. fileToRemove="$(ls -t ${backupMainDir} | tail -1)"
  104. echo "Removing file ${fileToRemove}"
  105. nFileToRemove="$((nFileToRemove - 1))"
  106. rm ${backupMainDir}/${fileToRemove}
  107. done
  108. fi
  109. # Inform the user that no files will be deleded if $maxnrOfFiles is set to zero (0)
  110. else
  111. echo
  112. echo "NOT deleting old backups because '\$maxnrOfFiles' is set to 0"
  113. fi
  114.  
  115. #All Done
  116.  
  117. echo
  118. echo "DONE!"
  119. echo
  120.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement