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

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


#!/usr/bin/env bash

# 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
# https://strobelstefan.de/2019/10/22/pi-hole-automatische-sicherung-der-konfigurationsdateien/
#
# Copyright by Stefan Strobel
# https://www.strobelstefan.org
#

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

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

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

# E-Mail Address
EMAIL="mail@address.de"

#Log file
LOGFILE="/mnt/usb/teleporter/pihole_teleporter.log"
exec 1> ${LOGFILE}


# Script name
MAINSCRIPT=$(basename -- "$0")

###################################
# This removes your old log file
###################################
# This removes your old log file.
# When you run the script the first time an error will be displayed,
# because no log file is in the defined path.

if test -f "${LOGFILE}"
	then
		echo "++++++++++++++++++++++++++" >&1
		echo "$(date +%Y-%m-%d_%H-%M-%S) - Start ${MAINSCRIPT} on $(hostname)" >&1
		echo "++++++++++++++++++++++++++" >&1
		echo >&1
		echo "$(date +%Y-%m-%d_%H-%M-%S) - Log File already exists!" >&1
		echo "$(date +%Y-%m-%d_%H-%M-%S) - Log File location: ${LOGFILE}" >&1
	else
		touch ${LOGFILE}
		echo "++++++++++++++++++++++++++" >&1
		echo "$(date +%Y-%m-%d_%H-%M-%S) - Start ${MAINSCRIPT} on $(hostname)" >&1
		echo "++++++++++++++++++++++++++"  >&1
		echo >&1
		echo "$(date +%Y-%m-%d_%H-%M-%S) - Log File newly created!" >&1
		echo "$(date +%Y-%m-%d_%H-%M-%S) - Log File location: ${LOGFILE}" >&1
fi


###################################
# Pi-hole - Teleporter Export
###################################
# Create a configuration backup. The backup will be created in the directory from which the command is run.
# Go to storage path

echo "++++++++++++++++++++++++++" >&1
echo "$(date +%Y-%m-%d_%H-%M-%S) - Change Directory: ${STORAGEPATH}" >&1
echo "++++++++++++++++++++++++++" >&1
echo >&1

cd ${STORAGEPATH} >&1


echo "++++++++++++++++++++++++++" >&1
echo "$(date +%Y-%m-%d_%H-%M-%S) - Start TELEPORTER" >&1
echo "++++++++++++++++++++++++++" >&1
echo >&1

# This command exports the configuration as a tar.gz file into the current directory
pihole -a teleporter

echo "++++++++++++++++++++++++++" >&1
echo "$(date +%Y-%m-%d_%H-%M-%S) - Finished TELEPORTER" >&1
echo "$(date +%Y-%m-%d_%H-%M-%S) - Saved to script location: ${STORAGEPATH}" >&1
echo "++++++++++++++++++++++++++" >&1
echo >&1

###################################
# Delete - Old Teleporter Export
###################################
# Deletes old teleporter files
# 43200 minutes = 30 days

echo "++++++++++++++++++++++++++" >&1
echo "$(date +%Y-%m-%d_%H-%M-%S) - Start to delete old TELEPORTER Files" >&1
echo "++++++++++++++++++++++++++" >&1
echo >&1

find ${STORAGEPATH}/pi-hole-*.* -type f -mmin +43200 -exec rm {} \; >&1

echo "++++++++++++++++++++++++++" >&1
echo "$(date +%Y-%m-%d_%H-%M-%S) - Finished to delete old TELEPORTER Files" >&1
echo "++++++++++++++++++++++++++" >&1
echo >&1

###################################
# Send EMail - Teleporter Log
###################################
# Optional
# Send your Pi-hole teleporter log file via e-mail
echo "++++++++++++++++++++++++++" >&1
echo "$(date +%Y-%m-%d_%H-%M-%S) - Send Status Email to ${EMAIL}" >&1
echo "++++++++++++++++++++++++++" >&1
echo >&1

echo $(hostname)' Pi-hole TELEPORTER' | mutt ${EMAIL} -a ${LOGFILE} -s $(hostname)" Pi-hole - Teleporter Export" >&1

exit 0

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/

Artikel überarbeitet

  • Dezember 2022

Schreibe einen Kommentar

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