1. Home
  2. Linux
  3. Backup nextcloud snap installation on linux

How To Backup A NextCloud Snap Installation On Linux

Nextcloud truly is the software of the future. Thanks to it, it’s simpler than ever to spin up your own user-friendly, Dropbox-like storage solution on Linux. Setup for the software is extremely easy thanks to things like Snap packages. However it isn’t as easy to backup a NextCloud Snap installation.

Note: Before doing a Nextcloud backup, ensure the entirety of your files has finished syncing. During the backup process, the NextCloud snap will shut off, suspending all services to all users connected to it.

Stop The Server

Creating a backup of Nextcloud, especially the Snap version, requires the suspension of the server software. Without turning the Nextcloud server off, certain files may fail to save, permission errors could arise, and even data could be lost.

Luckily, suspending a running Nextcloud server is just about as easy as installing it! To stop the server, open up a terminal and gain a root shell using su or sudo -s. Then, use the snap stop command to stop all Nextcloud services.

su -

or

sudo -s

snap stop nextcloud

Running the snap stop command will disable the SQL database, and other running Nextcloud services. It will not uninstall them, so don’t worry! Your files are safe!

Back Up Folders

With the server temporarily shut off, it’s safe to create a backup of Nextcloud. However, before the server-side backup can begin, we recommend creating a backup of the ~/Nextcloud folder for each user. This way, if anything happens to the server backup, users will still have a duplicate of their data.

To create a backup of a Nextcloud sync folder, go to any Linux PC that uses the sync server, and open up a terminal. In the terminal, use the Tar command to create an archive of the ~/Nextcloud folder. Be sure to replace “username” in the command below with your username.

tar -zcvpf nextcloud-local-backup-username.tar.gz ~/Nexcloud

Depending on how large ~/Nextcloud is, the compression process may take a while. When the archiving process is complete, use the GPG command to encrypt the archive (for security purposes.)

gpg -c nextcloud-local-backup-username.tar.gz

rm nextcloud-local-backup-username.tar.gz

GnuPG will output nextcloud-local-backup-username.tar.gz.gpg.

After GPG finishes the encryption process, place the encrypted backup somewhere safe.

Back Up Nextcloud

Backing up the Snap version of Nextcloud is by far the easiest, compared to the traditional Nextcloud setup. Since everything is in the Nextcloud Snap folder, there’s no need to export any SQL databases or mess with individual files. Instead, users can create a complete backup of Nextcloud by making a copy of two individual folders.

The first folder to backup within the Nextcloud Snap directory is the configuration directory. To determine the name of the Nextcloud config folder, run lsblk and see where it is on the system. As of writing this article, the mount folder is:

/var/lib/snapd/snap/nextcloud/7658

Make a new backup folder inside of /home/username/ with the mkdir command and use the cp command to copy everything to it.

mkdir ~/nextcloud-server-backup

mkdir ~/nextcloud-server-backup/config
sudo cp -rp /var/lib/snapd/snap/nextcloud/7658/* /home/username/nextcloud-server-backup/config

With the configuration files for Nextcloud in the backup folder, it’s time to save the data.

mkdir ~/nextcloud-server-backup/data

sudo cp -rp /var/snap/nextcloud/common/* /home/username/nextcloud-server-backup/data

Compress The Backup

Now that the backup is complete, it’s safe to compress the backup into a Tar archive for safekeeping. In the terminal, compress the data into a TarGZ archive, using the tar command.

Note: before compressing this backup, ensure you have enough disk space to support it.

tar -zcvpf nextcloud-snap-server-backup.tar.gz ~/nextcloud-server-backup

Depending on how much data is on Nextcloud, this could take a while. When the compression is complete, feel free to move the backup to an external hard drive or backup file server.

Encrypt Backup

The Nextcloud server has a backup but it’s not safe as it isn’t encrypted. To ensure the data on your Nextcloud server is safe from eavesdropping, encrypt it with GnuPG.

To encrypt the backup, open up a terminal and run the following command:

gpg -c nextcloud-snap-server-backup.tar.gz

Like the Nextcloud local backup, GPG will output a nextcloud-snap-server-backup.tar.gz.gpg file. This file is encrypted and safe, so it’s OK to delete the unencrypted archive:

rm nextcloud-snap-server-backup.tar.gz

Restore Backup

Need to restore the backup? Start out by moving nextcloud-snap-server-backup.tar.gz.gpg to /home/username/.

Next, decrypt the archive with gpg:

gpg nextcloud-snap-server-backup.tar.gz.gpg

Extract the archive, using tar.

tar -xvpf nextcloud-snap-server-backup.tar.gz

Reinstall before trying to restore the backup (if on a new system).

sudo snap install nextcloud

sudo snap stop nextcloud

Restore the backup with;

sudo cp -rpf /home/username/nextcloud-server-backup/data/* /var/lib/snapd/snap/nextcloud/7658/

sudo cp -rpf /home/username/nextcloud-server-backup/config/* /var/snap/nextcloud/common/

Finally, start up the Nextcloud server with the snap start command.

sudo snap start nextcloud

2 Comments

  1. Thanks for this article. I seem to have two config folders, do you know what the reason is for it?

    19112 and 19134 and they both contain the same folders.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.