Backup-Skript für die Nextcloud

Die Datensicherung einer Nextcloud lässt sich mit einem kleinen Shell Skript und einem Cron Job automatisieren und auf einem externen Speichermedium ablegen.

Im Backup sollten die beiden Verzeichnise

  • /config
  • /data

und auch die Datenbank enthalten sein.

Die Größe des config-Verzeichnisses ändert sich fast nicht. Deshalb ist die Sicherung dieses Verzeichnisses sehr schnell erledigt.

Das Verzeichnis /data und die Datenbank können über die Dauer sehr groß werden, wodurch die Datensicherung unter Umständen sehr lange dauern kann.

Das Backup eines sehr großen /data-Verzeichnis ist über einen anderen Weg, z.B. mit ➡ rsync oder ➡ borgbackup, sinnvoller und effizienter und kann auch während des laufenden Betriebs durchgeführt werden.

Vor einiger Zeit hatte ich das Problem, dass ein angeschlossener USB-Stick an meinem Raspberry Pi defekt war und der kleine PC nicht mehr startete:
Raspberry Pi startet nicht mehr – A start job is run-in for /dev/disk/by-uuid/xxxxxxx

Aus diesem Grund werden die externen Speichermedien, die ich zur Datensicherung verwende, nicht mehr während des Bootens eingebunden, sonderen nach Bedarf über ein Shell Skript.

Datensicherung

Skript 1 – nextcloudbackup.sh

Das Hauptskript ruft die beiden Skripte zum Ein- und Aushängen des externen Speichermediums auf und führt die Datensicherung durch.

Es werden ausschließlich im Hauptskript nextcloudbackup.sh die Variablen definiert und dann an die beiden Sub-Skripte weitergegeben. Wie das Ganze funktioniert ist in diesem Beitrag beschrieben
Shellskript – Variablen an andere Skripte übergeben

#!/usr/bin/env bash

# Copyright by Stefan Strobel
# https://www.strobelstefan.org

# If you like this script, feel free to buy me a coffee.
# PayPal https://www.paypal.com/donate?hosted_button_id=BNV5XKAAXK6TJ
# Bitcoin - bc1qfuz93hw2fhdvfuxf6mlxlk8zdadvnktppkzqzj


# Creates backups of your Nextcloud instance
# 1. config directory /var/www/html/nextcloud/config
# 2. data directory  /var/www/html/nextcloud/data/
# 3. dump of your MariaDB/MySQL data base

###################################
# Variables
###################################

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

# PWD of script
SCRIPTPWD="/etc/scripts/nextcloudpi/"

# Storage device as defined in your /etc/fstab.
MOUNTPOINT="/mnt/nextcloud/"

# Location of your Nextcloud Installation
# This is the standard path of a Nextcloud installation
NEXTCLOUDINSTALLATION="/var/www/html/nextcloud/"

# E-Mail Address For Log File
EMAIL="mail@e-mail.de"

#Log File location and name
LOGFILE="/var/log/nextcloudbackup.log"
exec 1> ${LOGFILE}

###################################
# Log File
###################################

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

###################################
# MOUNTPOINT Section - Check Mount point Availability
###################################
# Calls the script storage-mount.sh 
# and passes relevant variables

echo "---" >&1
echo "Mount - Section" >&1

/bin/bash ${SCRIPTPWD}storage-mount.sh "${MOUNTPOINT}" "${EMAIL}" "${LOGFILE}" "${MAINSCRIPT}" >&1

if [ $? != 0 ]

	then

		exit 1

fi

echo "---" >&1

###################################
# NEXTCLOUD Backup
###################################

# Nextcloud Maintenancemode -- ON
# No user login is possible!

echo "---" >&1
echo "Nextcloud - Section" >&1

sudo -u www-data php ${NEXTCLOUDINSTALLATION}occ maintenance:mode --on >&1

# Backup config files
sudo tar -cpzf /mnt/nextcloud/Nextcloud_Config_$(date +%Y-%m-%d).tar.gz -C /var/www/html/nextcloud/config . >&1

# Backup /data directory
#sudo tar -cpzf /mnt/nextcloud/Nextcloud_Data_`date +"%Y%m%d%H"`.tar.gz -C /var/www/html/nextcloud/data/ .

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

# Data base dump
mysqldump --default-character-set=utf8mb4 --single-transaction -h localhost -u nextcloud -password nextcloud > /mnt/nextcloud/nextcloud-sqlbkp_$(date +%Y-%m-%d).bak

	echo "$(date +%Y-%m-%d_%H-%M-%S) - End Data Base Dump"  >&1
	echo >&1
	
# Nextcloud Maintenancemode -- OFF
sudo -u www-data php ${NEXTCLOUDINSTALLATION}occ maintenance:mode --off >&1

echo "---" >&1


###################################
# UMOUNT Section - Unmount Storage Device
###################################
# Calls the script storage-umount.sh 
# and passes relevant variables

echo "---" >&1
echo "UMount - Section" >&1

/bin/bash ${SCRIPTPWD}storage-umount.sh "${MOUNTPOINT}" "${EMAIL}" "${LOGFILE}" "${MAINSCRIPT}" >&1

	echo "++++++++++++++++++++++++++" >&1
	echo "" >&1
	echo "" >&1

echo "---" >&1

exit 0


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.de


Kaffee via Bitcoin

bc1qfuz93hw2fhdvfuxf6mlxlk8zdadvnktppkzqzj


Skript 2 – storage-mount.sh

