Hallo zusammen,
heute möchte ich gerne einmal mein Backup Script teilen, welches ich nutze um meine Docker Container zu sichern.
Die entsprechenden Mount Points, also die persistenten Daten, werden dabei auf ein NFS Volume kopiert und dort gesichert.
Das Backup Script besteht aus den folgenden Funktionen
- Backup Verzeichnis wird erstellt (lokal auf dem Host)
- alle aktuell laufenden Docker Container werden gestoppt
- sämtliche Docker-Volumes werden gesichert
- alle zuvor laufenden Docker Container werden wieder gestartet
- das Backup wird per .zip auf den entsprechenden NFS-Pfad kopiert
- das lokale Backup wird gelöscht
- im Anschluss wird eine E-Mail gesendet mit dem Status zum Backup
Die entsprechende .zip Datei (euer Backup) sieht anschließend wie folgt aus:

Anbei nun das Backupscript.
Passt die folgenden Variablen nach euren Bedürfnissen und eurer Umgebung an – der Rest kann so bleiben wie er ist.

#!/bin/bash
# Datum und Uhrzeit für die Backup-Datei
BACKUP_DATE=$(date +'%Y-%m-%d_%H-%M-%S')
BACKUP_DIR="/home" # Lokales Verzeichnis für das Backup
BACKUP_FILE="$BACKUP_DIR/docker_volumes_backup_$BACKUP_DATE.zip"
# NFS-Zielinformationen
NFS_SERVER="NFS IP"
NFS_PATH="NFS Pfad"
NFS_MOUNT_POINT="/mnt/nfs_backup" # Lokaler Mount-Punkt für das NFS
# E-Mail-Konfiguration
SMTP_SERVER="SMTP IP"
EMAIL_FROM="Absender"
EMAIL_TO="Empfänger"
EMAIL_SUBJECT="Docker Backup Status - $BACKUP_DATE"
EMAIL_BODY=""
# Erstelle Backup-Verzeichnis, falls nicht vorhanden
mkdir -p "$BACKUP_DIR"
# Schritt 1: Speichere IDs der laufenden Docker-Container
echo "Saving IDs of running Docker containers..."
RUNNING_CONTAINERS=$(docker ps -q)
# Schritt 2: Alle laufenden Docker-Container stoppen
echo "Stopping all running Docker containers..."
docker stop $RUNNING_CONTAINERS
# Schritt 3: Mount Points sichern
echo "Backing up Docker container volumes to $BACKUP_FILE..."
VOLUME_PATHS=()
# Hole alle Container-IDs
for CONTAINER_ID in $(docker ps -a -q); do
# Hole die Volumes/Mountpoints des Containers
MOUNTS=$(docker inspect --format='{{range .Mounts}}{{.Source}} {{end}}' $CONTAINER_ID)
# Füge Mount-Pfade zum Array hinzu
for MOUNT in $MOUNTS; do
if [ -d "$MOUNT" ]; then
VOLUME_PATHS+=("$MOUNT")
fi
done
done
# Falls keine Mounts gefunden wurden
if [ ${#VOLUME_PATHS[@]} -eq 0 ]; then
echo "No volumes to backup."
EMAIL_BODY="No volumes to backup.\n"
else
# Sicherung erstellen
zip -r "$BACKUP_FILE" "${VOLUME_PATHS[@]}"
if [ $? -eq 0 ]; then
EMAIL_BODY="Backup completed successfully: $BACKUP_FILE\n"
echo "Backup completed: $BACKUP_FILE"
else
EMAIL_BODY="Backup failed during zip creation.\n"
echo "Backup failed."
fi
fi
# Schritt 4: Nur zuvor laufende Docker-Container neu starten
if [ -n "$RUNNING_CONTAINERS" ]; then
echo "Restarting previously running Docker containers..."
docker start $RUNNING_CONTAINERS
echo "Previously running containers have been restarted."
else
echo "No containers were running before the backup."
fi
# Schritt 5: NFS-Verzeichnis mounten (falls noch nicht gemountet)
if ! mount | grep -q "$NFS_MOUNT_POINT"; then
echo "Mounting NFS share $NFS_SERVER:$NFS_PATH to $NFS_MOUNT_POINT..."
mkdir -p "$NFS_MOUNT_POINT"
mount -t nfs "$NFS_SERVER:$NFS_PATH" "$NFS_MOUNT_POINT"
if [ $? -ne 0 ]; then
EMAIL_BODY+="Failed to mount NFS share. Exiting...\n"
echo "Failed to mount NFS share. Exiting..."
send_email "$EMAIL_SUBJECT" "$EMAIL_BODY"
exit 1
fi
fi
# Schritt 6: Backup auf den NFS-Share kopieren
echo "Copying backup to NFS share..."
cp "$BACKUP_FILE" "$NFS_MOUNT_POINT/"
if [ $? -eq 0 ]; then
EMAIL_BODY+="Backup successfully copied to NFS share: $NFS_MOUNT_POINT/\n"
echo "Backup successfully copied to NFS share: $NFS_MOUNT_POINT/"
# Schritt 7: Lokales Backup löschen
echo "Deleting local backup..."
rm "$BACKUP_FILE"
if [ $? -eq 0 ]; then
EMAIL_BODY+="Local backup file deleted: $BACKUP_FILE\n"
echo "Local backup file deleted: $BACKUP_FILE"
else
EMAIL_BODY+="Failed to delete local backup file.\n"
echo "Failed to delete local backup file."
fi
else
EMAIL_BODY+="Failed to copy backup to NFS share. Local backup will not be deleted.\n"
echo "Failed to copy backup to NFS share."
fi
# Schritt 8: NFS-Share unmounten
echo "Unmounting NFS share..."
umount "$NFS_MOUNT_POINT"
if [ $? -eq 0 ]; then
EMAIL_BODY+="NFS share successfully unmounted: $NFS_MOUNT_POINT\n"
echo "NFS share successfully unmounted: $NFS_MOUNT_POINT"
else
EMAIL_BODY+="Failed to unmount NFS share: $NFS_MOUNT_POINT\n"
echo "Failed to unmount NFS share: $NFS_MOUNT_POINT"
fi
# E-Mail mit Status senden
send_email() {
python3 <<EOF
import smtplib
from email.mime.text import MIMEText
msg = MIMEText("$2")
msg["Subject"] = "$1"
msg["From"] = "$EMAIL_FROM"
msg["To"] = "$EMAIL_TO"
with smtplib.SMTP("$SMTP_SERVER") as server:
server.sendmail("$EMAIL_FROM", "$EMAIL_TO", msg.as_string())
EOF
}
send_email "$EMAIL_SUBJECT" "$EMAIL_BODY"
echo "Backup process completed and status email sent."