1
0
Fork 0

Add Nextcloud Backup to script

This commit is contained in:
Tobias Strobel 2021-09-29 21:09:15 +02:00
parent c05662dcde
commit eba6edff6c
1 changed files with 81 additions and 9 deletions

View File

@ -9,9 +9,6 @@
#
LOGFILE="$(mktemp)"
GOTIFYURL="https://push.strobeto.de/message?token=Anks7VaBnyf7jCB"
HCURL="https://hc.strobeto.de/ping/615c0b9b-077d-46b4-913b-b70d911dab27"
SERVER="$(hostname)"
# ### Konfiguration
@ -22,9 +19,6 @@ HDMINFREE=90
# Welcher Pfad soll gesichert werden?
SRC_PATH=/
# Unter welchem Pfad wird gesichert?
DATA_PATH=/mnt/backupone
setLED () {
# Set LED status
echo heartbeat > "/sys/class/leds/helios64:blue:usb3/trigger"
@ -76,7 +70,7 @@ notify () {
;;
error)
NOTIFY_TITLE+="Backup has ERRORS"
NOTIFY_MESSAGE="Exit code $STATUSCODE. See logs attached to healthchecks for more information."
NOTIFY_MESSAGE="Exit code $2. See logs attached to healthchecks for more information."
NOTIFY_PRIORITY=8
;;
*)
@ -104,6 +98,34 @@ fi
}
basicChecks () {
CONFFILE=~/.config/externalbackup.conf
if ! [ -f $CONFFILE ] ; then
cat >>$CONFFILE <<EOF
# GOTIFYURL="https://push.domain.tld/message?token=MYSECRETTOKEN"
GOTIFYURL=""
# HCURL="https://ping.domain.tld/ping/aaaaaaaa-bbbb-cccc-dddddddddddd"
HCURL=""
EOF
fi
source $CONFFILE
if [ -z $GOTIFYURL ] || [ -z $HCURL ] ; then
echo "Fatal: GOTIFYURL and HCURL must be set in $CONFFILE. Please edit the file and set correct values, then try again."
exit 1
fi
if [ -z "$1" ] ; then
echo "A hdd name to backup to must be given as first parameter! (E.g. backupone, for /mnt/backupone)"
notify custom "NO_HDD_NAME_GIVEN" "FATAL" "No hdd name given to backup to. (E.g. backupone, for /mnt/backupone)"
exit 1
fi
HDDNAME="${1}"
# Unter welchem Pfad wird gesichert?
DATA_PATH="/mnt/${HDDNAME}"
if ! [ -d $DATA_PATH ] ; then
echo "Fatal: Data path does not exist: $DATA_PATH"
healthchecksFinish fail
@ -156,11 +178,61 @@ rotate () {
echo "Finished rotating backups of $SERVER..."
}
backupNC () {
NCDIR="/var/www/nextcloud"
NCOCC="sudo -u www-data php $NCDIR/occ"
NCDATADIR="$($NCOCC config:system:get datadirectory)"
NCDUMPSDIR="$NCDATADIR/sqldumps"
DBPASSWORD="$($NCOCC config:system:get dbpassword)"
DBNAME="$($NCOCC config:system:get dbname)"
DBHOST="$($NCOCC config:system:get dbhost)"
DBUSER="$($NCOCC config:system:get dbuser)"
DELETEDAYS=15
if [ ! -f "$NCDIR/config/config.php" ]; then
echo "WARN: Nextcloud config not found! Skip Nextcloud backup..."
notify custom "NC_CONFIG_NOT_FOUND" "WARN" "Nextcloud config not found! Skip Nextcloud backup..."
return 1
fi
mkdir -p $NCDUMPSDIR
find $NCDUMPSDIR/nextcloud-sqlbkp_* -maxdepth 0 -mmin +$(($DELETEDAYS*60*24)) -exec rm -rvf {} \; > 2&>1
echo "Starting nextcloud backup..."
$NCOCC maintenance:mode --on
echo "Backup Nextcloud DB"
PGPASSWORD=$DBPASSWORD pg_dump $DBNAME -h $DBHOST -U $DBUSER -f "$NCDUMPSDIR/nextcloud-sqlbkp_`date +"%Y-%m-%d-%H-%M-%S"`.bak"
echo "Backup $NCDATADIR"
rsync -aAXHh --stats --numeric-ids --noatime --delete --delete-excluded $NCDATADIR/ $DATA_PATH/"$SERVER"/daily.0$NCDATADIR
STATUSCODE=$?
# Rückgabewert prüfen.
# 0 = fehlerfrei,
# 24 ist harmlos; tritt auf, wenn während der Laufzeit
# von rsync noch (/tmp?) Dateien verändert oder gelöscht wurden.
# Alles andere ist fatal -- siehe man (1) rsync
if [ "$STATUSCODE" -ne 24 ] && [ "$STATUSCODE" -ne 0 ] ; then
echo "Fatal: rsync for Nextcloud finished on $SERVER with errors!"
healthchecksFinish "$STATUSCODE"
notify error "$STATUSCODE"
$NCOCC maintenance:mode --off
exit "$STATUSCODE"
fi
echo "Finished nextcloud backup..."
$NCOCC maintenance:mode --off
}
backup () {
backupNC
echo "Starting rsync backup from $SERVER..."
rsync -aAXHh --stats --numeric-ids --noatime --delete --delete-excluded \
--include={"/mnt/storage","/mnt/storage/nc_data**","/mnt/storage/media**","/mnt/storage/backup**"} \
--include={"/mnt/storage","/mnt/storage/media**","/mnt/storage/backup**"} \
--exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found","/.snapshots"} \
$SRC_PATH $DATA_PATH/"$SERVER"/daily.0
@ -188,10 +260,10 @@ backup () {
exec > >(tee -i "${LOGFILE}")
exec 2>&1
basicChecks ${1}
healthchecksStart
notify start
setLED
basicChecks
backup
rotate
healthchecksFinish