Nextcloud backup is a crucial step in ensuring the integrity and availability of your data. A single hard drive failure or server crash can result in data loss, so it's essential to have a reliable backup solution in place.
There are several nextcloud backup solutions available, including built-in backup tools like Nextcloud Backup and Nextcloud Restore. These tools allow for automated backups and easy restores.
Regular backups are key to minimizing data loss. According to Nextcloud's documentation, it's recommended to perform daily backups to ensure data is up-to-date.
Requirements
To effectively backup your Nextcloud, you'll need a few specific pieces of hardware and software.
You'll need an Ubuntu-based server running the Nextcloud Snap.
A USB hard drive is also necessary, and it should be at least double the size of the data you're backing up. I recommend getting the biggest one you can afford.
You'll also need Duplicati backup software installed on your Nextcloud server.
Additionally, you'll need a Backblaze B2 account.
Setting up the entire process will take around 30-60 minutes.
Here are the specific software requirements:
- tar
- pigz (https://zlib.net/pigz/) when using backup compression. It can be installed with apt install pigz (Debian/Ubuntu). If not available, you can use another compression algorithm (e.g. gzip)
Setup and Configuration
To set up Nextcloud Backup, you'll need to install and configure the Backup App. This can be done by heading to the Apps section of your Nextcloud as an "admin" user and downloading and enabling the Backup app.
The configuration file NextcloudBackupRestore.conf must be located in the same directory as the scripts for backup/restore. This is a crucial step to ensure the configuration is found.
Some optional options are not configured using setup.sh, but are set to default values in NextcloudBackupRestore.conf. These are marked as 'OPTIONAL' in the configuration file.
User Setup
To set up your backup routine securely, it's recommended to use a dedicated user account. This will keep the backup process separate from your normal user account.
Creating a dedicated user account for backups is a good practice to maintain security. I'll be using the username "ncbackup" in this guide.
You can use any username you prefer, but make sure it's easy to remember and understand. It's a good idea to use a name that clearly indicates its purpose.
Let's start by creating the ncbackup user account and the directories needed to store our backups.
Setup
To set up Nextcloud, you need to ensure the configuration file NextcloudBackupRestore.conf is in the same directory as the backup and restore scripts. This is a crucial step to avoid any configuration issues.
Keep in mind that some options in NextcloudBackupRestore.conf are not configured using the setup script, but are set to default values instead. These options are marked as 'OPTIONAL' in the configuration file.
Step 3: Connect to Opnsense
In Step 3, you'll connect Opnsense with Nextcloud. To do this, navigate to the Nextcloud Section in System ‣ Config ‣ Backup.
You'll need to enter a few values to establish the connection. Start by checking the "Enable" box. This will allow you to configure the connection.
Next, enter the Base URL of your Nextcloud installation. This is the full URL, including https://, like https://cloud.example.com. Make sure to include the "https://" part.
You'll also need to enter your chosen username. This is the name you'll use to log in to Nextcloud.
To complete the connection, paste your app password from Step 2 into the "Password" field. This password is specific to the Nextcloud app and should be kept secure.
Finally, give your backup directory a name consisting of alphanumeric characters. You can keep the default name if you like.
Verify Configuration Upload
After configuring your backup, you'll want to verify that everything worked as planned. The newly created directory should contain at least one backed up configuration file.
To do this, simply open the directory and take a look inside. You should see a single backed up configuration file, which is a good sign that the upload was successful.
The search feature in Nextcloud works quite well, and it does partial matching, so you only need to know a part of the file's name to find it. This can be a big help if you're not sure what the file is called.
The find command is also a useful tool for locating files and versions. It searches through each backup, making it easy to find what you need. If you have multiple versions of a file, the utility will show you all of them, including any old versions that might be stored in the files_versions directory.
You can also use the history command to get more information about a file's past versions. This will show you the history of changes between two different backups, including file sizes, which can give you a better idea of which version you want to restore.
Cloud and Storage
You can store backups externally, either by default or as an optional feature. By default, backups will be stored locally in your "appdata" folder, taking up roughly the same space as your entire Nextcloud setup.
To alleviate pressure on storage space, you can follow the corresponding step to change the backup location.
External storage can be added as a backup location by scrolling to the bottom of the Backup settings page and selecting it from the drop-down menu. You can also create a subfolder like "backups" to store your backups.
Here are the tools you'll need to set up NextCloud backup to Amazon S3:
- AWS CLI
- Rclone
These tools will help you configure Rclone for S3, create a backup script, and schedule the backup to run automatically.
Cloud
Backing up your cloud storage is crucial, populate the file with the following to make your backup script executable.
You can configure visudo to allow the backup user to run the script as sudo without a password. This way, the backup user won't be able to run anything else as sudo.
Enabling sudo access for the backup script introduces a potential security risk, the backup user can run the script as sudo without a password.
If you need to be able to login using the backup user, you can revert the change by running the following command.
S3
S3 is a popular cloud storage option that can be integrated with Nextcloud. You can store your backups on S3 using the External Storage Support App in Nextcloud, which allows you to configure a connection to your S3 storage.
To get started, you'll need to enable the External Storage Support App in the Apps section of your Nextcloud Admin settings. From there, you can configure your S3 storage settings, including entering your AWS Access Key ID, Secret Access Key, and region.
You can also use Rclone to manage files on your S3 storage. Rclone is a command-line program that allows you to configure a remote for your S3 storage, which you can then use to create a backup script.
Here are some common S3 storage options and their corresponding settings:
Once you've configured your S3 storage, you can create a backup script using Rclone. This script will allow you to automate your backups to S3, which can be scheduled to run at regular intervals using a tool like cron.
The File
The file is a crucial part of any cloud storage system. Fortunately, the search feature in Nextcloud works quite well, allowing you to find the file even if you only know a part of its name.
You can use the find command to locate the file and any other versions that may exist. This command is especially helpful if you have multiple versions of a backup downloaded.
If you're looking for a specific version of a file, you'll see it under the files_versions directory, and it might have a slightly different name like Example.md.v1651162117.
The history command provides a bit more information, showing you the history of a file between two different backups. This can be useful in determining which version you want to restore.
The file size can also give you an indication of which version you want to restore. For example, if one version is significantly larger than the others, it might be the most up-to-date version.
Restoring a file is relatively straightforward. You can use the find command to locate the file, and then use the history command to determine which version you want to restore.
Once you've restored the file, refreshing the page will show that it has been successfully restored. However, it won't show up in your Nextcloud immediately, for reasons that aren't entirely clear.
Backup and Restoration
To create a backup of Nextcloud, simply call the script NextcloudBackup.sh on your Nextcloud machine. This script can be called without a parameter, which will save the backup in a directory with the current timestamp in your main backup directory.
You can also specify the main backup directory as a parameter, such as ./NextcloudBackup.sh /media/hdd/nextcloud_backup. This will use the specified directory as the main backup directory. It's also a good idea to set up the script to run automatically using cron, which can be done by adding a line like 0 2 * * * /path/to/scripts/Nextcloud-Backup-Restore/NextcloudBackup.sh > /path/to/logs/Nextcloud-Backup-$(date +\%Y\%m\%d\%H\%M\%S).log 2>&1 to your crontab.
Restoring a backup is also a straightforward process. First, you'll need to decompress your backup to /var/snap/nextcloud/common, and then use the nextcloud.import command to restore it. This will restore everything from the point in time the backup was made, including files, database, configuration, and data.
Encrypting Your
Encrypting your backups is a critical component, especially when storing data in the cloud. It's also a good idea to encrypt your backups on external hard drives to minimize the risk of data theft.
You must export your config file and store the decryption key in a safe place. I recommend using a USB key or something small you can secure in a safe.
Keep a record of the long random string of letters that shows onscreen as your encryption key for the configuration file. This is crucial to accessing your encrypted backups.
Storing the configuration file in the same location as your backups is not recommended. Instead, use a separate, secure location to store it.
Hand-writing the encryption key on a piece of paper and keeping it somewhere safe is a good option. Alternatively, consider using a proper password manager like KeePassXC, pass, or BitWarden/ValutWarden.
Backup
To create a backup of your Nextcloud data, simply call the script NextcloudBackup.sh on your Nextcloud machine. This script can be run manually or scheduled to run automatically at a specified time.
If you run the script without any parameters, the backup will be saved in a directory with the current timestamp in your main backup directory. For example, this would be /media/hdd/nextcloud_backup/20170910_132703.
You can also specify a main backup directory by running the script with a parameter, such as ./NextcloudBackup.sh /media/hdd/nextcloud_backup.
It's a good idea to store your backups on an external hard drive or in the cloud, and to encrypt your backups to protect your data in case your device is stolen or compromised.
To encrypt your backups, you'll need to export your config file and store the decryption key in a safe place, such as a USB key or a password manager like KeePassXC or BitWarden.
Automation and Tools
Automating your Nextcloud backups is a great way to ensure your data is safe and secure. You can create a script and use scheduled tasks, such as cron jobs, to run the script at specific intervals.
To automate Nextcloud backups, you can use a script that incorporates the information from the provided content. Replace [database_server], [database_username], [database_password], and [database_name] with your actual MySQL/MariaDB server details.
Save the script to a file, for example, backup_script.sh, and make it executable. You can then schedule this script to run automatically using cron jobs. Open the crontab editor and add a line to schedule the backup script at your preferred interval, such as running the backup every day at 2 AM.
The backup script will perform several tasks, including putting NextCloud into maintenance mode, using rsync to copy the Nextcloud main directory to a backup location, creating a tar.gz archive of the data directory, using mysqldump to create a SQL dump of the Nextcloud database, and disabling maintenance mode.
Updating the Scripts
Updating the scripts is a crucial step to ensure you're working with the latest features and settings. This involves updating the scripts to a newer version and then executing the setup script again.
The setup script, typically named setup.sh, needs to be run once more to apply the latest changes to the configuration file NextcloudBackupRestore.conf. An existing version of this file will be overwritten in the process.
You should also run the setup script if you want to change basic settings like compression. This way, you can easily switch on or off compression as needed.
Automated Tools
Automated Tools can be a game-changer for managing your Nextcloud setup.
You can automate Nextcloud backups by creating a script and using scheduled tasks like cron jobs.
Save the script to a file, make it executable, and schedule it to run at your preferred interval using cron jobs.
To schedule the backup script, open the crontab editor and add a line to run the script at your preferred time, such as every day at 2 AM.
Here's a breakdown of the steps involved in the backup script:
- Puts NextCloud into maintenance mode to ensure data integrity during the backup.
- Uses rsync to copy the Nextcloud main directory to a backup location.
- Creates a tar.gz archive of the data directory.
- Uses mysqldump to create a SQL dump of the Nextcloud database.
- Disables maintenance mode.
Make sure to adjust paths and server details according to your setup and secure the backup script, especially if it contains sensitive information like database passwords.
Script
When it comes to creating a backup script for Nextcloud, you have a few options depending on your operating system. For Linux-based systems like Ubuntu, you can use a basic automated backup script that you can customize to fit your needs.
This script assumes you're using a Linux-based system and needs to be adapted to your server environment. You'll need to replace placeholders like /path/to/backup, your_database_user, your_database_password, and your_database_name with your actual values.
To make the script executable, save it and use a command like chmod +x to give it the necessary permissions.
On a Mac, you'll need to use the Terminal and a text editor to create a backup script. This involves opening Terminal and creating a new backup script using a command like nano.
A basic backup script can be run manually or scheduled using cron for regular automated backups. You'll need to adjust the cron schedule to fit your preferences.
Ensure that the script and any sensitive information, like database credentials, are stored securely. It's also a good idea to test the script in a safe environment before deploying it to production.
File Change Notification
You need to notify Nextcloud of the file change by running the files:scan command, which scans the modified directory and updates the database.
This step shouldn't happen automatically, but you can speed up the scan by only scanning the modified directory.
After running the files:scan command, you should call maintenance:data-fingerprint once to change the ETag for all files in communication with sync clients, allowing them to realize a file was modified.
This is a crucial step, especially after restoring a backup of your data directory or database, as it ensures your sync clients are aware of the file change.
Frequently Asked Questions
How do I export data from Nextcloud?
To export data from Nextcloud, use the command `sudo nextcloud export` to create a timestamped backup of your entire instance, excluding SSL Certificates. This CLI utility is a convenient way to safeguard your data and settings.
Featured Images: pexels.com