A quick and dirty howto for LDAP server migration

I will share you my method to migrate from one LDAP server to another with differents CN.

  • Configure the new LDAP server in your nextcloud instance.

  • In command line use OCC to update the mapping table (oc_ldap_group_mapping)

occ user:list

It will fill the table with users from the new directory.

  • Adapt and use this dirty python script to remove the old mapping and to connect local user to the new ldap one. If you have a lot of users, you will need another script to get the mapping list :face_with_hand_over_mouth:
#! /usr/bin/python3

import mariadb


mapping = {
    "uid=user1,ou=users,dc=test,dc=lan" : "uid=newuser1,cn=users,cn=accounts,dc=test,dc=fr",
    "uid=user2,ou=users,dc=test,dc=lan" : "uid=newuser2,cn=users,cn=accounts,dc=test,dc=fr"
}



try:
    conn = mariadb.connect(
        user="nextcloud",
        password="XXXXX",
        host="XXXXX",
        port=3306,
        database="nextcloud"
    )
except mariadb.Error as e:
    print(f"Error connecting to mariadb Platform: {e}")
    exit(-1)

cur = conn.cursor(named_tuple=True)

for oldLogin, newLogin in mapping.items():
    cur.execute("SELECT owncloud_name FROM oc_ldap_user_mapping WHERE ldap_dn LIKE \"{}\";".format(oldLogin))
    rows = cur.fetchall()
    if cur.rowcount != 1:
        print("More than one record found for {}.".format(oldLogin))
        continue
    userID = rows[0][0]
    cur.execute("DELETE FROM oc_ldap_user_mapping WHERE owncloud_name=\"{}\";".format(userID))
    cur.execute("UPDATE oc_ldap_user_mapping SET owncloud_name=\"{}\" WHERE ldap_dn LIKE \"{}\";".format(userID,newLogin))

conn.commit()
conn.close()

I give it as this. Read the script before use, there isn’t a lot of tests :wink:

1 Like