Die Datensicherung einer Nextcloud lässt sich mit einem kleinen Shell Skript und einem Cron Job automatisieren und auf einem externen Speichermedium ablegen.
Imagebackup-Skript – Optimiert
Das Skript imagebackup.sh
ermöglicht es dir automatisiert und in regelmäßigen Abständen ein komplettes Image der SD-Karte deines Raspberry Pis als Backup auf ein externes Speichermedium zu schreiben. Ich nutze das für meine beiden Pis auf denen eine Nextcloud und Pi-hole läuft.
macOS – Datensicherung mit rsync
Die Datensicherung auf einem Mac lässt sich mit rsync und einem kleinen Bash Skript sehr gut automatisieren.
Datensicherung mit rsync
Das folgende Skript verwende ich, um eine Datensicherung von meinem Linux Client auf ein openmediavault mit rsync durchzuführen.
imagebackup nun auf codeberg.org
Nachdem ich vor kurzem das Skript zur automatischen auf Codeberg für die bessere Verwaltung geladen habe, habe ich mich auch dazu entschieden das Gleiche für das imagebackup-Skript zu machen.
Synology-Backup per rsync automatisch zu openmediavault sichern
Die Daten und die Einstellungen einzelner Pakete eines Synology NAS lassen sich mit rsync regelmäßig und automatisch zu einem openmediavault sichern.
Das Synology fungiert dabei als Client und sendet die Daten an den rsync-Server, der auf dem openmediavault läuft. Die Authentifizierung erfolgt über Benutzernamen und Passwort und kann durch das Synology auch verschlüsselt werden.
Nextcloud – Datensicherung von Kalendern und Adressbüchern automatisch erstellen
Nextcloud bietet eine sehr gute Kalenderfunktion an, die umfangreiche Möglichkeiten für die Benutzer bietet.
Es kann eine beliebige Anzahl von Kalendern anleget und mit anderen Nextcloud-Benutzern geteilt werden.
Die Synchronisation der Kalender ist auf jedes Endgerät, Windows, Linux, Android, MacOS, iOS, etc. dank der Nutzung von CalDAV und CardDAV problemlos möglich.
In diesem Beitrag geht es darum, die Adressbücher und die Kalender der Benutzer mit Hilfe eines Cron Jobs zu sicheren.
Die Adressbücher werden dabei im vcf- und die Kalender im ics-Format exportiert.
Datensicherung für die Nextcloud mit rsync
In diesem Beitrage zeige ich auf, wie man seine Nextcloud-Daten mit Hilfe eines Skripts und rsync auf ein openmediavault sichern kann.
Datensicherung für die Nextcloud mit BorgBackup
Bevor es los geht, nein der Name Borg hat nichts mit den Borg aus dem Star Trek Universum zu tun. Der Name leitet sich vom Entwickler Jonas Borgström ab.
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.
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}