This script is designed to automate the process of creating backups of your TrueNAS configuration and deleting old backups once a specified limit is reached. The script uses environment variables to configure various settings, such as the server URL, API key, backup location, and the maximum number of backup files to keep. Here's a simple explanation of the main components of the script:
Import required libraries: The script imports various libraries needed for its functionality, such as os, subprocess, requests, schedule, time, logging, datetime, and dotenv. Load environment variables: The script uses the dotenv library to load environment variables from a .env file, which contains user-configurable variables like the server URL, API key, secret seed, backup location, maximum number of backup files, and scheduled time for the backup.
Create the backup directory: The script creates a directory for storing backup files using the os.makedirs() function.
Define the backup function: The backup() function is responsible for creating a new backup of the TrueNAS configuration and deleting old backups if the maximum number of backup files is reached. The function does the following: Generates a file name for the backup based on the hostname and the current date and time. Sends a POST request to the TrueNAS API to save the configuration. Writes the configuration data to a file in the backup directory. Checks if there are more backup files than the maximum number allowed and deletes the oldest backup files until the limit is reached.
Schedule the backup function: The script uses the schedule library to schedule the backup() function to run at the specified time every day. Keep the script running: The script continuously runs the scheduled tasks using a while loop, which checks for pending tasks and sleeps for one second between checks. The script is designed to run indefinitely, creating new backups and deleting old ones according to the specified schedule and maximum number of backup files allowed.