Migrating from owncloud to nextcloud in Fedora and CentOS

Introduction

If you are familiar with owncloud then you may have heard of the split that occurred last year where a group of engineers forked the project and left owncloud to carry on with nextcloud.

Unsurprisingly the rift is not referred to on the owncloud site but there some reference to it at the nextcloud site.

The two projects are running in parallel but it's hard to predict the future at the best of times. So long as the two projects don't end up with directly conflicting dependencies that cannot be resolved and continue with their developments then I do intend to maintain both in Fedora.

Do note that I specifically mention Fedora there as the next major releases will be dropping PHP 5.4 support in both applications so at that time they'll be retired from EPEL7. I will have an article nearer that time over the options CentOS and RHEL users have.

Why migrate

There is no urgent need to migrate, however there does appear to be a significant shift in development "energy" and community activity to the fork and there are a series of additional features in nextcloud not present in owncloud that may be of interest. Many of these were enterprise only previously but are now available to all.

Planning out the migration

The supported path for the migration is from owncloud 9.1.4 to nextcloud 10.0.4

These are the versions I have specifically tested, the nextcloud migration documentation upstream can be found here.

If there is capacity for a backup (or via snapshots or similar) then you should ensure there is a backup of /var/lib/owncloud, the database used and /etc/owncloud/config.php

Depending on the size of the owncloud install and the capacity available on the system it's possible to do a completely parallel install without touching the owncloud data to allow for a safe fallback. However it is also possible to do an in place switch over with minimal space requirements ... just remember data that isn't backed up isn't data you care about!

The steps described here are about migrating directly on the same system, but the principles can be applied to migrating to a new system if preferred. The steps this article goes through can be found on the system in /usr/share/doc/nextcloud/MIGRATION.fedora in a system where the Fedora nextcloud package is installed.

Throughout this article I'll be using owncloud_user, owncloud_pass, nextcloud_user, nextcloud_pass, ownclouddb, nextclouddb to refer to the database configuration ... naturally replace with the details suitable for your own environment.

Migrating minimising the risk of data loss

The safest way to carry out the migration is to duplicate the data, the database, fixup the config and only then if all is well remove the owncloud instance.

Disable access to owncloud

The first thing to do before anything else is to prevent anyone making any further changes

sudo -u apache php /usr/share/owncloud/occ maintenance:mode --on

Install nextcloud

This is safe to install in parallel to owncloud

dnf -y install nextcloud nextcloud-httpd nextcloud-mysql

Naturally use the appropriate subpackages for the environment.

Transfer the files over

With that safely in place we can move the data over

rsync -aPh /var/lib/owncloud/ /var/lib/nextcloud/

Next the database needs to be copied. The process for the differs between postgresql and mysql.

Duplicating the mysql database

The cleanest way to do this is using mysql_dump being piped to mysql. This assumes no password on the mysql root user (the default), but use -u and -p to provide the root credentials for your install.

mysql -e 'create database nextclouddb;'
mysql -e "grant all on nextclouddb.* to 'nextcloud_user'@'localhost' identified by 'nextcloud_pass';"
mysqldump -v ownclouddb | mysql  -D nextclouddb

Duplicating the postgresql database

Postgres allows the creation of a database based on another one

sudo -u postgres psql <<EOF
 /* Create the user for nextcloud */
 CREATE USER nextcloud_user WITH PASSWORD 'nextcloud_pass';

 /* KILL ALL EXISTING CONNECTION FROM ORIGINAL DB (ownclouddb)*/
 SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity
 WHERE pg_stat_activity.datname = 'ownclouddb' AND pid <> pg_backend_pid();

 /* CLONE DATABASE TO NEW ONE(nextclouddb) */
 CREATE DATABASE nextclouddb WITH TEMPLATE ownclouddb OWNER nextcloud_user;

 GRANT ALL PRIVILEGES ON DATABASE nextclouddb TO nextcloud_user;

/* The tables need to be transferred in owner as well */
\c nextclouddb;
REASSIGN OWNED BY owncloud_user TO nextcloud_user;
 EOF

Don't forget to update pg_hba.conf with the details of the database!

Copy and update the configuration

The configuration can be copied right over

cp /etc/owncloud/config.php /etc/nextcloud/config.php

The config needs to be updated to point to the nextcloud database and filesystem. If there is a perfect translation over just

