Backup-CloudPanel-Nextcloud/backup.sh

99 lines
3 KiB
Bash
Raw Normal View History

2021-07-12 11:12:00 +00:00
#!/bin/sh
#
# @author Mark Schenk
# @copyright 2017-2021 Foxly.de
# @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
# This backup script is customized for CloudPanel. However, with small changes, you can configure it for any purpose. For more information visit: https://foxly.de
#
# ToDo:
#
# Which PHP version do you use e.g. 7.3 , 7.4 , 8.0
phpversion="7.4"
# Just adjust the domain here e.g. cloud.example.org or example.org
domain="cloud.example.org"
# You can back up all databases or specific databases. For all databases enter "all".
databases="db1,db2 or all"
# Here you can assign a password (Borg passphrase) for the Borg backup archive.
backupPassword="p@ssw0rd"
# Here you have to specify the path to the Borg repository.
backupRepo="/path/to/backup"
# No Changes needed
clpLocation="/home/cloudpanel/htdocs/"
# You must edit this user if you are not using the default installation of ClouPanel.
# Otherwise, you can leave this value unchanged.
user="clp"
#
2021-07-15 07:22:40 +00:00
# Check for root
#
if [ "$(id -u)" != "0" ]
then
errorecho "ERROR: This script has to be run as root!"
exit 1
fi
2021-07-12 11:12:00 +00:00
# Nextcloud Maintenance aktivieren
echo "\n###### Aktiviere Wartungsmodus: ${currentDateReadable} ######\n"
sudo -u $user php$phpversion $clpLocation$domain/occ maintenance:mode --on
# Hier koennen auch explezite Datenbanken angegeben werden
echo "\n###### Datenbank Backups ######\n"
clpctl db:backup --databases=$databases
echo "\n###### Datenbank Backups erstellt! ######\n"
export BORG_REPO=$backupRepo
# Euer Passwort muss hier hinterlegt werden.
export BORG_PASSPHRASE=$backupPassword
info() { printf "\n%s %s\n\n" "$( date )" "$*" >&2; }
trap 'echo $( date ) Backup unterbrochen >&2; exit 2' INT TERM
info "Start backup"
# Hier wird das Backup erstellt, passt das so an wie Ihr das gerne haben möchtet
borg create \
--stats \
--compression lz4 \
::'{hostname}-{now}' \
/home/cloudpanel/backups \
/home/cloudpanel/htdocs
backup_exit=$?
# Nextcloud Maintenance deaktivieren
echo "\n###### Deaktiviere Wartungsmodus ######\n"
sudo -u $user php$phpversion $clpLocation$domain/occ maintenance:mode --off
echo "\n###### Ende des Backups: ${endDateReadable} (${durationReadable}) ######\n"
echo "Storage space usage of the backups:\n"
df -h ${backupRepo}
info "Loeschen von alten Backups"
# Automatisches löschen von alten Backups
borg prune \
--prefix '{hostname}-' \
--keep-daily 7 \
--keep-weekly 4 \
--keep-monthly 6
prune_exit=$?
# Informationen ob das Backup geklappt hat.
global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit ))
if [ ${global_exit} -eq 0 ]; then
info "Backup und/oder Prune erfolgreich beendet"
elif [ ${global_exit} -eq 1 ]; then
2021-07-22 07:13:34 +00:00
info "Backup und/oder Prune beendet mit Warungen"
2021-07-12 11:12:00 +00:00
else
info "Backup und/oder Prune beendet mit Fehlern"
fi
exit ${global_exit}