#!/bin/sh |
# NextCloud to BackBlaze B2 Backup Script |
# Author: Autoize (autoize.com) |
# This script creates an incremental backup of your NextCloud instance at BackBlaze's off-site location. |
# BackBlaze B2 is an object storage service that is much less expensive than using Amazon S3 for the same purpose, with similar versioning and lifecycle management features. |
# Uploads are free, and storage costs only $0.005/GB/month compared to S3's $0.022/GB/month. |
# Requirements |
# - BackBlaze B2 account (10 GB Free) - Create one at https://www.backblaze.com/b2/sign-up.html |
# - Python 3.x and Python PIP - sudo apt-get install python3 && wget https://bootstrap.pypa.io/get-pip.py && sudo python3 get-pip.py |
# - BackBlaze B2 CLI installed from PyPI - sudo pip install b2 |
# Instructions |
# 1. Insert the following line in your NextCloud config.php file above the ); to move the cache above each user's data directory. |
# If /media/external/CloudDATA is not your data directory, substitute the relevant directory before /cache. |
# 'cache_path' => '/media/external/CloudDATA/cache', |
# 2. Create a bucket and obtain your Account ID and Application Key from your B2 account. |
# 3. Authenticate your CLI using the b2 authorize_account command. |
# 4. Save this script to a safe directory such as /srv/backupToB2.sh and make it executable with the following command. |
# sudo chmod +x backupToB2.sh |
# 5. This script must be run as root. To run a backup now: |
# sudo ./backupToB2.sh |
# 6. Set up a cron job to run this backup on a predefined schedule (optional). |
# sudo crontab -u root -e |
# Add the following line to the crontab to conduct a weekly backup every Saturday at 2:00am. |
# 0 2 * * sat root sh /srv/backupToB2.sh > /srv/backupToB2.log |
# Save, quit and check that the crontab has been installed using the following command. |
# sudo crontab -u root -l |
# Name of BackBlaze B2 Bucket |
b2_bucket='b2_bucket_name' |
# Path to NextCloud installation |
nextcloud_dir='/var/www/nextcloud' |
# Path to NextCloud data directory |
data_dir='/media/external/CloudDATA' |
# MySQL/MariaDB Database credentials |
db_host='localhost' |
db_user='nextclouduser' |
db_pass='secret' |
db_name='nextcloud' |
# Check if running as root |
if [ '$(id -u)'!='0' ];then |
echo'This script must be run as root'1>&2 |
exit 1 |
fi |
echo'Started' |
date +'%a %b %e %H:%M:%S %Z %Y' |
# Put NextCloud into maintenance mode. |
# This ensures consistency between the database and data directory. |
sudo -u www-data php $nextcloud_dir/occ maintenance:mode --on |
# Dump database and backup to B2 |
mysqldump --single-transaction -h$db_host -u$db_user -p$db_pass$db_name> nextcloud.sql |
b2 upload_file $b2_bucket nextcloud.sql NextCloudDB/nextcloud.sql |
rm nextcloud.sql |
# Sync data to B2, then disable maintenance mode |
# NextCloud will be unavailable during the sync. This will take a while if you added much data since your last backup. |
# If /media/external/CloudDATA is not your data directory, modify the --excludeRegex flag accordingly, which excludes the NC cache from getting synced to B2. |
b2 sync --excludeRegex '/media/external/CloudDATA/cache/.*'$data_dir b2://$b2_bucket$data_dir |
sudo -u www-data php $nextcloud_dir/occ maintenance:mode --off |
date +'%a %b %e %H:%M:%S %Z %Y' |
echo'Finished' |
Nextcloud Hub is the first completely integrated on-premises content collaboration platform on the market, ready for a new generation of users who expect seamless online collaboration capabilities out of the box. .Status: Good - Algorithmus 7 and DNSKEY with KeyTag 17883 used to validate the DNSKEY RRSet. Status: Good - Algorithmus 7 and DNSKEY with KeyTag 27353 used to validate the DNSKEY RRSet. Status: Valid Chain of trust. Parent-DS with Algorithm 7, KeyTag 17883, DigestType 1 and Digest 'OMXPk7Npx1V+BRX6qlcGDxv7EsE=' validates local Key with the same values, Key ist Secure Entry. The Web Installer is the easiest way to install Nextcloud on a web space. It checks the dependencies, downloads Nextcloud from the official server, unpacks it with the right permissions and the right user account. Nextcloud Talk goes further than other encrypted communication technologies by keeping even metadata from leaking. This ensures you stay in complete control of communications. When SCM was looking for a simple, secure and well designed messenger platform that could be hosted on-premises, a decision was made for Nextcloud Talk.
I recently wrote about how I nearly lost all my data, then later I wrote about how I recovered from that by building a new server. The new server has now been in place for a couple of months, so I thought I would give you guys an update.
Not Plain Sailing
I knew that building the new server would be difficult; things were likely to change and it was all going to be a case of trial and error.
If you look at the building my home server post, you will see that I decided to go with Syncthing for file sync, Plex for media, UbuntuMATE as the OS and Cloudberry for the cloud backups.
After a week or so of use, I noticed that I was having stability issues where my server would completely lock up and stop responding. After some investigation, it was clear that the issue was Cloudberry. I removed Cloudberry and replaced it with Duplicati – no crashes since.
Pro tip: don’t waste your money on a Cloudberry license. Install Duplicati instead and donate the license fee to them.
Other changes
Once the system was stable (or so I thought) I happily went about my business assuming all was well in the world. That was until I booted up my laptop one day to find that Syncthing had deleted the entire contents of my Documents folder.
I assumed it was something I had done by accident. At the time I was moving a lot of data around, as I was working through the restoration of various backups. Maybe I had deleted the wrong folder and it had synced? Luckily I had file versioning turned on, so restoring the data was just a couple of clicks.
Weirdly, when I restored the the data, it did so to the root of my home folder, not /Documents. Again, a minor frustration that I could easily work around.
Fast-forward another few days and the same thing happened again, but this time it was my Photos folder. Then again a few days later to my Documents folder again.
I had no idea what was happening here and there was no details as to what was happening in the Syncthing logs. Enough was enough, I couldn’t trust Syncthing so I decided to move to something else.
Nextcloud
I had used Nextcloud in the past, but had a number of issues with it, so I went into this with a healthy amount of trepidation.
First time around I installed Nextcloud from the snap package. This was trivial to setup, but the sandboxing was causing issues when it came to backing up. So I ended up installing Nextcloud manually (guide coming soon).
I’m happy to report that since I last used Nextcloud, the syncing functionality has improved a lot. I haven’t had any issues with file syncing at all. However, that’s all I’m using it for, so I can’t comment on how the other Nextcloud apps perform.
I installed the Nextcloud app on my iPhone and all my photos are backing up perfectly. I just need to remember to open the Nextcloud app once in a while, as it doesn’t seem to check for new files in the background. That’s not a big deal though.
The Result
I’ve since bought a cheap Dell monitor from Amazon, just so I didn’t have to keep the old TV connected. I’ve also been successfully backing up my data to both a local USB and Backblaze B2 every night.
Duplicati is configured to send me backup reports via email (guide coming soon on that too), so I know if anything has failed. I’ve also done test restores from both USB and Backblaze; both of which were successful.
You may have noticed that I haven’t mentioned Plex during this entire post. That’s because it has been faultless; you just set it and forget it. Yes, it’s proprietary software, but it works really well.
I’ve now been running Plex, Nextcloud, Duplicati and Backblaze B2 together for around 6 weeks without issue.
My files are synced everywhere I need them. The photos I take on my phone are automatically synced to the server. I can stream movies and music with ease on all my devices and everything is backed up to multiple locations.
The Cost
Building my new server has been a lot of work, but it really has been worth it. I now have a server that should last me for years to come and as a bonus, it has cost me a fraction of what my Synology did.
The final costs in both time and money look something like this:
Nextcloud B2b
- Build time (inc. issues & research) – approx. 24 hours
- Hardware (case, CPU, PSU, RAM, SSD) – £290
- Dell monitor – £20
- Monthly B2 charges (approx 20GB) – £0.08
If you’re thinking about running your own server at home, I’d strongly recommend it. However, be prepared for lots of research and troubleshooting in the initial build stages.
Nextcloud B2 Pro
Are you running your own server at home? I’d love to hear about your setup in the comments below.
