Moving accounts
With the proxy live and every account still served by the source, the accounts can move, one at a time. Each move has four parts. Vandelay copies the account’s data while the account stays in use on the source. The account is created on the new deployment. Its mapping entry is switched, so that the proxy routes it to the new deployment. Finally, Vandelay copies what changed on the source in the meantime. The switch closes the account’s open sessions, and they reconnect to the new deployment.
vandelay migrate has no connector for a Stalwart source, so these commands are the only path for such accounts. The procedure below moves the account [email protected], and is repeated for every account. The proxy destinations are named old for the source and new for the new deployment, as in the proxy example.
Order of the steps
Section titled “Order of the steps”The steps of an account move are ordered so that no mail and no change is lost:
- Initial copy. Vandelay imports the account from the source into an archive. This is the longest step. The account does not exist on the new deployment yet, so the new deployment keeps relaying its inbound mail to the source.
- Account creation. The account is created on the new deployment. From this moment, the new deployment considers the address local and delivers the account’s inbound mail locally, while the user still reads mail on the source. This mail becomes visible to the user only at the switch, so keep steps 2 to 4 short.
- Export. Vandelay writes the archive to the new account.
- Switch. The account’s mapping entries are pointed at the new deployment, and its sessions are closed so that its clients reconnect there.
- Catch-up. The import and the export run again, and copy what changed on the source between the initial copy and the switch. After the switch, the user no longer writes to the source, so nothing written there is lost.
Prepare the tools
Section titled “Prepare the tools”The commands below run on the Vandelay host. They need the proxy’s management API and the passwords of two administrators, kept in environment variables so that they do not appear on command lines.
The proxy example binds the management API to 127.0.0.1:9443 on the proxy host, so the Vandelay host reaches it through an SSH tunnel to the proxy host, here at 203.0.113.10. The token file, /etc/proxy/admin-token, is readable only by the stalwart-proxy account, so the SSH account on the proxy host reads it with sudo, which must not ask for a password in a non-interactive SSH command:
ssh -f -N -L 9443:127.0.0.1:9443 203.0.113.10TOKEN=$(ssh 203.0.113.10 sudo cat /etc/proxy/admin-token)PROXY=https://127.0.0.1:9443/api/v1export SOURCE_ADMIN_PASSWORD='<source administrator password>'export NEW_ADMIN_PASSWORD='<new deployment administrator password>'On the proxy host itself, the tunnel is not needed, and the token is read with sudo cat /etc/proxy/admin-token.
Both servers advertise the public server name as their URL. Vandelay’s JMAP requests to either server therefore pass through the proxy, which routes them by the administrator’s login, like the requests of any other account. A mapping entry for the administrator account decides which server they reach: it points at the source while data is read from it, and at the new deployment while data is written to it. A small shell function switches it:
admin_route() { curl -sk -X PUT -H "Authorization: Bearer $TOKEN" \ -d "{\"destination\":\"$1\"}" "$PROXY/mappings/admin"}Each Vandelay run below calls admin_route old or admin_route new first. The entry disappears with the proxy at the end of the migration.
Internal server names often present a self-signed certificate, or one issued by an internal authority. Installing that authority on the Vandelay host lets every tool verify the certificate; otherwise Vandelay needs --allow-invalid-certs, stalwart-cli needs -k, the account plan scripts need --insecure and curl needs -k.
Make the initial copy
Section titled “Make the initial copy”The initial copy reads the account from the source into a local archive file, one file per account. The archive is kept until the account has been validated on the new deployment, as a local copy of everything that was moved.
The account is read over JMAP as the source’s administrator, acting on Alice’s account, and named as the source knows it. A Stalwart 0.16 source names every account by its address, [email protected], while a Stalwart 0.15 source with bare login names uses the bare name instead:
admin_route oldVANDELAY_PASSWORD="$SOURCE_ADMIN_PASSWORD" vandelay import jmap \ --url https://mail.example.org \ --auth-basic admin \ --objects mailbox,email,identity,sievescript \ --objects addressbook,contactcard \ --objects calendar,calendarevent,participantidentity \ alice.sqliteThis copies mail, mailboxes, identities, Sieve scripts and the vacation response, contacts, calendars and the calendar participant identities (the addresses the account organises and attends events as). --objects can be repeated, and the lists are merged in order.
Files are left out of the JMAP import with --objects, for a specific reason. Stalwart 0.15 and 0.16 advertise the urn:ietf:params:jmap:filenode capability, but implement earlier drafts of the JMAP file storage specification, which Vandelay does not follow. Reading their files over JMAP therefore fails, and the run ends with exit code 5. The files are read over WebDAV instead, which works with any Stalwart release, into the same archive. The path contains the account name as the source knows it, percent-encoded when it is an address:
VANDELAY_PASSWORD="$SOURCE_ADMIN_PASSWORD" vandelay import webdav \ --url https://mail.example.org/dav/file/alice%40example.org/ \ --auth-basic admin \ alice.sqliteTo check what the archive holds, run vandelay inspect alice.sqlite. It contacts no server, and prints the number of objects of each type. inspect never creates an archive: a mistyped path ends with exit code 7, and an empty file with archive is empty: nothing has been imported into it.
Create the account on the new deployment
Section titled “Create the account on the new deployment”The account is recreated from its definition on the source, with its password hash, so that the user keeps the existing password.
The proxy repository ships a script for each release that does this in two stages. The script reads the account from the source and writes a plan for stalwart-cli apply, and the CLI then creates the account on the new deployment from the plan. The scripts are migrate_v015_to_plan.py for Stalwart 0.15 and migrate_v016_to_plan.py for Stalwart 0.16. Both need Python 3 and no other modules. Both read the source at its internal address, so they need no administrator mapping entry.
The plan references the account’s domain on the new deployment, in one of two ways. Without further options, the 0.16 script’s plan contains an upsert of the domain by name, which finds the domain created when the new deployment was prepared without changing it, and creates a domain that is missing, such as one used only by an alias. The 0.15 script’s plan creates every domain it references instead, so on a 0.15 source each domain already created on the new deployment is passed with --existing-domain. With --existing-domain example.org=<domain-id>, the plan refers to the domain by the identifier that stalwart-cli query Domain --where name=example.org --fields id prints.
On a Stalwart 0.15 source, the script reads the account through the management API, named as the source knows it:
python3 migrate_v015_to_plan.py alice \ --url https://source.internal.example.org \ --user admin --password "$SOURCE_ADMIN_PASSWORD" \ --output alice-account.ndjsonThe plan creates the account with its password hashes, its description and its additional addresses as aliases, and gives it the User roles. App passwords and two-factor authentication secrets are not migrated.
On a Stalwart 0.16 source, the script reads the account over JMAP, and is given the account’s address. A Stalwart 0.16 server returns password hashes only when its environment contains STALWART_INCLUDE_SECRETS=1, which is added when going live, before the source restarts. Without it, the script stops with the source returned a masked password; restart it with STALWART_INCLUDE_SECRETS=1 in its environment. The script reads the administrator’s password from SOURCE_ADMIN_PASSWORD:
--url https://source.internal.example.org \ --user admin \ --output alice-account.ndjsonThe plan creates the account with its password hashes and two-factor authentication secret, its description, quotas, locale, time zone and aliases. The script prints a warning for each property it does not carry over:
- App passwords and API keys
- Group and tenant memberships
- The identifier of an external directory
- Custom roles, which are replaced by the default roles
- Encryption at rest, which is disabled until the user adds a public key again on the new deployment
Account-level permissions are copied, and also reported, so that they can be checked on the new deployment. Two options extend the script: --include-groups also exports groups, and --output-dir <dir> writes one plan per account in a single run, for every account or for the addresses given.
Check the plan with a dry run, then apply it to the new deployment:
stalwart-cli apply --file alice-account.ndjson --dry-runstalwart-cli apply --file alice-account.ndjsonA plan contains the password hashes of the account. Keep it only as long as the account move requires, and protect it like the hashes themselves. The 0.16 script creates its output files readable only by their owner; the 0.15 script creates them with the permissions of the shell’s umask, so run it after umask 077.
One step remains before the account can be used. The new deployment caches the fact that an address does not exist, so an account created a moment ago can appear to be unknown until that cache entry expires. Invalidating the negative caches makes the account known at once:
stalwart-cli create Action --json '{"@type":"InvalidateNegativeCaches"}'Skipping this step is the most common reason why a new account appears to reject its correct password. Then confirm that the account works, by signing in once against the new deployment’s internal address, before its mapping is switched:
openssl s_client -quiet -crlf -connect new.internal.example.org:993 <<'EOF'a1 LOGIN [email protected] "password"a2 LOGOUTEOFRun this test from a host outside the proxy’s trusted network, such as the Vandelay host. The new deployment drops connections from the proxy’s address that do not start with a PROXY header, so the test would fail from the proxy host.
Write the archive to the new deployment
Section titled “Write the archive to the new deployment”The archive is written to the new account as the new deployment’s administrator, acting on the account, which the new deployment names by its address:
admin_route newVANDELAY_PASSWORD="$NEW_ADMIN_PASSWORD" vandelay export \ --url https://mail.example.org \ --auth-basic admin \ alice.sqliteThe new account already has role mailboxes, and the export matches the archived mailboxes with them, so the inbox, sent, drafts, junk and trash folders are reused, not duplicated. The export also activates the Sieve script that was active on the source, and writes the archived vacation response. Stalwart reserves the script name vacation for the vacation response, so when the archive holds a vacation response and the new deployment supports it, an archived script of that name, in any case, is skipped and counted as skipped, because the vacation response replaces it. A source script named managed, the name the new deployment reserves for its own script, is exported under a new name, and the warning names both.
An export can be repeated safely. Objects already present on the target are matched and not created again, so a repeated run after an interruption completes the copy without duplicates.
Switch the account
Section titled “Switch the account”The account now holds on the new deployment what the source held at the initial copy, and it can be switched. The switch does two things for each login form, the address and, when the source accepted it, the bare login name: it points the account’s mapping entry at the new deployment, and it closes the account’s sessions, so that its clients reconnect through the new mapping:
curl -sk -X PUT -H "Authorization: Bearer $TOKEN" \ -d '{"destination":"new"}' "$PROXY/mappings/$id" curl -sk -X POST -H "Authorization: Bearer $TOKEN" \ -d "{\"identifier\":\"$id\"}" "$PROXY/connections/kick"doneA mapping change made through the management API takes effect for the account’s next connection. A kick closes only the sessions of the exact identifier given, which is why both login forms are kicked.
From now on, the account is served by the new deployment for every protocol. Its inbound mail, which the new deployment has delivered locally since the account was created, becomes visible to the user.
Copy what changed
Section titled “Copy what changed”Once the account is switched, the user no longer reaches the source, so nothing more is written to the account there. What remains is the work done on the source between the initial copy and the switch: messages that arrived before the account was created on the new deployment, sent messages, flag changes and folder moves, and new events, contacts, Sieve scripts and files.
Run the import and the export again, with the same archive as before, calling admin_route old before the import and admin_route new before the export. Add --update-keywords to the export, so that flags and folders of messages that were already copied follow the changes made on the source:
admin_route oldVANDELAY_PASSWORD="$SOURCE_ADMIN_PASSWORD" vandelay import jmap \ --url https://mail.example.org \ --auth-basic admin \ --objects mailbox,email,identity,sievescript \ --objects addressbook,contactcard \ --objects calendar,calendarevent,participantidentity \ alice.sqliteVANDELAY_PASSWORD="$SOURCE_ADMIN_PASSWORD" vandelay import webdav \ --url https://mail.example.org/dav/file/alice%40example.org/ \ --auth-basic admin \ alice.sqlite
admin_route newVANDELAY_PASSWORD="$NEW_ADMIN_PASSWORD" vandelay export \ --url https://mail.example.org \ --auth-basic admin \ --update-keywords \ alice.sqliteThe repeated import reads only what changed on the source, and the repeated export adds what is missing on the new deployment. Items from that interval appear a few minutes after the user has reconnected.
The catch-up has limits. Edited events, contacts and files that were already copied keep their copied version, and objects deleted on the source since the initial copy remain on the new deployment. Identities and Sieve scripts are the exception: the export brings them in line with the archive when they changed on the source, as described in Matched objects. Keeping the time between the initial copy and the switch short keeps the differences small.
The vacation response is overwritten by every export. A catch-up run after the switch therefore replaces a vacation response that the user has set on the new deployment in the meantime.
The export’s --prune option is not used here. It would delete folders, calendars, address books, contacts, events, Sieve scripts and files that the archive does not describe, including those that the user has already created on the new deployment.
Validate the account
Section titled “Validate the account”Validation confirms two things: that the account works, and that its data arrived complete. Validate each account before the next one moves, because an account is easier to roll back before its user has worked on the new deployment.
The first check is an IMAP session through the proxy, with the account’s password. It lists the migrated mailboxes and the number of messages in the inbox:
openssl s_client -quiet -crlf -connect mail.example.org:993 <<'EOF'a1 LOGIN [email protected] "password"a2 LIST "" "*"a3 STATUS INBOX (MESSAGES)a4 LOGOUTEOFThe second check compares the archive with the new deployment. A repeated export with --dry-run prints an export plan with, for each data type, the archived objects already matched on the new deployment and those that would still be created. After a complete copy, nothing remains to be created:
admin_route newVANDELAY_PASSWORD="$NEW_ADMIN_PASSWORD" vandelay export --dry-run \ --url https://mail.example.org \ --auth-basic admin \ alice.sqliteTotals per type can also be read over JMAP. The account identifier is the value of urn:ietf:params:jmap:mail in the primaryAccounts object of the session at /.well-known/jmap. A query with calculateTotal then returns the number of messages in the account, without listing them:
https://mail.example.org/jmap -d '{ "using": ["urn:ietf:params:jmap:core", "urn:ietf:params:jmap:mail"], "methodCalls": [["Email/query", {"accountId": "<account id>", "calculateTotal": true, "limit": 1}, "0"]]}'The final check is a real one: open the account in a mail client and in the WebUI, read existing mail, and send a message to the account.
Login names after migration
Section titled “Login names after migration”The new deployment identifies accounts by their address. A user who signed in to the source with a bare name, such as alice, can keep doing so with the mail protocols: with IMAP, POP3, SMTP submission, ManageSieve and JMAP, the new deployment completes a bare login with its default domain, set as described in Bare login names. The mapping entries for both forms, created at the switch, keep either login routed to the new deployment through the proxy.
CalDAV, CardDAV and WebDAV clients are the exception, because their URLs contain the account name. On the new deployment, the account segment of a collection URL is either one of the account’s addresses, percent-encoded, as in /dav/cal/alice%40example.org/, or the account’s numeric identifier in the form _<id>. A bare name does not resolve, and sub-addresses and catch-all addresses are not applied, so a client configured with /dav/cal/alice/ receives 404 Not Found after the switch.
Such clients are reconfigured with the address form of the URL, or set up again from the server name, in which case they discover their collections through /.well-known/caldav and /.well-known/carddav, which redirect to /dav/cal and /dav/card. The proxy cannot do this for them: it routes these requests by their credentials, but it does not rewrite paths. Users can be asked to sign in with their full address from now on, but nothing forces an immediate change for the mail protocols.
Move many accounts
Section titled “Move many accounts”The procedure is the same for every account, so it is usually run from a script over a list of accounts. The following skeleton runs the sequence for each account in a file with one address per line; migrate_account stands for the commands of this page, with the address as its argument:
while read -r account; do migrate_account "$account" >"logs/$account.log" 2>&1 \ && echo "$account done" || echo "$account FAILED"done < accounts.txtAccounts from a Stalwart source are moved one at a time. The single admin mapping entry switches every Vandelay run between the two servers, so parallel moves would send each other’s runs to the wrong server. Each Vandelay run already uses several connections, set by -j/--threads, and each account has its own archive, on which vandelay inspect shows how far that account’s copy got.
The switches of a batch of accounts can be made at once. With the file mapping store, the entries are appended to the mapping file, which is then reloaded in one call; with a Redis or SQL mapping store, they are written to the store directly. The second call clears the proxy’s mapping cache, so that the new entries take effect at once:
curl -sk -X POST -H "Authorization: Bearer $TOKEN" "$PROXY/mappings/reload"curl -sk -X POST -H "Authorization: Bearer $TOKEN" "$PROXY/cache/invalidate"Then close each switched account’s sessions with a kick, as in the switch above.
Mail between the two groups of accounts needs attention while the migration runs. Mail that users still on the source send to accounts that have already moved is delivered on the source, where the moved accounts still exist, and does not reach the new deployment. The catch-up import and export are therefore repeated for the moved accounts at intervals, for example after each batch, and once more for every account after the last one has moved. Each repeated run copies only what arrived on the source since the previous one.
Rollback
Section titled “Rollback”An account can be returned to the source at any time before the source is decommissioned, because the migration only copied its data. The immediate rollback is the switch in reverse: it points the account’s mapping entries back at the source and closes its sessions:
curl -sk -X PUT -H "Authorization: Bearer $TOKEN" \ -d '{"destination":"old"}' "$PROXY/mappings/$id" curl -sk -X POST -H "Authorization: Bearer $TOKEN" \ -d "{\"identifier\":\"$id\"}" "$PROXY/connections/kick"doneThe mapping is only half of the rollback, because inbound mail does not follow it. As long as the account exists on the new deployment, the new deployment keeps delivering the account’s inbound mail locally. Deleting the account on the new deployment, after its data has been carried back, makes the new deployment relay the account’s mail to the source again. Removing the account’s permissions instead is no substitute: it keeps the address local, and mail for it is rejected with This account is not authorized to receive email.
Mail and changes that the account received on the new deployment after the switch exist only there. They are carried back by running Vandelay in the other direction, reading the account from the new deployment and writing it to the source:
admin_route newVANDELAY_PASSWORD="$NEW_ADMIN_PASSWORD" vandelay import jmap \ --url https://mail.example.org --auth-basic admin \
admin_route oldVANDELAY_PASSWORD="$SOURCE_ADMIN_PASSWORD" vandelay export \ --url https://mail.example.org --auth-basic admin \The account is named as the source knows it, as for the initial copy. This carries back mail, mailboxes, Sieve scripts, contacts, calendars and files. On a Stalwart 0.15 source, files are the exception: Stalwart 0.15 does not accept the files that Vandelay writes over JMAP, so files created or changed on the new deployment after the switch are copied back to the source by hand, over WebDAV.
A rollback is simplest shortly after the switch, before much new data has accumulated on the new deployment. Once the account’s data has been carried back, delete the account on the new deployment, so that its mail is relayed to the source again. A later attempt starts again with a new initial copy.