Nextcloud 12 migration guide

nextcloudFollowing one of the better decisions a couple of days ago, it was time today to re-install my webserver that runs Nextcloud. I made some errors setting it up, now almost a year ago. A couple of things could be fixed, a couple of things I’d rather not and just build it from the ground up.

I think Nextcloud has some excellent documentation, but the migration guide just didn’t work for me. I migrated everything as per the guide, but couldn’t get it running. After a few hours I decided to just start over. The next walk-through is the result of that. The big difference with the official documentation is that I’m doing a clean Nextcloud installation on the new server and then use my previous settings afterwards.

This might not work 100% for you, but nevertheless it could be useful, there is some nice steps backing up your data in it, good PHP stuff, etc. Just have a go at it.

On the old server

I will be making an extra backup, besides my normal one, to another server with the following steps. Although I did this extra backup, I did the restore and migration with my regular backup. This is a nice, extra check to see if your backup will suffice. The extra backup is just in case everything else fails.

# cd /var/www/yournextcloudinstallation

# sudo -u apache php occ maintenance:mode --on

Wait 10 minutes and stop Apache:

# systemctl stop httpd

Check your regular backups and make an extra one like so:

# rsync -avP -e 'ssh -p 2222' /data root@backupserver.yourdomain.com:/extrabackup

# rsync -avP -e 'ssh -p 2222' /var/www/ root@backupserver.yourdomain.com:/extrabackup

And maybe your /etc directory and others you’ll need:

# rsync -avP -e 'ssh -p 2222' /etc root@backupserver.yourdomain.com:/extrabackup

I need the -p 2222 since I’m running SSH on another port. Also be sure to install rsync on the receiving server.

Databases

Backup your databases. I find easiest to create a file called .my.cnf in my /root directory with these contents:

[client]
user = root
password = "putinyourpassword"
host = localhost

Secure it for the time being:

# chmod 600 .my.cnf

In this way your mysql commands remain short and simple and your password isn’t in the open (in the history). Don’t forget to remove the .my.cnf file when you’re done!

Backing up just the Nextcloud database:

# mysqldump --single-transaction nextclouddb > `date +"%Y%m%d"`_nextcloud.bak

Backing up all databases:

# mysqldump --all-databases > `date +"%Y%m%d"`_all.bak

Getting it to the new server:

# rsync -avP -e 'ssh -p 2222' /2017* root@backupserver.yourdomain.com:/extrabackup

On the new server

As said the official migration guide did not work for me. These are the steps I did on the new server.

I installed the LAMP stack which is rather easy, except for the P.

L: Linux, in my case CentOS 7.4 was installed and fully updated.

A: Apache is easy.

# yum install apache openssl mod_ssl

The openssl and mod_ssl are very important.

# yum start apache

# yum enable apache

M: MariaDB installation is just as easy.

# yum install mariadb mariadb-server

# yum start mariadb

# mysql_secure_installation (set password i.e.)

# yum enable mariadb

P: This was a bit of a clusterfuck…

PHP 7.1 and Nextcloud

I’d previously installed PHP 7.1 via the Remi repository but now learned there is a semi-official repository in the form of the IUS Community Project. You can’t have both and that’s where things got ugly. Eventually I figured it out and below are the steps. Sure, you won’t need PHP 7.1 and I believe PHP 5.6 will do, but when I’m installing a new system, I want to minimally try and get the latest stuff on there.

First, get your database back in check after installing MariaDB. Get your database backups on your new server and import them with one ease step (I had multiple databases and restored them all):

# mysql < 20171128_all.bak

Then get the correct repository:

# yum install https://$(rpm -E '%{?centos:centos}%{!?centos:rhel}%{rhel}').iuscommunity.org/ius-release.rpm

# yum update

And install the PHP modules you need (as previously described here):

# yum install php71u[modulename]

But as you can see: be sure to use the php71u syntax before your specific module.

Don’t forget to open up your https port:

# firewall-cmd --add-port 443/tcp --permanent

# firewall-cmd --reload

SELinux

Move all your files in the correct location. For me this was my data folder (which was outside the Nextcloud installation folder) and a couple of configuration files like httpd.conf. Make sure SELinux doesn’t screw things up for you with the

# restorecon -RF yourfileorfolder

command. For your data folder specifically you’ll need:

# semanage fcontext -a -t httpd_sys_rw_content_t '/data(/.*)?'

# restorecon -RF /data

I did also set these SELinux booleans:

# setsebool -P httpd_can_network_connect_db on

# setsebool -P httpd_can_network_connect on

And to be able to send your notifications:

# setsebool -P httpd_can_sendmail on

Fresh Nextcloud installation

As you might have noticed, I did not put the Nextcloud installation back to it’s original location. That is because I did a clean install on this new server:

# cd /tmp

# yum install bzip2

# curl -o nextcloud-12-latest.tar.bz2 https://download.nextcloud.com/server/releases/latest-12.tar.bz2

# tar -xvjf nextcloud-12-latest.tar.bz2

# mkdir nextcloud/data

# mv nextcloud /var/www/cloud.yourdomain.com

# chown -R apache:apache cloud.yourdomain.com

# semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/cloud.yourdomain.com(/.*)?'

# restorecon -RF /var/www/cloud.yourdomain.com

# systemctl restart httpd

When you surf to your admin page, you get prompted for the database information (which we restored). Make a temporary new administrator, put in your database information, data location and login. Afterwards, log in with your old admin, check all your stuff and delete the temporary admin.

Also have a final look here and here to restore your additional config, if any.

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.