Datensicherung von Kalendern und Adressbüchern von Nextcloud https://strobelstefan.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

223 lines
6.8 KiB

#!/bin/bash
# by strobelstefan.de
# 2021-05-27
# Version: 3.0
# Nextcloud - Datensicherung von Kalendern und Adressbücher automatisch erstellen
# https://strobelstefan.org/2020/12/01/nextcloud-datensicherung-von-kalendern-und-adressbuechern-automatisch-erstellen/
#
# This script uses the project calcardbackup (https://codeberg.org/BernieO/calcardbackup) to export calendar and address books
# from Nextcloud and sends it via e-mail to a defined e-mail address
###################################
# Define Variables
###################################
# Storage device as defined in you /etc/fstab
# Thumb drive attached to the Raspberry Pi
mountpoint='/mnt/usbbackup'
# Location of your Nextcloud
NEXTCLOUD='/var/www/html/nextcloud'
# Location of your log file
LOGFILE="/var/log/calendarbackup.log"
# Location backup path
# Location where the backup copies of calendars and address books are located
IMAGE='/mnt/usbbackup/nextcloud/calendar/'
# Location of the calcardbackup script
CALCARDBACKUP='/etc/scripts/calcardbackup/calcardbackup'
# Folder of the calcardbackup script
CALCARDBACKUPFOLDER='/etc/scripts/calcardbackup/'
# E-mail address where the export should be sent to
EMAILBACKUP="e-mail@mail.de"
# E-mail where the log file should be sent to
EMAIL="e-mail@mail.de"
# Location of the backup script
BACKUPSCRIPT="/etc/scripts/calendarbackup.sh"
###################################
# This removes your old log file
###################################
rm -f ${LOGFILE}
echo >> ${LOGFILE}
echo "++++++++++++++++++++++++++" >> ${LOGFILE}
echo $(date +%Y-%m-%d_%H-%M-%S) " - Deleted Old Log File ${LOGFILE}" >> ${LOGFILE}
sudo touch ${LOGFILE}
echo $(date +%Y-%m-%d_%H-%M-%S) " - Created New Log File ${LOGFILE}" >> ${LOGFILE}
echo "++++++++++++++++++++++++++" >> ${LOGFILE}
echo >> ${LOGFILE}
###################################
# MOUNTPOINT Section - Check Mountpoint Availability
###################################
# It checks if your mountpoint is accessible by your RPi.
# This is a crucial step, if the storage 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 successful exit script, no further action will be conducted
echo >> ${LOGFILE}
echo "++++++++++++++++++++++++++" >> ${LOGFILE}
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 as defined in /etc/fstab
mount -a
if [ $? == 0 ]
then
echo $(date +%Y-%m-%d_%H-%M-%S) " - Mount of storage in first try successfully completed" >> ${LOGFILE}
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 -s "NEXTCLOUD Pi - Backup FAILED" ${EMAIL} >> ${LOGFILE}
exit
fi
fi
fi
echo "++++++++++++++++++++++++++" >> ${LOGFILE}
echo >> ${LOGFILE}
###################################
# Calls the calcardbackup script
# For more information please refer to
# https://codeberg.org/BernieO/calcardbackup
###################################
echo >> ${LOGFILE}
echo "++++++++++++++++++++++++++" >> ${LOGFILE}
echo $(date +%Y-%m-%d_%H-%M-%S) " - Start calcardbackup" >> ${LOGFILE}
sudo -u www-data ${CALCARDBACKUP} "${NEXTCLOUD}" -o ${IMAGE} -i -r 90 >> ${LOGFILE}
echo $(date +%Y-%m-%d_%H-%M-%S) " - End calcardbackup" >> ${LOGFILE}
echo "++++++++++++++++++++++++++" >> ${LOGFILE}
echo >> ${LOGFILE}
###################################
# Update of calcardbackup
###################################
# This sends the log file to ${EMAIL}
echo >> ${LOGFILE}
echo "++++++++++++++++++++++++++" >> ${LOGFILE}
echo $(date +%Y-%m-%d_%H-%M-%S) " - Start Update for calcardbackup" >> ${LOGFILE}
cd ${CALCARDBACKUPFOLDER}
sudo -u www-data git pull >> ${LOGFILE}
echo $(date +%Y-%m-%d_%H-%M-%S) " - End Update for calcardbackup" >> ${LOGFILE}
echo "++++++++++++++++++++++++++" >> ${LOGFILE}
echo >> ${LOGFILE}
###################################
# Send Address Book And Calendar via E-Mail
###################################
# 1. look for the newest export of your address book and calendar in the defined path ${IMAGE}
# 2. attach the {BACKUPSCRIPT}
# 3. attach the {LOGFILE}
# 4. send the files via e-mail to the defined address ${EMAILBACKUP}
echo >> ${LOGFILE}
echo "++++++++++++++++++++++++++" >> ${LOGFILE}
echo $(date +%Y-%m-%d_%H-%M-%S) " - Sent export via E-Mail to ${EMAILBACKUP}" >> ${LOGFILE}
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 ${BACKUPSCRIPT} ${LOGFILE} -s "NEXTCLOUD Pi - Kalender & Adressbuch exportiert"
###################################
###################################
# The following sections are no longer in use
###################################
###################################
###################################
# UMOUNT Section - Unmount Storage Device
###################################
# This command unmounts {mountpoint}
# In the first try it will gently try to unmount, if the device is busy the command will force the unmount of {mountpoint}.
# echo "++++++++++++++++++++++++++" >> ${LOGFILE}
# 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}
# fi
#fi
# echo "++++++++++++++++++++++++++" >> ${LOGFILE}
# echo >> ${LOGFILE}
###################################
# Send Backup Script via E-Mail
###################################
# This sends the log file to ${EMAIL}
# echo >> ${LOGFILE}
# echo "++++++++++++++++++++++++++" >> ${LOGFILE}
#echo $(date +%Y-%m-%d_%H-%M-%S) " - Kalender & Adressbuch Backupskript sent to ${EMAIL}" | mutt ${EMAIL} -a ${BACKUPSCRIPT} ${LOGFILE} -s "NEXTCLOUD Pi - Kalender & Adressbuch Backupskript"#
# echo "++++++++++++++++++++++++++" >> ${LOGFILE}