Nextcloud – Kalender und Adressbücher sichern mit calcardbackup

Vor kurzem habe ich nach einer Lösung gesucht, wie ich die Kalender und Adressbücher meiner Nextcloud einfach und schnell sichern kann. Ein ganz wichtiger Punkt für mich war, beides muss auch ohne eine Nextcloud wieder hergestellt werden können, z.B. bei meinem E-Mail-Provider.


Bei einer privat gehosteten Nextcloud-Instanz, die auf einem Raspberry Pi läuft finde ich das sehr wichtig. Schließlich gibt es einige Faktoren bei denen der kleine Pi von heute auf morgen den Geist aufgeben kann, z.B. Hardware kaputt, SD-Karte defekt, Überspannung, Blitzschlag, etc.


Ich bin bei meiner Suche auf das Projekt „calcardbackup“ gestoßen, dass genau das macht was ich brauche. Es speichert die Adressbücher im „vcf„-Format und die Kalender im „ics„-Format auf ein definierten Verzeichnis.

Das Skript kann von GitHub heruntergeladen werden
https://github.com/BernieO/calcardbackup

Die Installation ist dann wie in der Dokumentation beschrieben vorzunehmen.


Ich habe mich das Ganze noch ein wenig angepasst, da ich eine Speicherung der Adressbücher- und der Kalenderdateien auf meiner FRITZ!Box haben wollte und nicht irgendwo auf der SD-Karte des Pis einer angeschlossenen Festplatte. Zusätzlich sollte die Datei auch nochmal per E-Mail an meine Adresse versendet werden.

Ich verwende dabei ein paar Code-Schnipsel aus meinem Artikel
Raspberry Pi SD-Karte im Livebetrieb sichern, um das Share auf meiner Fritzbox zu mounten.

ACHTUNG NEUE VERSION DES SKRIPTS GIBT ES ➡ hier

Hier nun mein Skript:

#!/bin/bash

# by strobelstefan.de
# 2019-01-13
# Version: 1.0
# https://strobelstefan.de/?p=6094

#
# This script uses the project calcardbackup (https://github.com/BernieO/calcardbackup) to export calendar and address books 
# from Nextcloud and sends is via e-mail to a defined address 

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

# Storage device as defined in you /etc/fstab.
mountpoint='/mnt/fritzbox'

# Location of your Nextcloud
NEXTCLOUD='/var/www/html/nextcloud'

# Location of your log file
LOGFILE="/var/log/kalenderbackup.log"

# Location Backup Path
IMAGE='/mnt/fritzbox/Nextcloud/calcardbackup'

# Location of the calcardbackup script
CALCARDBACKUP='/etc/scripts/calcardbackup/calcardbackup'

# E-Mail Address where the export should be emailed to
EMAILBACKUP="backup@email.de"

# E-Mail where the log file should be mailed to
EMAIL="logfile@email.de"



###################################
# This removes your old log file
###################################

rm ${LOGFILE}


###################################
# MOUNTPOINT Section - Check Mountpoint Availability
###################################
# It checks if your mountpoint is accessible by your RPi.
# This is a crucial step, if the sotrage is not available the clone process of the SD card cannot conducted.
# Process
# 1. Check if mountpoint is accessible
# 2. If YES go to DELETION Section
# 3.1 If NO, try to mount storage device as defined in /etc/fstab
# 3.2 If mount is again not sucessful exit script, no futher action will be conducted

if [ "$(findmnt ${mountpoint})" ] ;
        then
                echo $(date +%Y-%m-%d_%H-%M-%S) " - Mountpoint accessible by your Raspberry Pi" >> ${LOGFILE}
        else
                echo $(date +%Y-%m-%d_%H-%M-%S) " - Mountpoint was not mounted, 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 sucessfully 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" | mail -a ${LOGFILE} -s "NEXTCLOUD Pi - Backup FAILED" ${EMAIL} < ${LOGFILE}
                exit
                fi
        fi

fi


###################################
# Calls the calcardbackup script
# For more information please refer to
# https://github.com/BernieO/calcardbackup
###################################

sudo -u www-data ${CALCARDBACKUP} "${NEXTCLOUD}" -o ${IMAGE} -i -r 90
echo $(date +%Y-%m-%d_%H-%M-%S) " - Successfully exported calendar and address books to AVM" >> ${LOGFILE}


###################################
# Send Export via E-Mail
###################################
# Will look for the newest file in the defined path ${IMAGE}
# and send the file as an attachement via e-mail to the defined address ${EMAILBACKUP}

newest=$(ls -rt ${IMAGE}*.tar.gz | tail -n1)
echo $(date +%Y-%m-%d_%H-%M-%S) " - Please find attached the actual export of calendars and address books of your Nextcloud instance." | mutt ${EMAILBACKUP} -a $newest -s "NEXTCLOUD Pi - Exported Calendar and Address Book"
echo $(date +%Y-%m-%d_%H-%M-%S) " - Successfully sent export via E-Mail to ${EMAILBACKUP}"  >> ${LOGFILE}


