Some time ago I posted this post over at Fosstodon showing my "perfect" Nextcloud setup and told that I'll write a blogpost about that. So here I'm writing the first part of that setup which only includes the migration part.
My cousins and I had a Nextcloud server that's been running since the March of 2021. It was an old AMD A55 based platform with an entry level 4 core Athlon CPU that can't even do simple math with 8Gbs of DDR3 1600MHz ram. At the beginning it had a single 500GB HDD as storage but later we added a 120GB Sandisk SSD as boot drive and a second 500Gb HDD for Raid1 and since than it runs with small amount of problems.
But it wasn't a stable setup, what I mean by that is system needed to be restarted after running under load for some time and sometimes it was just crashing itself out of nowhere. This actually isn't a problem if you're running this server only for yourself but we had atleast 6 users at this server and each of these users had 3 devices linked to their accounts for file syncing or picture uploading and thanks to our powerhouse it wasn't able to keep up with more than 5 devices connected at the same time. And problem wasn't a memory or storage problem, it was the CPU's problem.
Until now we just kept our server usage minimal as possible but later we decided to upgrade the server to a much better hardware. Because the device count kept growing and we can't just stop using the thing we created.
We began to search for the best hardware to run the server on. At the beginning we were about to build a server with 10th generation I9 with 16Gbs of Ram and 1 TBs of SSD storage but later we decided to use an Optiplex 7050 with 8Gbs of Ram and 1 TBs of HDD storage. We did that becouse while my cousins building the system I got an free Optiplex 7050 and when we compared two systems side by side and Optiplex was a better option in terms of power consumption. So we decided to go with it.
Migrating a Nextcloud server is a 3 step process and it's not that much complicated if you're migrating it to a Debian based system. But in our case we're migrating it to a Rocky Linux based system and that's a bit complicated becouse the PHP packages on Rocky Linux repos doesn't supports the password hashing system that Nextcloud uses and you need to install PHP from php-remi repo in order to fix it. Also things works differently on Rocky than Debian and thats what makes our migration complicated.
- Do not delete your data from old server before you got the new one up and running perfectly fine. Otherwise it could lead to data loss.
- I'm not responsible for any potential data loss. This is a risky task and please prepare yourself for that.
- As we all know thing that works for me may not work for you. In that case please refer to official Nextcloud Documention
Getting old server ready
Getting the old server ready is actually very simple. You just need to put Nextcloud to maintiance mode and clone database to a safe place.
Enabling Maintiance mode
In order to put Nextcloud to maintiance mode, went to your Nextcloud directory and run the command down below.
sudo -u www-data php occ maintenance:mode --on
This command will run the occ binary as www-data user which is the default user of Apache web server on Debian based systems and enable the maintiance mode for the Nextcloud which means no one will ever be able to read or write from server while migration progress and that prevents us from data loss.
Dumping MySQL to a file
In our configration we're using MySQL as the database for Nextcloud but if you're using SQLite3 or PostgreSQL you can get more information from Official Nextcloud documention.
To dump MySQL use the command down below.
mysqldump --single-transaction --default-character-set=utf8mb4 -h localhost -u [username] -p[password] [db_name] > nextcloud-sqlbkp.bak
In this command you need to replace
[username] with your database user,
[password] with users password and
[db_name] with your Nextcloud database without brackets.
Don't forget to store this file on somewhere safe and accsesible becouse we're gonna use it later.
Getting new server ready
Since we gonna run the OS and Nextcloud server on same drive temporarly. You need to done partitioning drives correctly in order to expand the Nextcloud when you move OS to a SSD or move everything to a bigger storage solution like raid.
As you can see from the image, we have seperate home, root and Nextcloud partitions on our 1Tb drive. I use the XFS file system becouse I found it was more stable than ext4 and btrfs. When we get a proper SSD for this server I'm gonna move the root and home partitions to that SSD and extend the Nextcloud partition.
Nextcloud has some other depencies than PHP and to install them we're gonna use the command
sudo dnf update -y && sudo dnf install epel-release yum-utils unzip curl wget bash-completion policycoreutils-python-utils mlocate bzip2 httpd mariadb mariadb-server
The command above will install the everything that Nextcloud needs other than PHP. From our database to webserver.
As I said above we gonna use the PHP from Remi repos. Good thing about this repo is it provides us a simple rpm package for adding repo with just a single command. To add the Remi repo use the command
sudo dnf install http://rpms.remirepo.net/enterprise/remi-release-9.rpm
If you're setting up on other versions of Rocky, you can find the proper packages on Remi's RPM Repository
After adding the repo we need to install the php and required plugins for it. To do that you can use the command
dnf install -y php php-gd php-mbstring php-intl php-pecl-apcu php-mysqlnd php-opcache php-json php-zip php-imagick
Setting it up
If you done your partition configration like mine, you must have your nextcloud partition mounted at
/nextcloud. Now what we're gonna do is to download the correct version of Nextcloud. What I mean by that is, you can't just switch between major versions of Nextcloud and becouse of that you need to download and setup the latest one that starts with the same number as your old server.
For example, I had the version 25.0.7 on our server and when migrating over, I used the version 25.0.12. Later you can update the Nextcloud with the included server updater.
At that point I'm gonna continue with the version 25.0.12. If you're using a different version than you can go with it too. All these steps are same for those versions.
Download and extract Nextcloud
Change your directory onto
/nextcloud Go to Nextcloud changelog page and find the version you need. Than download it with the command. Don't forget to replace those xs with your version number.
curl -O https://download.nextcloud.com/server/releases/nextcloud-2x.x.x.zip
After downloading it extract that file with command down below.
I suggest you to rename the folder
server to prevent confusion but you can keep it like that if you want.
At the beginning of this tutorial we dumped the database from our old server and now it's time to import that database to our new server.
But before importing it we need to create the database that we're gonna import our data. Which is easy to do.
Firstly you need to start up the MySQL server using systemd.
sudo systemctl enable mariadb.service
sudo systemctl start mariadb.service
After that we can enter to MySQL shell with the command
Now you should see something like this on your terminal window.
Now you can create our database with the commands I give down below
Don't forget to change the username and password sections. I suggest you to use the same username and password combo for your new database so that we could use our old config file. If you don't know these you can find them on your old config file in plain text.
CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
CREATE DATABASE IF NOT EXISTS nextcloud CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
GRANT ALL PRIVILEGES on nextcloud.* to 'username'@'localhost';
After that we can import our database with the command
mysql -h localhost -u [username] -p[password] [db_name] < nextcloud-sqlbkp.bak
To migrate the data folder from old server to new one, we can simple use the scp command. At that point I suggest you to plug those servers to a high speed physical network like a switch or your router otherwise it would take a lot of time to copy the data.
Command for this process:
scp -r [user]@[oldserver]:/[oldnextcloudDir]/data [newNextcloudDir]/
If you done the database configration as I suggested, you can use your old config file without any problems. You'll need to change the
datadirectory section with your new data directory and
maintenance to false.
I personaly don't use SELinux on my system but if you're willing to do you can get more information from Nextcloud Example installation page
Apache Web server
And the last thing we need to do is configure our Apache webserver. To do that, change your dir to
/etc/httpd/conf.d and create a new file named
nextcloud.conf. After creating that file create a Virtualhost on it with the code down below.
In this code you need to change the
DocumentRoot with your Nextcloud directory and
ServerName with your domain.
Now to be able access our server from other devices we need to add an rule for Apache to our firewall. To do that you can simply use the command
sudo firewall-cmd --zone=public --add-service=http --permanent && sudo firewall-cmd --reload
After doing everything right go and restart the Apache server with command
sudo systemctl restart httpd
And it's done! Now you can visit your new server from a browser and see your Nextcloud screen. I tried my best to type this post and it worked perfectly on my server and I hope it does on your server too. If you're having problems you can visit the Nextcloud Admin Manual and get help from there. Or you can join our Discord server and we could try to help you out. As always, see you on the next one!
Reply via E-Mail
Batuhan Y. Yilmaz - 09.10.2023 - 31/100