Dropbox is the primary cloud sync provider in my family. All our personal documents, family photographs, code projects etc. live there. Folder common to both my wife and I are shared between our accounts. Everything else remains separate. We also backup the data to external hard drives. My personal MacBook also uses Backblaze. We overdo our backup strategy due to a data loss incident couple of years back.
We also have NextCloud on a Debian system in our home network. It suffices as our media centre. I wanted to create a backup system for the NextCloud installation. I didn’t want to use S3 or anything else at this point as our NextCloud instance is not a huge data hog. So I am using my existing Dropbox instance for this. NextCloud allows Dropbox to be used as an external storage. But I use my local HDD as the storage for NextCloud and then rsync it to a Dropbox folder.
Initial setup for Dropbox
- On the host machine (where NextCloud is installed), install Dropbox. This process will vary based on your operating system.
- Create a folder “NEXTCLOUD-BKP” in Dropbox. Make it a shared folder if required.
- Using the Selective Sync feature remove all other folders except the newly created one. Since the primary purpose of my Debian system is NextCloud, I am only syncing the backup folder for Dropbox and not any other data.
-
This step is specific for my setup.
- My NextCloud instance uses Apache web server and runs as www-data user. But my Dropbox is installed as the main logged in user. In order to enable me to copy the data owned by www-data user, add the logged in user to www-data group.
usermod -a -G www-data <LOGGED_IN_USER>
. - Make the permissions of the “NEXTCLOUD-BKP” folder to g+rwx.
chmod g+rwx ~/Dropbox/NEXTCLOUD-BKP
. - Ensure that the Dropbox and “NEXTCLOUD-BKP” folder has “x” bit set to enable the www-data user to copy to its subfolders. https://askubuntu.com/questions/455000/group-permissions-allow-but-still-get-permission-denied
- My NextCloud instance uses Apache web server and runs as www-data user. But my Dropbox is installed as the main logged in user. In order to enable me to copy the data owned by www-data user, add the logged in user to www-data group.
Enable maintenance mode on NextCloud
This locks the sessions of existing logged in users to prevent inconsistency in the database. It also prevents new logins. php $NEXTCLOUD_PATH/occ maintenance:mode --on
.
Copy the NextCloud data using rsync
You can either copy the whole NextCloud folder or just the data, config and themes folder.
rsync -Aavx $NEXTCLOUD_PATH/data $DROPBOX_NEXTCLOUD_PATH
rsync -Aavx $NEXTCLOUD_PATH/config $DROPBOX_NEXTCLOUD_PATH
rsync -Aavx $NEXTCLOUD_PATH/themes $DROPBOX_NEXTCLOUD_PATH
Backup NextCloud database
My NextCloud database is MariaDB. If you use PostgreSQL or SQLite, then this step will be different for you.
$MYSQLDUMP_PATH --defaults-extra-file=$MYSQL_CREDENTIALS_FILE --single-transaction --databases nextcloud > $DROPBOX_NEXTCLOUD_PATH/database/nc-db-bkp_`date +"%Y-%m-%d-%H-%M"`.bak
The $MYSQL_CREDENTIALS_FILE
provides the username and password for the next cloud database. https://dev.mysql.com/doc/refman/5.5/en/option-files.html nextcloud
is the name of the database.
Update permissions of the backup folders created/updated
This step will depend on your setup and might be completely optional. Since I have different user running nextcloud, I need to do this for Dropbox to read the newly created files.
chmod -R 777 $DROPBOX_NEXTCLOUD_PATH/data
chmod -R 777 $DROPBOX_NEXTCLOUD_PATH/config
chmod -R 777 $DROPBOX_NEXTCLOUD_PATH/themes
chmod -R 777 $DROPBOX_NEXTCLOUD_PATH/database
Since my installation is within my home network and is behind a firewall and opens only the required ports, I feel safe doing the above. YMMV.
Disable maintenance mode on NextCloud
Once all the above steps are done, disable the maintenance mode. php $NEXTCLOUD_PATH/occ maintenance:mode --on
.
Setup a cron
I run the script once a day at 9PM.bash>0 21 * * * nextcloud-backup.sh
. The script must run as the www-data user or the user running the nextcloud process. Here is the complete script.
#!/usr/bin/env bash
# Can set the below three as environment variables or uncomment to set actual paths
NEXTCLOUD_PATH=<NEXTCLOUD_INSTALLATION_PATH>
DROPBOX_NEXTCLOUD_PATH=<PATH_TO_NEXTCLOUD_BACKUP_FOLDER_IN_DROPBOX>
MYSQL_CREDENTIALS_FILE=<MYSQL_CREDENTIALS_FILE_CONF_FORMAT>
PHP_PATH=`which php`
MYSQLDUMP_PATH=`which mysqldump`
# Enable Maintenance mode
$PHP_PATH $NEXTCLOUD_PATH/occ maintenance:mode --on
# Backup data, config and themes from nextcloud directory.
# Can sync the entire nextcloud directory if needed
rsync -Aavx $NEXTCLOUD_PATH/data $DROPBOX_NEXTCLOUD_PATH
rsync -Aavx $NEXTCLOUD_PATH/config $DROPBOX_NEXTCLOUD_PATH
rsync -Aavx $NEXTCLOUD_PATH/themes $DROPBOX_NEXTCLOUD_PATH
# Backup MySQL
$MYSQLDUMP_PATH --defaults-extra-file=$MYSQL_CREDENTIALS_FILE --single-transaction --databases nextcloud > $DROPBOX_NEXTCLOUD_PATH/database/nc-db-bkp_`date +"%Y-%m-%d-%H-%M"`.bak
# Update permissions of the backup path to 777 (Because the user running nextcloud is different.)
chmod -R 777 $DROPBOX_NEXTCLOUD_PATH/data
chmod -R 777 $DROPBOX_NEXTCLOUD_PATH/config
chmod -R 777 $DROPBOX_NEXTCLOUD_PATH/themes
chmod -R 777 $DROPBOX_NEXTCLOUD_PATH/database
# Disable Maintenance mode
$PHP_PATH $NEXTCLOUD_PATH/occ maintenance:mode --off
This provides a good setup for me and my NextCloud data is backed up to Dropbox. Please note that the mysqldump
is not incremental. So you might need to delete older files if required. I keep 5 days backup only as the actual data folder is completely overwritten by rsync
. You can update the rsync
commands by creating an archive and saving instead of actual copy.