Upgrade from TOS 3 to TOS 5 kills nextcloud installation

Hi folks,

today I had time to upgrade my Turris Omnia from TOS 3 to TOS 5. It seemed to work fine concerning the standard TO functionality. But the what about my Nextcloud installation? I was kind enough to make a full backup of my turris and it’s Nextcloud installation beforehand. So everything was fine for me though.
The TOS upgrade mechanism just installes NC 20 over NC 17 which is the recent version on TOS 3. It lead to an Internel Server Error…
These isntructions asume you have a backup created like that:

opkg update && opkg install mariadb-client-extra # Installs 'mysqldump' binary
cd /srv/www/nextcloud
sudo -u nobody php-cli ./occ maintenance:mode --on
cd /srv/www
rsync -avx nextcloud/ nextcloud-dirbkp_`date +"%Y%m%d"`/  # This does an incremental backup if executed on the same day again.
mysqldump --single-transaction -h 127.0.0.1 -u nextcloud -p[password] nextcloud > nextcloud-sqlbkp_`date +"%Y%m%d"`.bak  # For data to insert have a look at nextcloud/config/config.php.

cd /srv/www/nextcloud
sudo -u nobody php-cli ./occ maintenance:mode --off
cd /srv/www

# Just in case it matters, too: A full turris omnia backup can be done this way:
schnapps list
schnapps export <number_of_entry_to_backup> /srv/www

tar -cvzf nextcloud-dirbkp_`date +"%Y%m%d"`.tgz nextcloud-dirbkp_`date +"%Y%m%d"`/ nextcloud-sqlbkp_`date +"%Y%m%d"`.bak omnia-medkit-turris*

But there is a way to fix it:
cp -R nextcloud_backup_dir/config nextcloud/
cd nextcloud
chown -R nobody config/
chown -R nobody:nogroup config/config.php
cd …

cp -R nextcloud_backup_dir/data nextcloud/
cd nextcloud
chown -R nobody:nogroup data
cd ..

cp -R nextcloud_backup_dir/apps nextcloud/
cd nextcloud
chown -R nobody:nogroup apps
cd ..

cd nextcloud
sudo -u nobody php-cli ./occ maintenance:mode --on

At this point one can recreate the nextcloud database as described in Restoring backup — Nextcloud latest Administration Manual latest documentation. In my case the DB wasn’t touched at all during the TOS upgrade.
cd /srv/www/nextcloud

sudo -u nobody php-cli ./occ maintenance:mode --off
sudo -u nobody php-cli ./occ maintenance:data-fingerprint

This could be interesting/necessary too:

find . -type d -exec chmod 0755 {} \;
find . -type f -exec chmod 0644 {} \;
sudo -u nobody php-cli ./occ maintenance:repair
sudo -u nobody php-cli ./occ files:scan --all

After these steps I had a fine running Nextcloud version 17 again.
From there on it is possible to run the Nextcloud internal update proceedure again and again until it reached the version which comes with TOS5. By doing these updates nextcloud migrates all its data again and again so everything is fine after all.

Have fun with this little guide. Hope it helps some people like me that needed some time before installing the TOS upgrade.

kind regards
Christian