Pi-hole – Automatische Sicherung der Konfigurationsdateien

Für den Werbeblocker Pi-hole lässt sich sehr einfach eine Sicherung der wichtigsten Konfigurationsdateien erstellen, damit im Fall der Fälle schnell eine Wiederherstellung aus dem Backup möglich ist.


Neue Version

Zur neuen Version des Skripts geht es hier

https://strobelstefan.de/2021/04/22/pi-hole-automatische-sicherung-der-konfigurationsdateien-neue-version/


Pi-hole bietet im Standard mit dem Befehl „pihole -a teleporter“ die Möglichkeit an schnell und einfach die wichtigsten Dateien in ein tar.gz-Archiv zu sichern. Da die Datei dabei jedoch auf der SD-Karte des Raspberry Pis verbleibt habe ich ein kleiner Skript geschrieben, dass den Export per E-Mail versenden kann und auf ein Speichermedium verschieben kann. Per Cron kann man das ganze sehr einfach automatisieren und braucht sich so weniger Gedanken über das Backup machen.

Wenn ihr die E-Mail-Funktion des Skripts nutzen möchtet, wird „mutt“ benötigt, dass sehr einfach aus den Repositories installiert werden kann.

sudo aptitude install mutt

Das Skript „pihole_teleporter.sh“ könnt ihr in ein beliebiges Verzeichnis auf eurem Pi kopieren, die Variablen anpassen und dann das Skript noch ausführbar machen. Ich das Skript in das Verzeichnis „/etc/skripte/“ kopiert.

#!/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}

Gib mir gerne einen Kaffee ☕ aus!

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

PayPal Logo


liberapay.com/strobelstefan.org


Kaffee via Bitcoin

bc1qfuz93hw2fhdvfuxf6mlxlk8zdadvnktppkzqzj


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

Möchtet ihr das Skript zu einem anderen Zeitpunkt ausführen, könnt ihr auch den Crontab Generator nutzen ➡ https://crontab-generator.org/

2 Antworten auf „Pi-hole – Automatische Sicherung der Konfigurationsdateien“

  1. Super Anleitung, vielen Dank!Ich würde das Ganze gerne noch um ein SD Karten Backup mit dd erweitern. #Export Image of SD Carddd if=/dev/mmcblk0 of=${STORAGEPATH2}/${BACKUP_NAME}-$(date +%Y%m%d-%H%M%S).img bs=1MBMuss für das Backup pihole-FTL beendet werden oder kann man das gleichzeitig machen?

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert