I don’t know if others need that too, but it seems just naturally to post this tiny little piece of bash which is our Nextcloud backup script. And I am perfectly aware that this is SME style, not big iron. 🙂 And if you don’t like Databases or PHP, have a look at Owncloud Infinite Scale.
These two products have been targeting more and more diverging directions since their fork: Owncloud adresses large scale, scalable datacenter customers (like CERN) and promises 10 times faster speed than their own PHP solution, but Nextcloud is carving out market share from MS365 customers with gazillions of apps in their app store and a large PHP community – with all benefits and downsides. Raise your hand if you never had to deactivate some community app to make an update work! 🙂
Important: your MariaDB-Passwort will be in this file, I didn’t spend much time into investigating workarounds, but a chmod 700 of this file (read-write-execute) for root only seems appropriate. The DB is not listening on anything else but localhost, thus I am ready to accept this. If there’s a simple solution I missed, answer on Twitter or drop me a PM/Mail/Matrix message.
The Backup Script
As you can see I am backing up to a separate volume. I have file servers in several locations that fetch their backups (the tar files) from there. I don’t want the root fs run full, thus I don’t do a backup if /backup is not mounted. Alerting is done elsewhere. 🙂 On other systems, I mount the backup drive during such a script’s run. If you’re (like me) a seasoned but unexperienced Bash starter, add some “sleep 30” between the lines to see what’s happening and to easily break the script’s run. And you may or may not want the option –delete for rsync – your mileage may vary – and sorry for wordpress changing dashes and hyphens (Remember “Non-hyphenated is an example of a hyphenated word”).
# Backing up Nextcloud Server
# Database Root password is in here, so chmod 700 this file!
if grep -qs ‘/backup ‘ /proc/mounts; then
echo“Backup disk is mounted. Proceeding with backup from $DIR/.“
echo“Entering nextcloud directory“
echo“Putting NC into maintenance mode“
sudo -u www-data php occ maintenance:mode –on
echo“Backing up folders and config with Rsync:“
# sleep 30
/usr/bin/nice -18 /usr/bin/rsync -Aavz –delete $DIR/ $BACKUPDIR/
echo“Backing up MySQL data base ‘nextcloud’ :“
mysqldump –single-transaction -h localhost -u root -p$DBPASS nextcloud > $BACKUPDIR/nextcloud-sqlbkp_`date +“%Y%m%d“`.bak
sudo -u www-data php occ maintenance:mode –off
/usr/bin/nice -18 /bin/tar -czf $BACKUPTAR $BACKUPDIR
echo“Sorry, Backup disk is not mounted, quitting. CU again tomorrow.“
I have two aliases on my NC servers:
alias ncd=’cd /var/www/nextcloud’
alias occf=’sudo -u www-data php ./occ’
With these I don’t need to bother about different document root directories (Debian/Ubuntu/Suse) nor remember the sudo… syntax (yes, I’m old) :-).
That proves helpful to do a lot of occ commands, like cleaning up the hard way:
occf trashbin:cleanup –all-users
occf versions:cleanup –all-users
clean up all trashbins and remove all old versions of files (yes ALL!).
If you don’t want Nextcloud to slowly fill up your hard drive / storage, add this to the config.php file:
‘trashbin_retention_obligation’ => ‘auto’,
‘versions_retention_obligation’ => 60, ‘auto’,
These two options will make Nextcloud store only files that are younger than 30 in the trashbin and versions of files no longer than 60 days.