s365b.modules.exchange.module ============================= .. py:module:: s365b.modules.exchange.module .. autoapi-nested-parse:: 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 (:mod:`s365b.modules.exchange.restore`). Calendars, contacts and tasks (Microsoft To Do) are stored as Graph's JSON, with restore copies like messages (:mod:`s365b.modules.exchange.items`, :mod:`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 ---------- .. autoapisummary:: s365b.modules.exchange.module.READ_PERMISSIONS s365b.modules.exchange.module.CALENDAR_PERMISSION s365b.modules.exchange.module.CONTACTS_PERMISSION s365b.modules.exchange.module.TASKS_PERMISSION s365b.modules.exchange.module.RESTORE_PERMISSIONS s365b.modules.exchange.module.FTS s365b.modules.exchange.module.EXPORT_MAX_ITEMS s365b.modules.exchange.module.EXPORT_MAX_BYTES s365b.modules.exchange.module.IMMUTABLE s365b.modules.exchange.module.PAGE s365b.modules.exchange.module.MESSAGE_FIELDS s365b.modules.exchange.module.FOLDER_FIELDS s365b.modules.exchange.module.DOWNLOAD_AHEAD s365b.modules.exchange.module.PARALLEL_MAILBOXES s365b.modules.exchange.module.NO_MAILBOX s365b.modules.exchange.module.Folder Classes ------- .. autoapisummary:: s365b.modules.exchange.module.FolderInfo s365b.modules.exchange.module.ExchangeConfig s365b.modules.exchange.module.ExchangeModule Functions --------- .. autoapisummary:: s365b.modules.exchange.module.message_metadata s365b.modules.exchange.module.mailbox_id s365b.modules.exchange.module.export_batches s365b.modules.exchange.module.exported Module Contents --------------- .. py:data:: READ_PERMISSIONS .. py:data:: CALENDAR_PERMISSION :value: 'Calendars.Read' .. py:data:: CONTACTS_PERMISSION :value: 'Contacts.Read' .. py:data:: TASKS_PERMISSION :value: 'Tasks.Read.All' .. py:data:: RESTORE_PERMISSIONS .. py:data:: FTS :value: 'fts' .. py:data:: EXPORT_MAX_ITEMS :value: 20 .. py:data:: EXPORT_MAX_BYTES :value: 25165824 .. py:data:: IMMUTABLE .. py:data:: PAGE .. py:data:: MESSAGE_FIELDS :value: '' .. py:data:: FOLDER_FIELDS :value: 'id,displayName,parentFolderId,childFolderCount,totalItemCount' .. py:data:: DOWNLOAD_AHEAD :value: 4 .. py:data:: PARALLEL_MAILBOXES :value: 4 .. py:data:: NO_MAILBOX :value: ('MailboxNotEnabledForRESTAPI', 'MailboxNotFound', 'ErrorInvalidUser', 'ResourceNotFound') .. py:data:: Folder .. py:class:: FolderInfo A mail folder to back up. .. py:attribute:: path :type: str .. py:attribute:: count :type: int .. py:class:: ExchangeConfig Bases: :py:obj:`pydantic.BaseModel` Exchange module settings. .. py:attribute:: include_shared_mailboxes :type: bool .. py:attribute:: include_resource_mailboxes :type: bool .. py:attribute:: include_junk_email :type: bool .. py:attribute:: include_deleted_items :type: bool .. py:attribute:: include_calendar :type: bool .. py:attribute:: include_contacts :type: bool .. py:attribute:: include_tasks :type: bool .. py:function:: message_metadata(msg: dict[str, Any], folder_id: str) -> dict[str, Any] Pick the fields shown and searched in the browse view. :param msg: Message from Graph. :param folder_id: The folder it is in. :returns: Metadata for the item version. .. py:class:: ExchangeModule Bases: :py:obj:`s365b.modules.sdk.BackupModule` Backs up mail in Exchange Online mailboxes. .. py:attribute:: id :type: ClassVar[str] :value: 'exchange' .. py:attribute:: display_name :type: ClassVar[str] :value: 'Exchange Online' .. py:attribute:: description :type: ClassVar[str] :value: 'Email, calendars and contacts in user and shared mailboxes; email as original .eml files.' .. py:attribute:: version :type: ClassVar[str] :value: '0.1.0' .. py:attribute:: config_model :type: ClassVar[type[pydantic.BaseModel]] .. py:attribute:: object_noun :type: ClassVar[tuple[str, str]] :value: ('mailbox', 'mailboxes') .. py:attribute:: parallel_objects :type: ClassVar[int] :value: 4 .. py:method:: estimate(ctx: s365b.modules.sdk.ModuleContext, obj: s365b.modules.sdk.ProtectedObject, state: s365b.modules.sdk.SyncState) -> int :async: Count the messages in folders that have no finished first backup. Folders already backed up only send changes, which are few and not counted. :param ctx: Module context. :param obj: The mailbox. :param state: Its saved state. :returns: Expected number of messages to process. .. py:method:: state_for_new_repository(state: s365b.modules.sdk.SyncState) -> s365b.modules.sdk.SyncState 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. :param state: The state saved by the backup into the other repository. :returns: The state to start from. .. py:method:: required_permissions(config: pydantic.BaseModel, *, restore: bool) -> set[str] Return the Graph application permissions needed. :param config: Module settings. :param restore: True for the Restore app. :returns: Read and export permissions for backup (calendars and contacts when included); folder and import permissions for restore. .. py:method:: optional_permissions(config: pydantic.BaseModel) -> set[str] Back up without the export permission (no restore copies). :param config: Module settings. :returns: The export permission. .. py:method:: extra_copies(config: pydantic.BaseModel) -> tuple[str, ...] Keep Exchange's export of every message, for restore. :param config: Module settings. :returns: The restore copy. .. py:method:: extra_label(name: str) -> str Progress text while fetching restore copies. :param name: Copy name. :returns: Text for the progress line. .. py:method:: 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]] :async: Export messages with Exchange's mailbox export API. :param ctx: Module context. :param obj: The mailbox. :param name: ``"fts"``. :param items: Messages (immutable IDs) to export. :Yields: ``(message ID, export stream)`` for each message exported. :raises ModuleError: If the Backup app may not export. .. py:method:: restore(ctx: s365b.modules.sdk.ModuleContext, request: s365b.modules.sdk.RestoreRequest) -> s365b.modules.sdk.RestoreResult :async: Import messages' restore copies into a mailbox. See :func:`s365b.modules.exchange.restore.restore_mail` for the options. :param ctx: Module context (Restore app). :param request: Messages and where to put them. :returns: The outcome. .. py:method:: item_types() -> list[s365b.modules.sdk.ItemTypeInfo] Describe the item types. :returns: Email messages, calendar events and contacts. .. py:method:: export(config: pydantic.BaseModel, request: s365b.modules.sdk.ExportRequest) -> collections.abc.AsyncIterator[s365b.modules.sdk.ExportFile] :async: Events and tasks as one iCalendar file, contacts as vCards, messages as .eml. Options: ``name``, the file's name, and for ``ics`` the calendar's name in it (e.g. the calendar or To Do list exported). :param config: Module settings. :param request: The items; only events and tasks (``ics``), contacts (``vcf``) or messages (``eml``) are exported. :Yields: One ``.ics`` or ``.vcf`` file, or an ``.eml`` file per message in its folder (read when the file is written). :raises NotSupportedError: For another format. .. py:method:: discover(ctx: s365b.modules.sdk.ModuleContext) -> collections.abc.AsyncIterator[s365b.modules.sdk.ProtectedObject] :async: Yield every mailbox the settings include. Users without a mailbox (no licence, guests) are left out. :param ctx: Module context. :Yields: Mailboxes. :raises ModuleError: If mailbox settings cannot be read at all. .. py:method:: 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] :async: Back up one mailbox incrementally. State: ``{"folders": {folder_id: {"path", "delta", "next"}}}``. :param ctx: Module context. :param obj: The mailbox. :param state: Saved state. :Yields: Messages, folder renames/removals and checkpoints. :raises ModuleError: If the mailbox cannot be read. .. py:function:: mailbox_id(ctx: s365b.modules.sdk.ModuleContext, user: str) -> str :async: Return a user's mailbox ID for the mailbox import/export API. :param ctx: Module context. :param user: User ID. :returns: The primary mailbox ID, e.g. ``MBX:…@…``. .. py:function:: export_batches(items: list[s365b.modules.sdk.ExtraWanted]) -> list[list[s365b.modules.sdk.ExtraWanted]] Group messages into export requests: at most 20, and not too large. :param items: Messages with their sizes. :returns: Batches; a message larger than the size limit goes alone. .. py:function:: exported(batch: list[s365b.modules.sdk.ExtraWanted], results: list[dict[str, Any]], ctx: s365b.modules.sdk.ModuleContext) -> list[tuple[str, bytes]] 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). :param batch: Messages asked for. :param results: The response's ``value``. :param ctx: Module context, for logging. :returns: ``(message ID, export stream)`` for each message exported.