sed -i 's/owncloud/nextcloud/g' /etc/nextcloud/config.php

Enable the web interface

If using httpd the web interface is blocked by default so as per the README enable it

ln -s /etc/httpd/conf.d/nextcloud-access.conf.avail /etc/httpd/conf.d/z-nextcloud-access.conf
systemctl reload httpd

At this point the /nextcloud/ path should show the maintenance screen and not an error.

Carry out the upgrade itself

Due to the complexities it's highly recommended to do this at the CLI.

sudo -u apache php /usr/share/nextcloud/occ upgrade

Verify config has migrated and check apps

The best way to check this before allowing others access is to put it into single user mode and log in as an admin.

sudo -u apache php /usr/share/nextcloud/occ maintenance:singleuser --on
sudo -u apache php /usr/share/nextcloud/occ maintenance:mode --off

This is normal not to see the files at this point as DAV will be disabled until maintenance is off.

In testing it appeared that apps needed to be disabled and then enabled again to be picked up correctly.

Enable the nextcloud instance for all

At this point we can let the users at it!

sudo -u apache php /usr/share/nextcloud/occ maintenance:singleuser --off

Cleanup the old owncloud bits

Finally a bit of tidy up

dnf -y remove owncloud\*
rm -rf /var/lib/owncloud /etc/owncloud /etc/httpd/conf.d/*owncloud*
systemctl reload httpd
# mysql
mysql -e 'drop database ownclouddb; drop user owncloud_user;'
# postgresql
sudo -u postgres psql <<EOF
DROP DATABASE ownclouddb;
DROP USER owncloud_user;
EOF

Double disk space? We'll do it live!

Sometimes there just isn't the resources to do it the careful way...

Disable access to owncloud

The first thing to do before anything else is to prevent anyone making any further changes

sudo -u apache php /usr/share/owncloud/occ maintenance:mode --on

Install nextcloud

This is safe to install in parallel to owncloud

dnf -y install nextcloud nextcloud-httpd nextcloud-mysql

Naturally use the appropriate subpackages for the environment.

Transfer the files over

There's just a straight move needed

mv /var/lib/owncloud/* /var/lib/nextcloud/

Next the database needs to be copied. The process for the differs between postgresql and mysql.

Moving the databases

Since the intention is to be destructive this time then there is no need to change the database name from a technical perspective, the database configuration for nextcloud can just remain pointing at the original database. However if there's a preference for consistency in naming then it's worth carrying through these steps.

Note that since relatively speaking the database sizes are tiny compared to the file storage I strongly recommend going through the steps above to duplicate instead, however to be comprehensive this is how to carry out the database renames.

MySQL

mysql -e 'create database nextclouddb'
mysql -e "grant all on nextclouddb.* to 'nextcloud_user'@'localhost' identified by 'nextcloud_pass';"
mysql ownclouddb -sNe 'show tables' | while read table;  do mysql  -sNe "rename table ownclouddb.$table to nextclouddb.$table;"; done

PostgreSQL

sudo -u postgres psql <<EOF
 /* Create the user for nextcloud */
 CREATE USER nextcloud_user WITH PASSWORD 'nextcloud_pass';

 /* KILL ALL EXISTING CONNECTION FROM ORIGINAL DB (ownclouddb)*/
 SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity
 WHERE pg_stat_activity.datname = 'ownclouddb' AND pid <> pg_backend_pid();

 /* ALTER DATABASE to rename it */
 ALTER DATABASE ownclouddb RENAME TO nextclouddb;
 ALTER DATABASE nextclouddb OWNER TO nextcloud_user;

 GRANT ALL PRIVILEGES ON DATABASE nextclouddb TO nextcloud_user;

/* The tables need to be transferred in owner as well */
\c nextclouddb;
REASSIGN OWNED BY owncloud_user TO nextcloud_user;
EOF

Don't forget to update pg_hba.conf and reload postgres!

And the rest ...

The remaining steps are the same as above

  • Copy over and update the config file
  • If using httpd enable the web interface
  • Carry out the upgrade, preferably at CLI
  • Enable any apps that got disabled
  • Tidy up

Conclusion

At the end of this all data should be migrated and there should be a working nextcloud instance. If there's any difficulties please do contact me in #fedora-devel on freenode or through my Fedora account.

Add new comment