Learn how to back up your Nextcloud installation in order to avoid data loss.
You’ve probably heard by now that Nextcloud has fallen victim to a targeted ransomware named NextCry. This ransomware encrypts user files and offers up this message:
“YOU HAVE BEEN HACKED YOUR FILES HAVE BEEN ENCRYPTED USING A STRONG AES-256 ALGORITHM–SEND 0.025 BTC TO THE FOLLOWING WALLET [wallet cryptographic address] AND AFTER PAY CONTACT [the cyber criminals’ email] TO RECOVER THE KEY NECESSARY TO DECRYPT YOUR FILES”
The ransomware deletes any folders and files that might be used to restore infected files to a clean state and then encrypts the user’s files.
SEE: Hybrid cloud: A guide for IT pros (TechRepublic download)
Because of this, you might want to know how to backup your Nextcloud instance. As of now Nextcloud doesn’t have an official backup solution. According to Nextcloud co-founder (and just generally awesome guy), Jos Poortvliet, “We wanted to develop [a backup solution] for [Nextcloud] 18, but the guy working on it got a lot of stuff on his plate so it won’t be January for sure. But at some point, we really want to do it.”
So a backup solution could come to Nextcloud in the near future. Until then, you need to know how to backup that instance.
What you’ll need
What you’ll need to make this work are:
A running instance of Nextcloud
A user with sudo privileges
A location to house the backed up data
I’m going to assume your instance of Nextcloud is installed in /var/www/html/nextcloud. If yours varies, make that adjustment as needed.
How to put Nextcloud in maintenance mode
The first thing to do is place your instance of Nextcloud in maintenance mode. To do this, log in to the hosting server, and issue the commands:
cd /var/www/html/nextcloud sudo -u www-data php occ maintenance:mode --on
At this point, Nextcloud is now in maintenance mode.
How to back up folders
Next, there are a number of folders and files to be backed up. However, instead of backing them up individually, we’ll back up the entire Nextcloud folder using rsync. This is where you’ll need a secondary location for the data. I’d do this with a few steps:
Create the backup with the following commands:
cd /var/www/html/ sudo rsync -Aavx nextcloud/ /LOCATION/nextcloud-backup_`date +"%Y%m%d"`/
Where LOCATION is the directory to house the Nextcloud installation. Depending on how much data you have in that directory, this could take some time.
Tar the backup folder with the following command:
tar cfz /LOCATION/nextcloud-backup_DATE.tgz /LOCATION/nextcloud-backup_DATE/
Where LOCATION is the location housing the backup and DATE is the date attached to the filename.
How to back up the database
The directories aren’t the only thing that requires backing up. We also must backup our database. I’m going to assume you’re using either MySQL or MariaDB. In order to backup the database, issue the command:
sudo mysqldump --single-transaction -h SERVER -u USER -p nextcloud > nextclouddb-backup_`date +"%Y%m%d"`.bak
Where SERVER is the location of the database–if it’s hosted on the same machine as Nextcloud, it will be localhost–and USER is a user with MySQL admin privileges.
Once you have both your database and your directory backups taken care of, house them in a secure location.
You should probably take regular backups of both your Nextcloud directory and database. So consider creating a cron job for this task (putting both commands together in a script).
How to move your Nextcloud server out of maintenance mode
Now that your backups are taken care of, move your Nextcloud server out of maintenance mode with the commands:
cd /var/www/html/nextcloud sudo -u www-data php occ maintenance:mode --off
Congratulations, your Nextcloud server has been backed up. The next time we address this topic, we’ll restore the backup to a newly installed instance of Nextcloud.