Hallo zusammen,
nachdem ich am Wochenende meine native Nexcloud-Instanz erfolgreich in das Betriebsmodell docker umgezogen habe, möchte ich dem Forum gerne mein angepasstest Backup-Script zur Verfügung stellen. Ich verwende ein Nextcloud-Image von Linuxserver inkl. mariadb und redis. Hintergrund dafür ist ein docker-Problem "random-number-generator".
Da ich sowas hier noch nicht gemacht habe, hoffe ich, dass ich diese </> Funktion des Forums richtig verwende.
Bei Interesse kann ich gerne auch eine rudimentäre Anleitung des Umzugs bereitstellen.
cheers,
abrocksi
nachdem ich am Wochenende meine native Nexcloud-Instanz erfolgreich in das Betriebsmodell docker umgezogen habe, möchte ich dem Forum gerne mein angepasstest Backup-Script zur Verfügung stellen. Ich verwende ein Nextcloud-Image von Linuxserver inkl. mariadb und redis. Hintergrund dafür ist ein docker-Problem "random-number-generator".
Da ich sowas hier noch nicht gemacht habe, hoffe ich, dass ich diese </> Funktion des Forums richtig verwende.
Bei Interesse kann ich gerne auch eine rudimentäre Anleitung des Umzugs bereitstellen.
cheers,
abrocksi
Bash:
#!/bin/bash
#
# Bash script for creating backups of Nextcloud.
#
#
# IMPORTANT
# You have to customize this script (directories, users, etc.) for your actual environment.
# All entries which need to be customized are tagged with "TODO".
#
# Variables
currentDate=$(date +"%Y%m%d_%H%M%S")
# TODO: The directory where you store the Nextcloud backups
backupMainDir="/volume1/NextcloudDockerBackups"
# The actual directory of the current backup - this is is subdirectory of the main directory above with a timestamp
backupdir="${backupMainDir}/${currentDate}/"
# TODO: The directory of your Nextcloud installation
nextcloudFileDir="/volume1/docker/nextcloud/app"
# TODO: The directory of your Nextcloud data directory
# If your data directory is located under Nextcloud's file directory, the data directory should not be a separate part of the backup
nextcloudDataDir="/volume1/docker/nextcloud/data"
# TODO: Nextcloud container_name; not the image name
nextcloudContainerApp="Nextcloud-APP"
nextcloudContainerDB="Nextcloud-DB"
# TODO: Nextcloud
MAINTENANCE_COMMAND_ON="occ maintenance:mode --on"
MAINTENANCE_COMMAND_OFF="occ maintenance:mode --off"
# TODO: Your Nextcloud database name
nextcloudDatabase="xxxxxdatabase_namexxxxxx"
# TODO: Your Nextcloud database user
dbUser="xxxxxxxxxx"
# TODO: The password of the Nextcloud database user
dbPassword="xxxxxxxxxxxxxxxxxx"
# TODO: Your web server user; depends on nextcloud image; could be http, abc or www-
webserverUser="xxxx"
# TODO: The maximum number of backups to keep (when set to 0, all backups are kept)
maxNrOfBackups=3
# File names for backup files
# If you prefer other file names, you'll also have to change the NextcloudRestore.sh script.
fileNameBackupFileDir="nextcloud-filedir.tar.gz"
fileNameBackupDataDir="nextcloud-datadir.tar.gz"
fileNameBackupDb="nextcloud-db.sql"
# Function for error messages
errorecho() { cat <<< "$@" 1>&2; }
#
# Check for root
#
if [ "$(id -u)" != "0" ]
then
errorecho "ERROR: This script has to be run as root!"
exit 1
fi
#
# Check if backup dir already exists
#
if [ ! -d "${backupdir}" ]
then
mkdir -p "${backupdir}"
else
errorecho "ERROR: The backup directory ${backupdir} already exists!"
exit 1
fi
#
# Set maintenance mode
#
echo "Set maintenance mode for Nextcloud..."
echo $(date +"%Y%m%d_%H%M%S")
docker exec --env COLUMNS=80 "${nextcloudContainerApp}" sh -c "${MAINTENANCE_COMMAND_ON}"
#sudo -u "${webserverUser}" occ maintenance:mode --on
echo "Done"
echo
sleep 60
#
# Backup file and data directory
#
echo "Creating backup of Nextcloud file directory..."
echo $(date +"%Y%m%d_%H%M%S")
tar -cpzf "${backupdir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir}" .
echo "Done"
echo
echo "Creating backup of Nextcloud data directory..."
echo $(date +"%Y%m%d_%H%M%S")
tar -cpzf "${backupdir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir}" .
echo "Done"
echo
#
# Backup DB
#
echo "Backup Nextcloud database..."
echo $(date +"%Y%m%d_%H%M%S")
docker exec --env COLUMNS=80 "${nextcloudContainerDB}" mysqldump -u xxxxxxx -pxxxxxxxxxxxxxxx xxxxxdatabase_namexxxxxx > "${backupdir}/${fileNameBackupDb}"
echo "Done"
echo
#
# Disable maintenance mode
#
echo "Switching off maintenance mode..."
echo $(date +"%Y%m%d_%H%M%S")
docker exec --env COLUMNS=80 "${nextcloudContainerApp}" sh -c "${MAINTENANCE_COMMAND_OFF}"
echo "Done"
echo
#
# Delete old backups
#
if [ ${maxNrOfBackups} != 0 ]
then
nrOfBackups=$(ls -l ${backupMainDir} | grep -c ^d)
if [ ${maxNrOfBackups} > ${nrOfBackups} ]
then
echo "Removing old backups..."
ls -t ${backupMainDir} | tail -$(( nrOfBackups - maxNrOfBackups )) | while read dirToRemove; do
echo "${dirToRemove}"
rm -r ${backupMainDir}/${dirToRemove}
echo "Done"
echo
done
fi
fi
echo
echo "DONE!"
echo "Backup created: ${backupdir}"
Zuletzt bearbeitet: