Nextcloud

Thursday, May 26, 2022
I run Nextcloud as an 'App' on my TrueNAS server. This guide will be written from that perspective.

🔗Nextcloud client advanced settings

I use Cloudflare as a proxy in front of my Nextcloud instance. The maximum upload size for my zone is 200 MB. For that reason, I need to set some custom values in the Nextcloud Client config file so it knows to chunk the files correctly, otherwise the upload/download fails.

🔗Linux

vi ~/.config/Nextcloud/nextcloud.cfg

Add the following under the last entry in the [General] section.

chunkSize=50000000
minChunkSize=5000000
maxChunkSize=200000000
targetChunkUploadDuration=6000

Exit the client and re-open it to apply the changes.

🔗MacOS

vi ~/Library/Preferences/Nextcloud/nextcloud.cfg

Add the following under the last entry in the [General] section.

chunkSize=50000000
minChunkSize=5000000
maxChunkSize=200000000
targetChunkUploadDuration=6000

Exit the client and re-open it to apply the changes.

🔗Restore Nextcloud from a backup

🔗Background

The scenario is that the container(s) running Nextcloud on TrueNAS were deleted, or corrupted beyond repair. In that case we will need to restore the Nextcloud directory backup and the PostgreSQL database dump to a clean install. This guide assumes you have a backup of the Nextcloud install directory, along with a databse dump.

🔗How to backup Nextcloud

🔗Backup install directory

🔗Enable maintenance mode

  1. Shell into the nextcloud container.
  2. Once the prompt loads type runuser and press enter.
  3. Enable maintenance mode by executing the following command runuser -u www-data -- php occ maintenance:mode --on
  4. You can confirm maintenance mode was enabled by trying to go to your Nextcloud instance in your browser.

🔗Create a backup of the Nextcloud install directory

  1. Go to the TrueNAS shell System SettingsShell
  2. Change directory to where you have Nextcloud installed cd /mnt/pool_name/nextcloud
  3. The next command will create a backup of the entire install directory while preserving timestamps rsync -Aavx install/ nextcloud-dirbkp/
  4. Once the command finishes running type ls -l to confirm there is a directory called nextcloud-dirbkp

🔗Create a backup of the Nextcloud database

  1. Shell into the nextcloud-postgres container.
  2. Once the prompt loads type runuser and press enter.
  3. The following command will dump the nextcloud databse pg_dump nextcloud -h localhost -U oc_admin -f nextcloud-sqlbkp.bak
  4. Type ls -l to confirm that you see the file nextcloud-sqlbkp.bak
  5. We now need to copy the backup to a directory outside of the container. The directory postgres_backups/ is mapped outside of the container by default cp nextcloud-sqlbkp.bak postgres_backups

Now that we have the backup copied, we need to move it to a location that won't be deleted after the container is removed.

  1. Go to the TrueNAS shell and execute the following cp /mnt/pool_name/ix-applications/releases/nextcloud/volumes/ix_volumes/ix-postgres_backups/nextcloud-sqlbkp.bak /mnt/pool_name/nextcloud/

You now have a backup of your Nextcloud installation and databse.

🔗How to restore Nextcloud Backup

🔗Create new Nextcloud containers

  1. Delete any existing Nextcloud containers
  2. Use the following parameters from the container configuration.
- Application Name: `nextcloud`
- Certificate Configuration: `Select one if you have it`
- Nextcloud host: `Set domain if you have it. 123.example.com`
- Storage: Select `Enable Host Path for Nextcloud Data Volume` and set the path

Leave everything else set to the default.

We need to login to the install one time before we can restore our data. The default login is admin/changeme

🔗Restore Nextcloud data

  1. Shell into the nextcloud container.
  2. Once the prompt loads type runuser and press enter.
  3. Enable maintenance mode by executing the following command runuser -u www-data -- php occ maintenance:mode --on
  4. You can confirm maintenance mode was enabled by trying to go to your Nextcloud instance in your browser.
  5. Go to the TrueNAS shell and change directory to where you have Nextcloud installed cd /mnt/pool_name/nextcloud
  6. Execute the following to copy our backup data over the new install rsync -Aavx nextcloud-dirbkp/ install/
  7. Shell into the nextcloud-postgres container. Once the prompt loads type runuser and press enter.
  8. Before we can restore the database dump, we need to drop and re-create all tables psql -h localhost -U oc_admin -d template1 -c "DROP DATABASE \"nextcloud\";" then psql -h localhost -U oc_admin -d template1 -c "CREATE DATABASE \"nextcloud\";"
  9. Go to the TrueNAS shell and copy the postgres dump to the mounted postgres_backups directory in the container cp /mnt/pool_name/nextcloud/nextcloud-sqlbkp.bak /mnt/pool_name/ix-applications/releases/nextcloud/volumes/ix_volumes/ix-postgres_backups/
  10. Shell into the nextcloud-postgres container. Once the prompt loads type runuser and press enter.
  11. cd to the backup directory cd postgres_backups/. Type ls to confirm that you see nextcloud-sqlbkp.bak
  12. Restore the database backup psql -h localhost -U oc_admin -d nextcloud -f nextcloud-sqlbkp.bak
  13. stop then start the Nextcloud containers. If you see an Internal Server Error page when you try to load Nextcloud in your browser, it's likely due to the fact hat the oc_admin password needs to be updated. To verify this, shell into the nextcloud container. Once the prompt loads type runuser and press enter.
  14. Execute runuser -u www-data -- php occ. If the first few lines say FATAL: password authentication failed for user "oc_admin" in /var/www/html/lib/private/DB/Connection.php:139 then that means we need to update the password. If it says something else...good luck.
  15. While we're in the nextcloud container we need to get the updated password. Run cat config/config.php | grep dbpassword and note the password somehwhere.
  16. Shell into the nextcloud-postgres container. Once the prompt loads type runuser and press enter.
  17. Execute the following command replacing DBPASSWORD with the password from the previous step psql -h localhost -U oc_admin -d template1 -c "ALTER user oc_admin password 'DBPASSWORD'";. If successful, you should see the output ALTER ROLE
  18. stop then start the Nextcloud containers.
  19. If that fixed the issue, you should now see the Nextcloud maintenance page in your browser instead of Internal Server Error
  20. Shell into the nextcloud container. Once the prompt loads type runuser and press enter.
  21. Disable maintenance mode by executing the following command runuser -u www-data -- php occ maintenance:mode --off
  22. The last step is to run runuser -u www-data -- php occ maintenance:data-fingerprint in the nextcloud container that announces to clients a backup was restored.
  23. You should now be able to login and everything should be right where you left it. Restart any desktop clients if syncing does not start automatically.

🔗Common issues

When trying to delete a file in the browser or from a client, you may see an error 423 LOCKED or UNABLE TO DELETE. This happens because something in the database causes a file lock to not be released. Luckily it's an easy fix.

  1. Shell into the nextcloud container.
  2. Once the prompt loads type runuser and press enter.
  3. Enable maintenance mode by executing the following command runuser -u www-data -- php occ maintenance:mode --on
  4. Shell into the nextcloud-postgres container. Once the prompt loads type runuser and press enter.
  5. This step is optional, but you can type \dt to see all the tables.
  6. Before we delete the locks, let's see them SELECT * FROM oc_file_locks WHERE lock = 1;. You should see some results printed to the screen, if you don't then there are no locks.
  7. If you do see locks or just want to try to delete the anyway execute DELETE FROM oc_file_locks WHERE lock = 1;
  8. Shell into the nextcloud container. Once the prompt loads type runuser and press enter.
  9. Disable maintenance mode by executing the following command runuser -u www-data -- php occ maintenance:mode --off
  10. You should now be able to delete any files that you were not able to delete before.