###################################
# UMOUNT Section - Unmount Storage Device
###################################
# This command unmounts the defined storage device
# In the first try it will gently try to unmount, if the device is busy the command will force the unmount.

echo $(date +%Y-%m-%d_%H-%M-%S) " - Started to unmount storage device">> ${LOGFILE}

umount ${mountpoint}

if [ $? != 0 ]
        then
                echo $(date +%Y-%m-%d_%H-%M-%S) " - Device busy, unmount forcefully" >> ${LOGFILE}
        sleep 5
                umount -l ${mountpoint}
        if [ $? != 0 ]
    then
        echo $(date +%Y-%m-%d_%H-%M-%S) " - Issue with umount you storage, check manually" >> ${LOGFILE}
    break
        fi
fi


###################################
# Send Log File
###################################
# This sends the log file to ${EMAIL}

echo $(date +%Y-%m-%d_%H-%M-%S) " - Calendar & Address Book exported and saved" | mail -a ${LOGFILE} -s "NEXTCLOUD Pi - Calendar & Address Book exported and saved" ${EMAIL} < ${LOGFILE}

Das Skript noch ausführbar machen

sudo chmod +x skript.sh

Und dann testen

sudo ./skript.sh

Automatisieren kann man den ganzen Prozess mit einen CRON Job:

sudo crontab -e

Dort die folgenden Zeilen eintragen, damit einmal am Tag um 0:00 Uhr der Export gestartet wird

#Calender & Addressbook Backup
@daily /bin/bash /etc/scripts/skript.sh

Und hier das Skript zu herunterladen:
https://codeberg.org/strobelstefan.org/kalenderbackup


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


Skript-Verbesserungen

Der Entwickler von calcardbackup BernieO hat mir ein paar Verbesserungen für das Skript vorgeschlagen, die ich euch nicht vorenthalten möchte.
Hier das neue SKript mit den Kommentaren

#!/bin/bash

# by strobelstefan.de
# 2019-01-13
# Version: 1.0
# https://strobelstefan.de/?p=6094

#
# This script uses the project calcardbackup (https://github.com/BernieO/calcardbackup) to export calendar and address books 
# from Nextcloud and sends is via e-mail to a defined address 

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

# Storage device as defined in you /etc/fstab.
mountpoint='/mnt/fritzbox'

# Location of your Nextcloud
NEXTCLOUD='/var/www/html/nextcloud'

# Location of your log file
LOGFILE="/var/log/kalenderbackup.log"

# Location Backup Path
IMAGE='/mnt/fritzbox/Nextcloud/calcardbackup'

# Location of the calcardbackup script
CALCARDBACKUP='/etc/scripts/calcardbackup/calcardbackup'

# E-Mail Address where the export should be emailed to
EMAILBACKUP="backup@email.de"

# E-Mail where the log file should be mailed to
EMAIL="logfile@email.de"



###################################
# This removes your old log file
###################################

rm ${LOGFILE}


###################################
# MOUNTPOINT Section - Check Mountpoint Availability
###################################
# It checks if your mountpoint is accessible by your RPi.
# This is a crucial step, if the sotrage is not available the clone process of the SD card cannot conducted.
# Process
# 1. Check if mountpoint is accessible
# 2. If YES go to DELETION Section
# 3.1 If NO, try to mount storage device as defined in /etc/fstab
# 3.2 If mount is again not sucessful exit script, no futher action will be conducted

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

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

# hier schlage ich vor, auf Fehlercode "0" zu prüfen, dann stimmt die Textausgabe in Zeile 69:
        if [ $? == 0 ]  # Original: if [ $? != 0 ]
                then
                        echo $(date +%Y-%m-%d_%H-%M-%S) " - Mount of storage in first try sucessfully completed" >> ${LOGFILE}
# die nächste Zeile ('else') habe ich neu eingefügt. Der else-Block wird nur ausgeführt, wenn 'mount -a' Fehler ausgespuckt hat.
                else
                        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" | mail -a ${LOGFILE} -s "NEXTCLOUD Pi - Backup FAILED" ${EMAIL} < ${LOGFILE}
                            exit
                        fi
        fi

fi


###################################
# Calls the calcardbackup script
# For more information please refer to
# https://github.com/BernieO/calcardbackup
###################################

# diese Zeile:
# sudo -u www-data ${CALCARDBACKUP} "${NEXTCLOUD}" -o ${IMAGE} -i -r 90
# ersetzen durch (hierbei wird der Variablen 'newest' der Pfad zur Backup-Datei zugewiesen (batch-mode von calcardbackup))
# nicht irritieren lassen durch die vielen Gänsefüßchen - die Variablenzuweisung stimmt so (command substitution!):
newest="$(sudo -u www-data "${CALCARDBACKUP}" "${NEXTCLOUD}" -o "${IMAGE}" -i -r 90 -b)"
echo $(date +%Y-%m-%d_%H-%M-%S) " - Successfully exported calendar and address books to AVM" >> ${LOGFILE}


