Skip to content

Instantly share code, notes, and snippets.

@mrflobow
Last active April 19, 2024 13:35
Show Gist options
  • Save mrflobow/d891ae0cbe7485aff5f20b1596dc4308 to your computer and use it in GitHub Desktop.
Save mrflobow/d891ae0cbe7485aff5f20b1596dc4308 to your computer and use it in GitHub Desktop.
Unraid Nextcloud Backup Script
#!/bin/bash
#
######### UNRAID Nextcloud Backup Script #########
#
#Created for Nextcloud Linuxserver.io Container
#
#Update the params to meet your requirements
#
##################################################
#### USER CONFIG ####
DB_HOST=XXXXXX
DB_USER=XXXXXX
DB_PASS=XXXXXX
DB_NAME=XXXXXX
NEXTCLOUD_DATA=/mnt/user/nextcloud_data/
NEXTCLOUD_CONTAINER=/mnt/user/appdata/nextcloud/
BACKUP_BASE=/mnt/user/backup/nextcloud
EXCLUDE_FROM_DATA_BACKUP="--exclude=appdata*/preview"
#Cleanup Settings
MAX_BACKUP_COUNT=5
##### SCRIPT VARS #####
TIMESTAMP=`date +"%Y%m%d-%H%M%S"`
BACKUP_PATH=$BACKUP_BASE/$TIMESTAMP
DB_DUMP_PATH=$BACKUP_PATH/database/
#### SCRIPT START #####
trap "docker exec nextcloud occ maintenance:mode --off" EXIT
docker exec nextcloud occ maintenance:mode --on
mkdir $BACKUP_PATH
mkdir $DB_DUMP_PATH
mkdir $BACKUP_PATH/config
mkdir $BACKUP_PATH/themes
##### Extended Unicode Check ############################
# Check if in config.php the following line is present
# 'mysql.utf8mb4' => true,
# Check if table collation is utf8mb4_bin
##########################################################
#If extended unicode support is not enabled use this line
#docker exec mariadb mysqldump --single-transaction -h $DB_HOST -u $DB_USER -p$DB_PASS $DB_NAME > $DB_DUMP_PATH/mariadb_nextcloud.sql
# MySQL Dump with extended Unicode Support enabled
docker exec mariadb mysqldump --single-transaction --default-character-set=utf8mb4 -h $DB_HOST -u $DB_USER -p$DB_PASS $DB_NAME > $DB_DUMP_PATH/mariadb_nextcloud.sql
rsync -Aavx $EXCLUDE_FROM_DATA_BACKUP $NEXTCLOUD_DATA $BACKUP_PATH/data/
rsync -Aavx $NEXTCLOUD_CONTAINER/www/nextcloud/config/ $BACKUP_PATH/config/
rsync -Aavx $NEXTCLOUD_CONTAINER/www/nextcloud/themes/ $BACKUP_PATH/themes/
docker exec nextcloud occ maintenance:mode --off
###### Cleanup ########
BACKUP_COUNT=`ls $BACKUP_BASE | wc -l`
if [ "$BACKUP_COUNT" -gt "$MAX_BACKUP_COUNT" ]
then
BACKUP_DIFF_COUNT=$(($BACKUP_COUNT-$MAX_BACKUP_COUNT))
RM_FOLDERS=`ls -t $BACKUP_BASE | tail -$BACKUP_DIFF_COUNT`
for f in ${RM_FOLDERS[@]}; do
rm -rf "$BACKUP_BASE/$f"
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment