Backup-CloudPanel-Nextcloud/backup.sh

98 lines
2.9 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-09-02 12:53:19 +00:00
# Nextcloud Maintenance
2021-07-12 11:12:00 +00:00
echo "\n###### Aktiviere Wartungsmodus: ${currentDateReadable} ######\n"
sudo -u $user php$phpversion $clpLocation$domain/occ maintenance:mode --on
echo "\n###### Datenbank Backups ######\n"
clpctl db:backup --databases=$databases
echo "\n###### Datenbank Backups erstellt! ######\n"
export BORG_REPO=$backupRepo
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"
2021-09-02 12:53:19 +00:00
# Here the backup is created, adjust this as you would like to have it
2021-07-12 11:12:00 +00:00
borg create \
--stats \
--compression lz4 \
::'{hostname}-{now}' \
/home/cloudpanel/backups \
/home/cloudpanel/htdocs
backup_exit=$?
2021-09-02 12:53:19 +00:00
# Nextcloud Maintenance
2021-07-12 11:12:00 +00:00
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"
2021-09-02 12:53:19 +00:00
# Automatically delete old backups
2021-07-12 11:12:00 +00:00
borg prune \
--prefix '{hostname}-' \
--keep-daily 7 \
--keep-weekly 4 \
--keep-monthly 6
prune_exit=$?
2021-09-02 12:53:19 +00:00
# Information whether the backup worked.
2021-07-12 11:12:00 +00:00
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}