Hi, I have not seen anyone else show how to use mysqldump to generate database backups similar to
https://docs.nextcloud.com/server/latest/admin_manual/maintenance/backup.html
So this is the script I smashed together to make my backups (using User Scripts by Squid) that I run every day
Recap what is does
The script sets nextcloud in maintenance mode first
Then the backup run (note that I use the utf8mb4 setting) | I compress it a bit (this could be better) and add a timestamp to the file.
There is printed a successful or not statement to the terminal/log
Nextcloud is taken out of maintenance
Then I keep the 7 newest files and delete any older ones in my case keep the last 7 days. (this is due to not bloat my remote backup) It produces an error message here but it works.
The dedicated backup user have these privileges “SELECT, SHOW VIEW, TRIGGER, PROCESS, LOCK TABLES” for the db. I can’t remember where I found the overview of what was necessary. You could use the root user, just don’t, that user is too valuable.
To create a dedicated backup user
Open the mariadb terminal
mysql -uroot -p
enter your root password
run the user create command under. You need to change all {{}} content
CREATE USER '{{Dedicated Backup user}}'@'%' IDENTIFIED by '{{password}}'; GRANT SELECT, PROCESS, LOCK TABLES, TRIGGER, SHOW VIEW ON *.* TO '{{Dedicated Backup user}}'@'%';
This user have access to all DBs on your mariadb instance but can't grant privilege, delete or update content
I hope this is helpful and do you have any feedback let me know.
#!/bin/bash
#description=Run to backup db for nextcloud
#foregroundOnly=false
#backgroundOnly=false
#arrayStarted=true
#turn on maintenacemode
docker exec nextcloud occ maintenance:mode --on
#Backup and compression of db
docker exec MariaDB /usr/bin/mysqldump --single-transaction --default-character-set=utf8mb4 --user={{Dedicated Backup user}} --password={{password}} nextcloud | gzip > /mnt/user/{{full path to backup}}/nextcloudDB/nextcloud-sqlbkp_`date +"%Y%m%d"`.gz
if [ "$?" -eq 0 ]
then
echo "Mysqldump executed successfully"
else
echo "Mysqldump encountered a problem"
fi
#turn off maintenacemode
docker exec nextcloud occ maintenance:mode --off
#Keep the 7 newest files, delete the oldest
cd /mnt/user/{{full path to backup}}/nextcloudDB && ls -tp | grep -v '/$' | tail -n +8 | tr '\n' '\0' | xargs -0 rm --
---------------------------------------------------------------------
RESTORE
So my caching drive died, and I needed to recreate my db for nextcloud
This is the steps I used, together with this guide.
https://docs.nextcloud.com/server/latest/admin_manual/maintenance/restore.html
First use the same db creation commands as first time (note that I use the utf8mb4 setting), if you lost your full mariadb install like me you have lost the user accounts as well
write this is in the mariadb terminal
mysql -uroot -p
CREATE DATABASE IF NOT EXISTS nextcloud CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
CREATE USER 'nextcloud' IDENTIFIED by 'password';
GRANT ALL PRIVILEGES ON nextcloud.* to 'nextcloud' IDENTIFIED BY 'password';
Find your latesed db backup-file and decompressed it from main terminal
gzip -d /path/to/file/nextcloud-sqlbkp_20220601.gz
write this command to restore from dump in mariadb terminal (but not logged into MariaDb commandline, Ctrl+c to logout)
make sure that your file is reachable from your mariadb docker container
mysql --user root --password nextcloud < /path/to/file/nextcloud-sqlbkp_20220601
you will get promted for a password. "nextcloud" above is the database name of target db.
It will take som time to restore, and your db is back 😄
Be aware that you need a backup of your config files and data also, I do not understand it good enough to write about it here, but I had backups.