🖥️ CLI Commands¶
📋 Overview¶
Some work does not belong in the web UI: importing an existing archive from the local filesystem, or moving stored original messages between storage backends. Mail Archiver accepts both as commands on the command line.
| Command | Purpose |
|---|---|
--import-mbox |
Import an MBox file from a mounted directory |
--import-eml |
Import a ZIP archive of EML files from a mounted directory |
--migrate-raw-storage |
Move stored original messages between the database and the filesystem |
All commands are handled before the web pipeline starts and exit when they are done, so they are safe to run with docker compose exec while the archive is running.
⚠️ Security Model¶
The import commands are CLI-only – there is no web endpoint for browsing or importing local files. You must have Docker host access (docker exec) to run them.
A path whitelist (LocalImport:AllowedPaths) controls which directories the importer can read from. Files outside these paths are rejected.
--migrate-raw-storage is not subject to that whitelist: it reads only the message files that the archive itself wrote under RawMessages:FileSystem:BasePath.
🛠️ Setup¶
1. Configure Allowed Paths¶
In your docker-compose.yml, set the allowed import paths:
environment:
- LocalImport__AllowedPaths__0=/data/import
# Optional: multiple paths
# - LocalImport__AllowedPaths__1=/data/import2
2. Mount Your Files¶
Mount the directory containing your MBox/EML files into the container:
services:
mailarchive-app:
volumes:
- /home/user/mbox-archives:/data/import
- ./data-protection-keys:/app/DataProtection-Keys
Replace /home/user/mbox-archives with the actual path on your Docker host.
3. Restart the Container¶
📝 Usage¶
Find Your Target Account ID¶
Before importing, you need the database ID of the target mail account. You can find it via the Web UI:
Navigate to "Email Accounts" open the destination account details, the account ID is displayed in the account list or in the URL when viewing an account.
Import an MBox File¶
docker compose exec mailarchive-app dotnet MailArchiver.dll --import-mbox --file /data/import/myarchive.mbox --account-id 1 --folder INBOX
Parameters:
| Parameter | Required | Description |
|-----------|----------|-------------|
| --import-mbox | Yes | Signals an MBox import operation |
| --file <path> | Yes | Path to the MBox file inside the container |
| --account-id <id> | Yes | Database ID of the target mail account |
| --folder <name> | No | Target folder name (default: INBOX) |
Import EML Files (ZIP Archive)¶
docker compose exec mailarchive-app dotnet MailArchiver.dll --import-eml --file /data/import/emails.zip --account-id 1
Parameters:
| Parameter | Required | Description |
|-----------|----------|-------------|
| --import-eml | Yes | Signals an EML import operation |
| --file <path> | Yes | Path to a ZIP archive containing .eml files inside the container |
| --account-id <id> | Yes | Database ID of the target mail account |
Important:
--import-emlexpects a ZIP archive containing.emlfiles — it does not accept individual.emlfiles. To import single EML files, wrap them in a ZIP archive first:Note for
Thedotnet run: When usingdotnet run(instead of the published DLL), separate dotnet's arguments from the application's arguments with--:--tellsdotnet runthat all following arguments belong to your application, not to the .NET compiler.
📊 Example Output¶
Target account: user@example.com (ID: 1)
=== Local MBox Import ===
File: /data/import/myarchive.mbox
Size: 245.32 MB
Target Account ID: 1
Target Folder: INBOX
[Importing... progress logged to container logs]
=== Import Results ===
Status: Completed
Total Emails: 15234
Imported Successfully: 15120
Failed: 42
Skipped (malformed): 28
Skipped (duplicates): 44
Duration: 00:12:45
🔍 Monitoring¶
During import, detailed progress is written to the container logs:
⚙️ Configuration Reference¶
| Environment Variable | Default | Description |
|---|---|---|
LocalImport__AllowedPaths__0 |
/data/import |
First allowed path for local imports |
LocalImport__AllowedPaths__1 |
(empty) | Second allowed path (optional) |
Add more paths by incrementing the index (__2, __3, etc.).
🚫 Error Handling¶
| Error | Cause | Solution |
|---|---|---|
File not found |
The --file path does not exist in the container |
Verify the file path and volume mount |
File path is not in an allowed import directory |
The file is outside whitelisted paths | Either move the file into an allowed path or add its directory to LocalImport:AllowedPaths |
Mail account with ID X not found |
Wrong account ID | Verify the account ID via the Web UI or database query |
Invalid account-id |
Non-numeric value for --account-id |
Use a numeric database ID |
🔒 Security Best Practices¶
-
Use read-only mounts when possible:
-
Restrict AllowedPaths to only the directories you actually use.
-
Remove volume mounts after import is complete to maintain a clean security surface.
-
Never expose the import directory via the web server – there is no web endpoint for local imports.
📦 Import Formats at a Glance¶
| CLI Flag | Input Format | Typical File Extension | Single Email | Bulk |
|---|---|---|---|---|
--import-mbox |
MBox file | .mbox, .mbx |
❌ (always bulk) | ✅ |
--import-eml |
ZIP archive of EMLs | .zip |
❌ (ZIP required) | ✅ |
--migrate-raw-storage |
Stored original messages | — | — | ✅ |
Single .eml files are not directly supported. Use zip to wrap them, or import via the web upload.
🔧 Maintenance Commands¶
Moving Stored Original Messages¶
When RawMessages:Enabled is on, the original message of each email is kept either in the database or in files, depending on RawMessages:Storage. This command moves the already-stored originals from one backend to the other; see Original Message Storage for the configuration.
# database -> filesystem
docker compose exec mailarchiver dotnet MailArchiver.dll \
--migrate-raw-storage --from postgres --to filesystem
# filesystem -> database
docker compose exec mailarchiver dotnet MailArchiver.dll \
--migrate-raw-storage --from filesystem --to postgres
| Argument | Default | Description |
|---|---|---|
--from |
configured RawMessages:Storage |
Backend the messages are moved out of. |
--to |
configured RawMessages:Storage |
Backend the messages are moved into. |
--batch |
200 |
Messages per database transaction. |
Unlike the import commands this one is not restricted by LocalImport:AllowedPaths: it reads only the message files that the archive itself wrote under RawMessages:FileSystem:BasePath.
What it guarantees:
- Each message is verified against the checksum recorded at archive time before it is copied. A message that fails is left exactly where it is and reported — a corrupt original is never propagated and its only remaining copy is never deleted.
- A file is deleted only after the database transaction that moved it has committed, so an interrupted run cannot lose the only copy.
- The run is resumable: work is selected by the storage recorded on each message, so starting the command again continues with what is still outstanding.
- One bad message does not abort the run. The exit code is
1if any message failed,0otherwise.
Example output:
Batch: 200 moved, 0 skipped, 0 failed
Batch: 200 moved, 0 skipped, 0 failed
Batch: 42 moved, 0 skipped, 1 failed
Done: 442 moved, 1 failed.
Email 12345: checksum mismatch (recorded 4f1c..., file has 9ab2...)
Reads always follow the backend recorded on each message, not the configured setting, so a download keeps working during and after a migration. Changing RawMessages:Storage only affects newly archived mail — run this command to move what is already stored.
Backup: with the filesystem backend the message files live outside the database. Back up
RawMessages:FileSystem:BasePathtogether with the database dump; a restore without the files leaves originals that cannot be served.
❓ FAQ¶
Q: Why does --import-eml require a ZIP file?
A: The underlying EML import service (EmlImportService) processes ZIP archives internally using .NET's ZipFile API. This design supports bulk imports efficiently. For individual .eml files, wrap them in a ZIP archive (e.g., zip emails.zip *.eml) or use the web upload.
Q: Why can't I browse files from the web UI? A: This is a deliberate security design. This function is intentionally implemented via CLI for system-level interactions because normal users do not have direct access to the OS level; they only interact with the Web UI.
To prevent security risks such as path traversal attacks and unauthorized filesystem exposure, regular users are restricted to using the Web UI for file uploads and imports. This ensures they cannot bypass the intended interface to access the underlying container filesystem.