> For the complete documentation index, see [llms.txt](https://documentation.ocsinventory-ng.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://documentation.ocsinventory-ng.org/project-overview/migration-guide/migrating-the-administrative-data.md). Agents can also search and read this documentation through its [MCP server](https://documentation.ocsinventory-ng.org/~gitbook/mcp).

# Migrating the administrative data

The `import_legacy_accountinfo` command migrates the administrative data of an OCS 2.X database (MySQL / MariaDB) into the 3.X backend. It never modifies the legacy database. It creates the missing fields, links each legacy machine to its 3.X asset and copies the values into it. A value already set in 3.X is not replaced unless you ask for it.

Run it once the machines send their inventory to the 3.X server, either with their original 2.X agent or with a 3.X agent: upgrading the agent is not required. A machine that has no 3.X asset yet is listed in the report, but no asset is created for it.

### What is migrated

| Legacy (OCS 2.X)                                        | 3.X                                      |
| ------------------------------------------------------- | ---------------------------------------- |
| Administrative fields of the computers, including `TAG` | Accountinfo fields targeting "Assets"    |
| Possible values of the lists and checkboxes             | Possible values of the field             |
| Values entered on each machine                          | Values of the field on the matched asset |

What is not migrated: the other legacy data (inventory, groups, packages).

Type mapping:

| Legacy type         | 3.X type                      |
| ------------------- | ----------------------------- |
| TEXT                | Text                          |
| TEXTAREA            | Textarea                      |
| SELECT, RADIOBUTTON | Select                        |
| CHECKBOX            | Checkbox                      |
| DATE                | Text, formatted `YYYY-MM-DD`  |
| NUMBER              | Text                          |
| any other type      | ignored, listed in the report |

If a field with the same name already exists in 3.X (targeting "Assets"), it is reused as is. Only the list values it lacks are added.

### Prerequisites

1. The agents (2.X or 3.X) send their inventory to the 3.X server, and each machine has sent at least one.
2. The `legacy_duplicate_reconciliation` server setting has been checked (see "Machine matching").
3. The 3.X server can reach the legacy database on the MySQL port (3306 by default).

Create a read-only MySQL account on the legacy database. The command opens a read-only session anyway, but a restricted account rules out any mistake:

```sql
CREATE USER 'ocs_migration'@'V3_SERVER_IP' IDENTIFIED BY 'PASSWORD';
GRANT SELECT ON ocsweb.accountinfo_config TO 'ocs_migration'@'V3_SERVER_IP';
GRANT SELECT ON ocsweb.accountinfo        TO 'ocs_migration'@'V3_SERVER_IP';
GRANT SELECT ON ocsweb.config             TO 'ocs_migration'@'V3_SERVER_IP';
GRANT SELECT ON ocsweb.hardware           TO 'ocs_migration'@'V3_SERVER_IP';
GRANT SELECT ON ocsweb.bios               TO 'ocs_migration'@'V3_SERVER_IP';
GRANT SELECT ON ocsweb.networks           TO 'ocs_migration'@'V3_SERVER_IP';
```

Replace `ocsweb` with the name of your legacy database, `V3_SERVER_IP` with the address of the 3.X server and `PASSWORD` with the password you chose.

### Procedure

Run every command from the 3.X backend directory. First define the connection once, replacing `LEGACY_HOST`, `ocsweb` and `PASSWORD`:

```bash
export OCS_LEGACY_PASSWORD='PASSWORD'
LEGACY_URL=mysql://ocs_migration@LEGACY_HOST:3306/ocsweb
```

Passing the password through `OCS_LEGACY_PASSWORD` rather than in the URL keeps it out of the shell history.

**1. Simulate the creation of the fields.**

```bash
python manage.py import_legacy_accountinfo \
    --legacy-url "$LEGACY_URL" \
    --definitions-only --dry-run
```

Review the `ignored` fields in the report: unsupported type, name too long, missing column.

**2. Simulate the full import.**

```bash
python manage.py import_legacy_accountinfo \
    --legacy-url "$LEGACY_URL" \
    --dry-run
```

Review the `unmatched`, `duplicate` and `rejected` rows of the report (see "[Reading the report](#reading-the-report)").

**3. Fix what can be fixed.** Let the missing machines send their inventory to the 3.X server, deal with the duplicate 3.X assets, adjust the matching setting if needed. Run the simulation again until the result is satisfactory.

**4. Run the actual import.**

```bash
python manage.py import_legacy_accountinfo \
    --legacy-url "$LEGACY_URL"
```

**5. Check and archive.** Check a few assets in the 3.X interface and keep the CSV report.

**6. Run it again when needed.** Once new machines have reached 3.X, run the same command again. It only fills in the values that are still empty and never creates duplicates.

### Options

| Option               | Default                              | Effect                                                                          |
| -------------------- | ------------------------------------ | ------------------------------------------------------------------------------- |
| `--legacy-url`       | required                             | connection to the legacy database, `mysql://user[:password]@host:port/database` |
| `--dry-run`          | no                                   | writes nothing in 3.X, prints the same output and writes the same report        |
| `--overwrite`        | no                                   | replaces the 3.X values already set                                             |
| `--definitions-only` | no                                   | only creates the fields and their list values                                   |
| `--batch-size`       | 500                                  | machines written per transaction (1 to 10,000)                                  |
| `--report`           | `./accountinfo-migration-<date>.csv` | path of the detailed report                                                     |

{% hint style="warning" %}
**Be careful with `--overwrite`**: it replaces the values entered in 3.X since the switch with the legacy values. Only use it if the legacy database is the reference. An empty legacy value never clears a 3.X value, even with this option.
{% endhint %}

Add `-v 2` to display the progress batch by batch.

### Console output

```bash
Legacy database: ocsweb@host (schema 7067) - read only
Definitions: 12 created, 3 existing, 1 ignored (type 7)
Machines: 8 412 matched (8 120 by DEVICEID, 292 by duplicate rules), 188 unmatched, 4 duplicates
Values: 41 230 imported, 512 kept (existing), 37 rejected
Report: ./accountinfo-migration-2026-10-17.csv
```

In a simulation, the first line ends with `- DRY RUN, nothing written`.

| Exit code | Meaning                                                                                      |
| --------- | -------------------------------------------------------------------------------------------- |
| 0         | import completed; any rejected values are listed in the report                               |
| 1         | invalid option                                                                               |
| 2         | legacy database unreachable, access denied or schema not recognized                          |
| 3         | error during the import: the current batch is rolled back, the previous batches stay written |

After exit code 3, fix the cause and run the command again: the values already imported are recognized and kept.

### Machine matching

Each legacy machine is linked to a single 3.X asset:

1. **By DEVICEID**: a 2.X agent sending its inventory to the 3.X server usually keeps its DEVICEID, so the 3.X asset carries the same one as the legacy machine.
2. **Otherwise, by duplicate rules**: the `name`, `serial` and `srcmac` fields selected in the `legacy_duplicate_reconciliation` setting, which must all match. `name` is the machine name (case-insensitive), `serial` the BIOS serial number, `srcmac` the MAC address of the first network interface up on the legacy machine, found if it is one of the MAC addresses of the 3.X asset. With the default setting (`uuid` only), the command uses the machine name and the BIOS serial number. This step also finds the machines moved to a 3.X agent, which no longer report their legacy DEVICEID.
3. **An empty field** (`Empty`, or a missing serial number, common on virtual machines): the machine is `unmatched`. It is never matched on the remaining fields alone.
4. **Several candidate assets**: the machine is `duplicate`, nothing is imported for it.
5. **An asset claimed by several legacy machines**: it goes to the machine seen most recently (`LASTDATE`), the others become `duplicate`. If their dates are equal, they all become `duplicate`.

### Reading the report

The report is a UTF-8 CSV file, with `;` as separator. It holds one row per field, per unmatched machine and per value processed. An existing report is never overwritten: a `-1`, `-2`… suffix is added to the name.

| Status        | Scope   | Meaning                                                           |
| ------------- | ------- | ----------------------------------------------------------------- |
| `created`     | field   | field created in 3.X                                              |
| `existing`    | field   | 3.X field with the same name reused                               |
| `ignored`     | field   | field not migrated, reason in `reason`                            |
| `unmatched`   | machine | no 3.X asset found, or no matching field set                      |
| `duplicate`   | machine | several candidate assets, or asset given to a more recent machine |
| `imported`    | value   | value copied into an empty 3.X field                              |
| `kept`        | value   | 3.X value already set, kept                                       |
| `overwritten` | value   | 3.X value replaced (`--overwrite`)                                |
| `rejected`    | value   | legacy value that matches nothing in 3.X, reason in `reason`      |
| `failed`      | batch   | batch rolled back after an error                                  |

{% hint style="info" %}
The report never contains the connection URL or the password.
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://documentation.ocsinventory-ng.org/project-overview/migration-guide/migrating-the-administrative-data.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