Bindet das externe Speichermedium ein. Die Variablen werden im Skript nextcloudbackup.sh definiert.

#!/usr/bin/env bash

# Copyright by Stefan Strobel
# https://www.strobelstefan.org

# If you like this script, feel free to buy me a coffee.
# PayPal https://www.paypal.com/donate?hosted_button_id=BNV5XKAAXK6TJ
# Bitcoin - bc1qfuz93hw2fhdvfuxf6mlxlk8zdadvnktppkzqzj


SCRIPTNAME=$(basename -- "$0")

###################################
# MOUNTPOINT Section - Check Mount point Availability
###################################
# It checks if your mount point is accessible
# 1. mount ${MOUNTPOINT} as defined in /etc/fstab
# 2. Check if mount point is accessible by server
# 2. If YES go to next Section
# 3.1 If NO, try to mount storage device again as defined in /etc/fstab
# /etc/fstab - UUID=xxxxxxxxx /mnt/nextcloud exfat defaults,noauto,umask=000,users,rw 0 0
# 3.2 If NO, full stop and exit

	echo "" >&1

	echo "Passed Variables to ${SCRIPTNAME}:" >&1
	echo "-----" >&1
	echo "MOUNTPOINT = $1" >&1
	echo "EMAIL = $2" >&1
	echo "LOGFILE = $3" >&1
	echo "MAINSCRIPT $4" >&1

	echo "" >&1

	echo "$(date +%Y-%m-%d_%H-%M-%S) - Mount storage device as configured in /etc/fstab" >&1
	
mount $1

	echo "$(date +%Y-%m-%d_%H-%M-%S) - Sleep 10 Seconds" >&1
	
sleep 10

	echo "$(date +%Y-%m-%d_%H-%M-%S) - Check if mount point is accessible" >&1

if [ "$(findmnt $1)" ] ;
	then
		echo "$(date +%Y-%m-%d_%H-%M-%S) - MOUNTPOINT accessible" >&1
	else
		echo "$(date +%Y-%m-%d_%H-%M-%S) - $4 FAILED! Was not able to mount your storage device!" >&1
		echo "Image Backup FAILED!" | mutt $2 -a $3 -s "$(hostname) - $4 FAILED!" >&1
		exit 1
fi

Skript 3 – storage-umount.sh

Hängt das externe Speicermedium wieder aus. Die Variablen werden im Skript nextcloudbackup.sh definiert.

#!/usr/bin/env bash

# Copyright by Stefan Strobel
# https://www.strobelstefan.org

# If you like this script, feel free to buy me a coffee.
# PayPal https://www.paypal.com/donate?hosted_button_id=BNV5XKAAXK6TJ
# Bitcoin - bc1qfuz93hw2fhdvfuxf6mlxlk8zdadvnktppkzqzj


SCRIPTNAME=$(basename -- "$0")

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

	echo "" >&1

	echo "Passed Variables to ${SCRIPTNAME}:" >&1
	echo "-----" >&1
	echo "${MOUNTPOINT} = $1" >&1
	echo "${EMAIL} = $2" >&1
	echo "${LOGFILE} = $3" >&1

	echo "" >&1

	echo "$(date +%Y-%m-%d_%H-%M-%S) - Umount storage device" >&1

umount $1

sleep 30

if [ "$(findmnt $1)" ] ;
	then
		echo "$(date +%Y-%m-%d_%H-%M-%S) - Force umount of $1" >&1
		umount -l $1
	else
		echo "$(date +%Y-%m-%d_%H-%M-%S) - Device already umounted" >&1
fi

	echo "$(date +%Y-%m-%d_%H-%M-%S) - Finished to umount storage device" >&1

Skripte abspeichern, Rechte vergeben und ausführen

Die Skripte sind im Verzeichnis /etc/scripts/nextcloudpi/ abgelegt.
Als Eigentümer wird der Benutzer „root“ mit den Rechten 0700 (= rwx——) gesetzt.

Ausgeführt wird der Backup-Prozess mit:

sudo sh /etc/scripts/nextcloudpi/nextcloudbackup.sh

Cron anlegen

Das Skript lässt sich mit Hilfe eines Cron Jobs automatisiert zu immer der gleichen Zeit ausführen.

sudo crontab -e
# nextcloudbackup.sh-Skript - Jeden Tag um 5:30Uhr
30 5 * * * /bin/bash /etc/scripts/nextcloudpi/nextcloudbackup.sh

Logrotate

Das Skript schreibt alle Ereignisse in die Datei /var/log/nextcloudbackup.log.
Damit die Datei in regelmäßigen Abständen rotiert und gelöscht wird, sollte ein Logrotate-Profil angelegt werden.

sudo nano /etc/logrotate.d/nextcloudbackup

Der Inhalt der Datei kann z.B. so aussehen:

/var/log/nextcloudbackup.log {
rotate 4
daily
missingok
notifempty
compress
delaycompress
sharedscripts
endscript
}

Weitere Infos zu Logrotate gibt es in de manpages:

man logrotate

Download der Skripte

Download der 3 Skripte in einem zip-Archiv.

Backup-Skript für die Nextcloud

Schritt-für-Schritt zur eigenen Nextcloud

Eine detaillierte Anleitung zur Installation und Konfiguration der eigenen Nextcloud findet ihr hier im Blog:

Weitere Datensicherungesmöglichkeiten für die Nextcloud

Photo by benjamin lehman on Unsplash

Schreibe einen Kommentar

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