Zum Inhalt

Pi-hole - Automatische Sicherung der Konfigurationsdateien mit Teleporter

Die Konfiguration eines Pi-hole kann mit dem Tool Teleporter sehr einfach über die Weboberfläche oder die CLI gesichert werden.

Mit einem kleinen Shell-Skript und Cron lässt sich der Sicherungsprozess automatisieren.

Für den E-Mail-Versand wird Postfix und mutt benötigt.

Die Installationsanleitung für Postfix gibt es hier:

👉 E-Mail-Versand für den Raspberry Pi konfigurieren.

mutt lässt sich aus den Paketquellen installieren:

sudo apt install mutt
````

```bash
#!/bin/bash

# by strobelstefan.org
# 2019-10-22
# Version: 1.0
# https://strobelstefan.de/?p=7746

#
# This script exports the pi-hole configuration
# You can trigger the export manually by pihole -a teleporter
# This command will create a zip file that contains all relevant Pi-hole scripts

###################################
# Define Variables
###################################

# Storage device as defined in your /etc/fstab.
mountpoint='/mnt/backuplaufwerk/'

# Path were the image of your SD card should be saved to
STORAGEPATH="/mnt/backuplaufwerk/pihole/teleporter/"

# E-Mail Address
EMAIL="email@email.de"

#Log file
LOGFILE="/var/log/pihole_teleporter.log"



# This removes your old log file
rm ${LOGFILE}

###################################
# MOUNTPOINT Section - Check Mount point Availability
###################################
# It checks if your mount point is accessible by your RPi.
# This is a crucial step, if the storage is not available the clone process of the SD card cannot conducted.


if [ "$(findmnt ${mountpoint})" ] ; 
    then
        echo $(date +%Y-%m-%d_%H-%M-%S) " - Mount point accessible by your Raspberry Pi" >> ${LOGFILE}
    else
        echo $(date +%Y-%m-%d_%H-%M-%S) " - Mount point was not accessible, try to mount it now as defined in your /etc/fstab" >> ${LOGFILE}

    #This command mounts all storages defined in /etc/fstab
    mount -a

    if [ $? != 0 ]
        then
            echo $(date +%Y-%m-%d_%H-%M-%S) " - Mount of storage in first try successfully completed" >> ${LOGFILE}
        sleep 5
            mount -a
        if [ $? != 0 ]
        then
            echo $(date +%Y-%m-%d_%H-%M-%S) " - Backup FAILED! Was not able to mount your storage device. Stop backup process. You have to check it manually." >> ${LOGFILE}
            echo "Sent backup status via e-mail" | mutt ${EMAIL} -a ${LOGFILE} -s $HOSTNAME" - Backup FAILED" >> ${LOGFILE}
        exit
        fi
    fi

fi


###################################
# Pi-hole - Teleporter Export
###################################

# This command exports the configuration as a tar.gz file into the directory where this script is located
pihole -a teleporter
echo $(date +%Y-%m-%d_%H-%M-%S) " - Pi-hole Teleporter Export created and saved to script location" >> ${LOGFILE}

#Optional
#Send your Pi-hole teleporter export via e-mail
echo $(date +%Y-%m-%d_%H-%M-%S) " - Pi-hole Teleporter Export sent via e-mail to" ${EMAIL} >> ${LOGFILE}
echo "Sent backup status via e-mail" | mutt ${EMAIL} -a ${LOGFILE} pi-hole-teleporter*.tar.gz -s $HOSTNAME" - Teleporter Export" >> ${LOGFILE}

# Move the exported configuration from the script directory into your defined directory
sudo mv pi-hole-teleporter*.tar.gz ${STORAGEPATH}
echo $(date +%Y-%m-%d_%H-%M-%S) " - Pi-hole Teleporter moved to storage path" ${STORAGEPATH} >> ${LOGFILE}

# Deletes old teleporter files
#43200 Minuten = 30 Tage
find ${STORAGEPATH}/pi-hole-teleporter*.* -type f -mmin +43200 -exec rm {} \;
echo $(date +%Y-%m-%d_%H-%M-%S) "Deleted old image files"  >> ${LOGFILE}

#Unmounts your backup device
umount ${mountpoint}
echo $(date +%Y-%m-%d_%H-%M-%S) "Unmounted storage device"  >> ${LOGFILE}

Das Skript ausführbar machen:

sudo chmod +x pihole\_teleporter.sh

Damit das Backup auch noch automatisch abläuft ist noch ein Cron Job zu erstellen:

sudo crontab -e

Zum täglichen Ausführen des Skript um 23:00Uhr ist die Zeile notwendig:

0 23 \* \* \* /bin/bash /etc/skripte/pihole\_teleporter.sh

Der Ausführungszeitpunk des Cron kann natürlich angepasst werden. Dafür kann der crontab-generator verwendet werden:

👉 https://crontab-generator.org/

Gib mir gerne einen Kaffee ☕ aus ❗️

Wenn dir meine Beiträge gefallen und geholfen haben, dann kannst du mir gerne einen Kaffee ☕️ ausgeben.

Donation via PayPalDonation via LiberaPay

Donation via Bitcoin
Bitcoin Address: bc1qfuz93hw2fhdvfuxf6mlxlk8zdadvnktppkzqzj