minimal docker compose

https://github.com/nextcloud/docker/blob/master/.examples/docker-compose/insecure/postgres/apache/docker-compose.yml

# docker-compose.yml
version: '3'

services:
  db:
    image: postgres:alpine
    restart: always
    volumes:
      - db:/var/lib/postgresql/data:Z
    env_file:
      - db.env

  redis:
    image: redis:alpine
    restart: always

  app:
    image: nextcloud:apache
    restart: always
    ports:
      - 127.0.0.1:8080:80
    volumes:
      - nextcloud:/var/www/html:z
    environment:
      - POSTGRES_HOST=db
      - REDIS_HOST=redis
    env_file:
      - db.env
    depends_on:
      - db
      - redis

  cron:
    image: nextcloud:apache
    restart: always
    volumes:
      - nextcloud:/var/www/html:z
    entrypoint: /cron.sh
    depends_on:
      - db
      - redis

volumes:
  db:
  nextcloud:
# db.env
POSTGRES_PASSWORD=
POSTGRES_DB=nextcloud
POSTGRES_USER=nextcloud

sqlite -> postgres

official guide

https://muetsch.io/migrating-nextcloud-from-sqlite-to-mysql-with-docker.html example with mysql

php occ db:convert-type [options] type username hostname database php occ db:convert-type –all-apps mysql oc_mysql_user 127.0.0.1 new_db_name

someone’s blog

https://muetsch.io/migrating-nextcloud-from-sqlite-to-mysql-with-docker.html

  1. run docker mysql:5.7
  2. migrate
    1. create new db (sql script) // I ope the ENV vars solve this
    2. docker network // k3s stuff
    3. migrate data “freeze data”
php occ maintenance:mode --on

(skip) mysql 4-byte UTF-8

...

migrate data

php occ db:convert-type --all-apps --clear-schema mysql nextcloud_user mysql nextcloud

turn off maintenance mode

php occ maintenance:mode --off

verify server is now using mysql http://your-nextcloud-server.tld/settings/admin/serverinfo change startup command

|docker run -d \
-v /var/data/nextcloud:/var/www/html \
-v /var/data/nextcloud/data/:/var/www/html/data \
-p 127.0.0.1:9000:80 \
--network mysql \
-e MYSQL_DATABASE=nextcloud \
-e MYSQL_USER=nextcloud_user \
-e MYSQL_PASSWORD=iwonttellyouthis \
-e MYSQL_HOST=mysql \
--name nextcloud \
nextcloud:19

compare command used

php occ db:convert-type [options]  type  username      hostname  database
php occ db:convert-type --all-apps mysql oc_mysql_user 127.0.0.1 new_db_name

php occ db:convert-type [options]                 type  username       hostname  database
php occ db:convert-type --all-apps --clear-schema mysql nextcloud_user mysql nextcloud

my steps

(failed) check if can access db from container

(failed) with bash

DB_HOST=nextcloud-db DB_PORT=5432 bash -c 'printf "" 2>>/dev/null >>/dev/tcp/${DB_HOST}/${DB_PORT}'

(src: https://stackoverflow.com/questions/26911508/postgres-testing-database-connection-in-bash)

nope, container does not have /dev/tcp/

(failed) with php

<?php
    $connection = pg_connect ("host=localhost dbname=site user=postgres password=root");
    if($connection) {
       echo 'connected';
    } else {
        echo 'there has been an error connecting';
    } 
?>

src: https://stackoverflow.com/questions/32700618/simple-test-php-with-postgresql-database

<?php
    $connection = pg_connect ("host=nextcloud-db dbname=nextcloud user=nextcloud password=xxx");
    if($connection) {
       echo 'connected';
    } else {
        echo 'there has been an error connecting';
    } 
?>

failed, no way to run php from command line


migrate data

  1. turn on maintenance mode
occ maintenance:mode --on
  1. migrate data
#   db:convert-type [options]  type  username  hostname     database
occ db:convert-type --all-apps pgsql nextcloud nextcloud-db nextcloud
  1. turn off maintenance mode
occ maintenance:mode --off

failed at step 2:

Nextcloud is in maintenance mode, no apps are loaded.
Commands provided by apps are unavailable.

In ConvertType.php line 183:

  This command is temporarily disabled (until the next maintenance release).


db:convert-type [--port PORT] [--password PASSWORD] [--clear-schema] [--all-apps] [--chunk-size CHUNK-SIZE] [--] <type> <username> <hostname> <database>

-> blocked by bug

bug workaround

(src: https://github.com/nextcloud/server/issues/45257#issuecomment-2305327982)

edit createConnectionParams to add param + if statement

vi lib/private/DB/ConnectionFactory.php
public function createConnectionParams(string $configPrefix = '', array $additionalConnectionParams = [], $type = '') {
                if ($type === '') {
		        $type = $this->config->getValue('dbtype', 'sqlite');
                 }

edit ConvertType to pass $type

vi /app/www/public/core/Command/Db/ConvertType.php
/createConnectionParams
$connectionParams = $this->connectionFactory->createConnectionParams('', [], $type);

edit ConvertType to remove the guard comment out the entire throw block that has the throw statement with a comment referring to issue 45257

// WARNING:
// Leave in place until #45257 is addressed to prevent data loss (hopefully in time for th
//
//throw new \InvalidArgumentException(
//		'This command is temporarily disabled (until the next maintenance release).'
//);

to be safe, you probably want to take a backup of your sqlite db (assuming everyone’s data is at /data, I’m using linuxserver/nextcloud image)

occ maintenance:mode --on
cp /data/owncloud.db /data/owncloud.db-`date +%F`
occ maintenance:mode --off

run the migration

occ maintenance:mode --on
# Maintenance mode enabled

occ db:convert-type --all-apps pgsql nextcloud nextcloud-db nextcloud
# What is the database password?
# Creating schema in new database
# [...]
# Skipping migrations table because it was already filled by running the migrations
#  - oc_text_steps
#  91/91 [============================] 100% < 1 sec/< 1 sec

occ maintenance:mode --off
# Maintenance mode already disabled

confirm db used at https://<your-site>/settings/admin/serverinfo

and you may want to redo the code changes, dunno (or reboot the container, which wipes the code changes anyway)