s365b.modules.exchange.module¶
The Exchange Online backup module: mail, calendars and contacts.
Each mailbox is a protected object. Messages are stored as their original
MIME (.eml), which keeps headers, body and attachments exactly as
Exchange has them and opens in any mail program.
Incremental backups use one delta query per folder. Graph is asked for
immutable IDs (Prefer: IdType="ImmutableId"), so a message keeps its ID
when it moves between folders: a move becomes a new version with the new
folder instead of a deletion plus a new item.
Each message also gets a restore copy: Exchange’s own export of it
(mailbox import/export API), which restores with full fidelity (read state,
dates, flags, not a draft). It is fetched after each mailbox’s backup for
every message that lacks one, so older backups get it too. Restore imports
it (s365b.modules.exchange.restore).
Calendars, contacts and tasks (Microsoft To Do) are stored as Graph’s JSON,
with restore copies like messages (s365b.modules.exchange.items,
s365b.modules.exchange.tasks).
Not covered by Graph (v1.0), and so not backed up: in-place archive mailboxes, the Recoverable Items folder, public folders.
Attributes¶
Classes¶
A mail folder to back up. |
|
Exchange module settings. |
|
Backs up mail in Exchange Online mailboxes. |
Functions¶
|
Pick the fields shown and searched in the browse view. |
|
Return a user's mailbox ID for the mailbox import/export API. |
|
Group messages into export requests: at most 20, and not too large. |
|
Match an export response to the messages asked for. |
Module Contents¶
- s365b.modules.exchange.module.FOLDER_FIELDS = 'id,displayName,parentFolderId,childFolderCount,totalItemCount'[source]¶
- s365b.modules.exchange.module.NO_MAILBOX = ('MailboxNotEnabledForRESTAPI', 'MailboxNotFound', 'ErrorInvalidUser', 'ResourceNotFound')[source]¶
- class s365b.modules.exchange.module.ExchangeConfig[source]¶
Bases:
pydantic.BaseModelExchange module settings.
- s365b.modules.exchange.module.message_metadata(msg: dict[str, Any], folder_id: str) dict[str, Any][source]¶
Pick the fields shown and searched in the browse view.
- Parameters:
msg – Message from Graph.
folder_id – The folder it is in.
- Returns:
Metadata for the item version.
- class s365b.modules.exchange.module.ExchangeModule[source]¶
Bases:
s365b.modules.sdk.BackupModuleBacks up mail in Exchange Online mailboxes.
- description: ClassVar[str] = 'Email, calendars and contacts in user and shared mailboxes; email as original .eml files.'[source]¶
- async estimate(ctx: s365b.modules.sdk.ModuleContext, obj: s365b.modules.sdk.ProtectedObject, state: s365b.modules.sdk.SyncState) int[source]¶
Count the messages in folders that have no finished first backup.
Folders already backed up only send changes, which are few and not counted.
- Parameters:
ctx – Module context.
obj – The mailbox.
state – Its saved state.
- Returns:
Expected number of messages to process.
- state_for_new_repository(state: s365b.modules.sdk.SyncState) s365b.modules.sdk.SyncState[source]¶
Re-list every mail folder; keep the folder lists.
Each mail folder restarts its delta query as a re-listing, which afterwards finds the messages deleted meanwhile. Calendars, contacts and tasks are always listed in full; their saved folder names still find renames and removed folders.
- Parameters:
state – The state saved by the backup into the other repository.
- Returns:
The state to start from.
- required_permissions(config: pydantic.BaseModel, *, restore: bool) set[str][source]¶
Return the Graph application permissions needed.
- Parameters:
config – Module settings.
restore – True for the Restore app.
- Returns:
Read and export permissions for backup (calendars and contacts when included); folder and import permissions for restore.
- optional_permissions(config: pydantic.BaseModel) set[str][source]¶
Back up without the export permission (no restore copies).
- Parameters:
config – Module settings.
- Returns:
The export permission.
- extra_copies(config: pydantic.BaseModel) tuple[str, ...][source]¶
Keep Exchange’s export of every message, for restore.
- Parameters:
config – Module settings.
- Returns:
The restore copy.
- extra_label(name: str) str[source]¶
Progress text while fetching restore copies.
- Parameters:
name – Copy name.
- Returns:
Text for the progress line.
- async fetch_extras(ctx: s365b.modules.sdk.ModuleContext, obj: s365b.modules.sdk.ProtectedObject, name: str, items: list[s365b.modules.sdk.ExtraWanted]) collections.abc.AsyncIterator[tuple[str, bytes]][source]¶
Export messages with Exchange’s mailbox export API.
- Parameters:
ctx – Module context.
obj – The mailbox.
name –
"fts".items – Messages (immutable IDs) to export.
- Yields:
(message ID, export stream)for each message exported.- Raises:
ModuleError – If the Backup app may not export.
- async restore(ctx: s365b.modules.sdk.ModuleContext, request: s365b.modules.sdk.RestoreRequest) s365b.modules.sdk.RestoreResult[source]¶
Import messages’ restore copies into a mailbox.
See
s365b.modules.exchange.restore.restore_mail()for the options.- Parameters:
ctx – Module context (Restore app).
request – Messages and where to put them.
- Returns:
The outcome.
- item_types() list[s365b.modules.sdk.ItemTypeInfo][source]¶
Describe the item types.
- Returns:
Email messages, calendar events and contacts.
- async export(config: pydantic.BaseModel, request: s365b.modules.sdk.ExportRequest) collections.abc.AsyncIterator[s365b.modules.sdk.ExportFile][source]¶
Events and tasks as one iCalendar file, contacts as vCards, messages as .eml.
Options:
name, the file’s name, and foricsthe calendar’s name in it (e.g. the calendar or To Do list exported).- Parameters:
config – Module settings.
request – The items; only events and tasks (
ics), contacts (vcf) or messages (eml) are exported.
- Yields:
One
.icsor.vcffile, or an.emlfile per message in its folder (read when the file is written).- Raises:
NotSupportedError – For another format.
- async discover(ctx: s365b.modules.sdk.ModuleContext) collections.abc.AsyncIterator[s365b.modules.sdk.ProtectedObject][source]¶
Yield every mailbox the settings include.
Users without a mailbox (no licence, guests) are left out.
- Parameters:
ctx – Module context.
- Yields:
Mailboxes.
- Raises:
ModuleError – If mailbox settings cannot be read at all.
- async backup(ctx: s365b.modules.sdk.ModuleContext, obj: s365b.modules.sdk.ProtectedObject, state: s365b.modules.sdk.SyncState) collections.abc.AsyncIterator[s365b.modules.sdk.ItemVersion | s365b.modules.sdk.GroupChange | s365b.modules.sdk.Checkpoint][source]¶
Back up one mailbox incrementally.
State:
{"folders": {folder_id: {"path", "delta", "next"}}}.- Parameters:
ctx – Module context.
obj – The mailbox.
state – Saved state.
- Yields:
Messages, folder renames/removals and checkpoints.
- Raises:
ModuleError – If the mailbox cannot be read.
- async s365b.modules.exchange.module.mailbox_id(ctx: s365b.modules.sdk.ModuleContext, user: str) str[source]¶
Return a user’s mailbox ID for the mailbox import/export API.
- Parameters:
ctx – Module context.
user – User ID.
- Returns:
The primary mailbox ID, e.g.
MBX:…@….
- s365b.modules.exchange.module.export_batches(items: list[s365b.modules.sdk.ExtraWanted]) list[list[s365b.modules.sdk.ExtraWanted]][source]¶
Group messages into export requests: at most 20, and not too large.
- Parameters:
items – Messages with their sizes.
- Returns:
Batches; a message larger than the size limit goes alone.
- s365b.modules.exchange.module.exported(batch: list[s365b.modules.sdk.ExtraWanted], results: list[dict[str, Any]], ctx: s365b.modules.sdk.ModuleContext) list[tuple[str, bytes]][source]¶
Match an export response to the messages asked for.
Results carry the item ID; if Exchange returns IDs in another format, they are matched by position (one result per ID asked for, in order).
- Parameters:
batch – Messages asked for.
results – The response’s
value.ctx – Module context, for logging.
- Returns:
(message ID, export stream)for each message exported.