###################################
# Send Export via E-Mail
###################################
# Will look for the newest file in the defined path ${IMAGE}
# and send the file as an attachement via e-mail to the defined address ${EMAILBACKUP}

# folgende Zeile kann wegfallen, da ${newest} schon den Pfad zur Backup-Datei enthält (siehe Zeile 94):
# newest=$(ls -rt ${IMAGE}*.tar.gz | tail -n1)
echo $(date +%Y-%m-%d_%H-%M-%S) " - Please find attached the actual export of calendars and address books of your Nextcloud instance." | mutt ${EMAILBACKUP} -a $newest -s "NEXTCLOUD Pi - Exported Calendar and Address Book"
echo $(date +%Y-%m-%d_%H-%M-%S) " - Successfully sent export via E-Mail to ${EMAILBACKUP}"  >> ${LOGFILE}


###################################
# UMOUNT Section - Unmount Storage Device
###################################
# This command unmounts the defined storage device
# In the first try it will gently try to unmount, if the device is busy the command will force the unmount.

echo $(date +%Y-%m-%d_%H-%M-%S) " - Started to unmount storage device">> ${LOGFILE}

umount ${mountpoint}

if [ $? != 0 ]
        then
                echo $(date +%Y-%m-%d_%H-%M-%S) " - Device busy, unmount forcefully" >> ${LOGFILE}
        sleep 5
                umount -l ${mountpoint}
        if [ $? != 0 ]
    then
        echo $(date +%Y-%m-%d_%H-%M-%S) " - Issue with umount you storage, check manually" >> ${LOGFILE}
# die folgende Zeile kann wegfallen, da "break" bei "if" keinerlei Funktion hat.
#    break
        fi
fi


###################################
# Send Log File
###################################
# This sends the log file to ${EMAIL}

echo $(date +%Y-%m-%d_%H-%M-%S) " - Calendar & Address Book exported and saved" | mail -a ${LOGFILE} -s "NEXTCLOUD Pi - Calendar & Address Book exported and saved" ${EMAIL} < ${LOGFILE}

6 Antworten auf „Nextcloud – Kalender und Adressbücher sichern mit calcardbackup“

  1. Hallo Stefan,

    wie schön, dass dir mein Skript gefällt und du einen Blogbeitrag über calcardbackup geschrieben hast. Das finde ich super – Danke!

    Mir sind bei deinem Skript ein paar Kleinigkeiten aufgefallen:
    in Zeile 98 benutzt du `find`, um die neu erstellte Backupdatei zu finden.
    Ich schlage eine Vereinfachung vor:
    da du in Zeile 88 die Ausgabe von calcardbackup sowieso nicht in dein Logfile pipest, könntest du calcardbackup auch mit der option -b aufrufen.  Dann wird nur der Pfad zur erstellten Backupdatei ausgegeben. Diesen Namen kannst du gleich der entsprechenden Variablen zuweisen und dir Zeile 98 mit dem `find` dann komplett sparen:
    newest=“$(sudo -u www-data „${CALCARDBACKUP}“ „${NEXTCLOUD}“ -o „${IMAGE}“ -i -r 90 -b)“

    Außerdem scheint mir der auszugebende Text in Zeile 68 falsch zu sein. Ich würde daher in Zeile 66 auf `$? == 0` und vor Zeile 69 (sleep 5) eine neue Zeile `else` einfügen. Dann stimmt der Text 🙂

    Das `break` in Zeile 121 macht keinen Sinn. `if` ist keine Schleife aus der man mit `break` aussteigen könnte. Die Zeile könntest du löschen, da sie eh keine Auswirkungen hat.Ansonsten TOP! Ich finde es eine super Idee, das Backup auf der Fritzbox zu sichern. So landet das Backup auf einem anderen Gerät, falls sich Festplatte oder SD-Card ins Nirwana verabschieden sollten.

    Viele Grüße

    BernieO

  2. Hallo Stefan,Vielen Dank erstmal für das Script! Funktioniert soweit auch.Wenn ich das Script händisch ausführe bekomme ich eine Mail mit Export und Log-file. Sobald ich allerdings einen cronjob anlege bekomme ich nur noch die Log-file. Hast du eine Idee, an was das legen kann?Gruß Robin

  3. Hallo Stefan,ich bin beim Thema Backup für Kalender und Adressbuch meiner privat gehosteten Nextcloud (Ubuntu 20.04 unter Hyper-V) auf deine Seite gestoßen.Das Script von BernieO hab ich erfolgreich installieren können und die Backups werden auch erstellt (auch täglich per Crontab).Da ich persönlich garkeine Affinität zu Scripten/Coden habe, wollte ich folgendes fragen:Wie kriege ich nur den E-Mail-Versand der tar.gz Datei hin?Alles weitere aus deinem Script würde ich nicht benötigen.Vllt kannst du mir da weiterhelfen bitte?MfGFabian

    1. Hallo Fabian,

      das Skript macht genau das was du möchtest. Die Exporte werden als tar.gz-Datei versendet.
      Du musst lediglich deine E-Mail-Adresse eintragen.

      Grüße

      Stefan

Schreibe einen Kommentar

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