Python
Before even learning python during my studies, I embarked on developing a script (tho i've already developed something else 6 years before).
A python script ?
Easy to run, to implement, and it was ok with the company, so doing so wansn't too bad. I could keep using only a linux bash script, but I found Python easier to implement in order to send an email if an error occured.
How does my script work ?
This backup script started at the origin as a backup for Mariadb and GLPI in the GLPI project I made.
So in this explanation and image, the process will be based on that.
This script is run in a podman container, to which IMAP credential and server are provided through environement variables.
This allows to keep the logins informations more secure than if it was written in a plain text file (Don't do it).

The script includes a timer, with a loop executing it every day at a precise hour. This is not really necessary as it could be done with cron.
When the time is right, the python scrit executes, and starts by executing linux shell commands with the subprocess library, checking each time if there is an error :
- Send an SSH command to the Mariadb podman server > Dump database
- Use Rsync to copy the database dump.
- Use Rsync to copy the GLPI files.
- The fourth step is an if :
- If there was no error : Compress everyting in a tar.gz archive, and send it somewhere safe.
- If there was an error, here it complexifies, it starts the error process.
The error process :
If during a bash command there is an error, the script collects it and appends it to a Variable "Error".
Then, the script checks if Error is not null. If it is not null, it executes the error process.
In this project, I am using environement variables. So we have to use a library allowing us to read environement variables.
However, the issue is that Python's variables and process doesn't see the linux variables.
So to get it to work, we have to bring those variables inside Python's environement, and to do so, I used the dotenv library.
By using the command dotenv.load_dotenv()
, we can get those variables inside Python, then recover them with the
os library using var = os.environ.get('myvar')
.
Then after getting the variables, it uses those to write an email with the smtplib library.
In the mail, there is a little description text, then the errors.
Once it sends the email, the script also logs the errors in a logfile.