Reinstall using existing data-dir and user names

I am planning on migrating my Nextcloud installation to a new VM and came across an issue while testing the installation.

By setting --data-dir to an existing nfs share and using the same username as the original installation, it failed with the following error:

Username is invalid because files already exist for this user

I think it may be related to this change: https://github.com/nextcloud/server/pull/18130

Is there a recommended procedure for using the existing data with the same usernames in the new install?

Full command used to install:

php occ maintenance:install --database mysql --database-name my_db_name \
    --database-user my_db_user --database-pass my_db_pass --admin-user admin_user \
    --admin-pass admin_pass --data-dir /path/to/nfs/share

+1. I’m running into the same problem. Did you ever find a solution @chuckn246 ?

@skidelo
Maybe it is better to open an own thread.

If I were you, I would install Nextcloud completely empty, create the users manually, etc. Then I would copy (or mount) the data back into the datadirectory structure, and then re-load the files/directory in Nextcloud database with

sudo -u www-data /path/to/nextcloud/occ files:scan --all
documentation

I accomplished this by doing the following:

(note that I keep my store in a non-standard and detached-storage location. Also note that I’m using nextcloud snap)

The backstory:
I hosed my server by fatfingering a couple of commands as a privileged user. I had to wipe and reinstall the entire OS et al

How I fixed the exact same error username is invalid because it already exists for this user when attempting to create the user on my freshly installed nextcloud:

(note that for the purposes of this walk-through, let’s say the directory is named “theo”)

  1. I went to the datastore where the user directory existed and mv theo theo-restore
  2. I created the new “theo” user in nextcloud, which succeeded this time.
  3. Nextcloud created the new directory automatically in the data store. When I checked it, it was just a normal empty directory. So, I copied the contents from the old directory to the newly created directory in its entirety. cp -ap theo-restore/* theo/

Nextcloud happily imported the state of the files in the directory all by itself.

When all is done and validated, rm -rf theo-restore