Skip to content

Add --restore-backup to download room keys from the server-side backup#209

Open
macalinao wants to merge 2 commits into
8go:mainfrom
macalinao:add-restore-backup
Open

Add --restore-backup to download room keys from the server-side backup#209
macalinao wants to merge 2 commits into
8go:mainfrom
macalinao:add-restore-backup

Conversation

@macalinao

@macalinao macalinao commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

matrix-commander-rs builds the client with the matrix-sdk default
BackupDownloadStrategy::Manual, so even when the store already holds the
megolm backup decryption key and version, the SDK never downloads the
backed-up room keys on its own. A verified device can therefore only decrypt
newly received messages and never reads encrypted history.

Add a --restore-backup action (alias --backup-restore) that enumerates
every room in the latest server-side key backup
(get_latest_backup_info + get_backup_keys) and downloads its keys into
the store via Backups::download_room_keys_for_room(), which works
regardless of the configured download strategy. It reports how many rooms
were restored / failed, for both text and --output json.

matrix-commander-rs --verify emoji      # device verified / has backup key
matrix-commander-rs --restore-backup    # pull all backed-up keys into the store
matrix-commander-rs --room '!room:server' --tail 50   # now reads history

Help text under help/ regenerated via scripts/create-help-*.sh.

matrix-commander-rs builds the client with the matrix-sdk default
BackupDownloadStrategy::Manual, so even when the store already holds the
megolm backup decryption key and version, the SDK never downloads the
backed-up room keys. A verified device can therefore only decrypt newly
received messages and never reads encrypted history.

Add a --restore-backup action (alias --backup-restore) that enumerates
every room in the latest server-side key backup and explicitly downloads
its keys into the store via Backups::download_room_keys_for_room(), which
works regardless of the configured download strategy.

It lists rooms with get_latest_backup_info + get_backup_keys and reports
how many rooms were restored (text and --output json). Help text in
help/ regenerated via scripts/create-help-*.sh.
@Benjamin-Loison

Benjamin-Loison commented Jun 24, 2026

Copy link
Copy Markdown
Collaborator

I do not notice any string typo and the code seems to quickly make sense.

I am waiting investigating #208, then produce the issue this pull request tries to solve, then will try with this pull request patch.

@Benjamin-Loison

Benjamin-Loison commented Jun 24, 2026

Copy link
Copy Markdown
Collaborator
cargo run -- --tail 40
Output:
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.28s
     Running `target/debug/matrix-commander-rs --tail 40`
Message: type Encrypted: room "!sNARMdEsFZERaQAJzl:matrix.org", sender "@benjamin_loison:matrix.org", event_id "$oa2zPV4iCHAGQ6ZSW6xPbP5FLdzvlb4r0FYM3MQlvyA", message could not be decrypted
Message: type Encrypted: room "!sNARMdEsFZERaQAJzl:matrix.org", sender "@benjamin_loison:matrix.org", event_id "$6gecl88G8hjtIIaR1nWkWgB7Im4i_cocomRBXIy6KVU", message could not be decrypted
Message: type Encrypted: room "!sNARMdEsFZERaQAJzl:matrix.org", sender "@benjamin_loison:matrix.org", event_id "$1ZhcYnEVSDENI8_77r-DGPlm4OALirrHv69-EXiEgDw", message could not be decrypted
Message: type Encrypted: room "!sNARMdEsFZERaQAJzl:matrix.org", sender "@benjamin_loison:matrix.org", event_id "$hOXOhmsLehfZDf-sikhAUWOJh6_EJxJZwXRopc_s_0U", message could not be decrypted
Message: type Encrypted: room "!sNARMdEsFZERaQAJzl:matrix.org", sender "@benjamin_loison:matrix.org", event_id "$o6zgKaO0c5zB1NWZdbQ4mcJTnF56LRc6EfVii3RiGGA", message could not be decrypted
Message: type Encrypted: room "!sNARMdEsFZERaQAJzl:matrix.org", sender "@benjamin_loison:matrix.org", event_id "$6kan2Sf0lf6vfP0Xu8LturuLweFerMS9S27iLYHStE8", message could not be decrypted
Message: type Encrypted: room "!sNARMdEsFZERaQAJzl:matrix.org", sender "@benjamin_loison:matrix.org", event_id "$zVoSIb4uSG-QNpzryfi3G2PTHSctgBB82q0PzjCd0Wk", message could not be decrypted
Message: type Encrypted: room "!sNARMdEsFZERaQAJzl:matrix.org", sender "@benjamin_loison:matrix.org", event_id "$BGRs9nCMsCOcN4dp1OBKzqYIiHENDd3JmIqTDe5k9BM", message could not be decrypted
Message: type Text: body "B", room "!sNARMdEsFZERaQAJzl:matrix.org", sender "@benjamin_loison:matrix.org", event id "$gC_umVkXT4IrnI2cZtOwDgHRx5BxZ3SyTrlkkMkWm0o", formatted None, 
Message: type Text: body "C", room "!sNARMdEsFZERaQAJzl:matrix.org", sender "@benjamin_loison:matrix.org", event id "$xFzqRlw6MYKNOrprWhNyP2pKC6vY95fdavZ1UCQJLl4", formatted None, 
Message: type Text: body "D", room "!sNARMdEsFZERaQAJzl:matrix.org", sender "@benjamin_loison:matrix.org", event id "$N4opf7pYH_efkTrKx5CWusawzlffsNYg6v4N-L8ohB8", formatted None, 
Message: type Text: body "E", room "!sNARMdEsFZERaQAJzl:matrix.org", sender "@benjamin_loison:matrix.org", event id "$WiADAB8d9-eXSh6QfRPcArc0ARjMKxWmxZ3FFfZkbXU", formatted None, 
Message: type Text: body "C", room "!sNARMdEsFZERaQAJzl:matrix.org", sender "@benjamin_loison:matrix.org", event id "$tRFx-UDQlk7cmeJtzeUAbXy303xdTtdrHJwaQtfO-JA", formatted None, 
Message: type Text: body "c", room "!sNARMdEsFZERaQAJzl:matrix.org", sender "@benjamin_loison:matrix.org", event id "$2CGM1oUriVNr-RcleAFnKlVpaFhjizdopvrW8236cLM", formatted None, 
2026-06-24T22:49:12.916573Z ERROR matrix_commander_rs: Error: crate::listen_tail reported Not Implemented Yet
2026-06-24T22:49:12.917879Z ERROR matrix_commander_rs: Encountered 1 error.
Error: NotImplementedYet
cargo run -- --restore-backup
Output:
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.27s
     Running `target/debug/matrix-commander-rs --restore-backup`
Restored room keys from backup version 2: 1 rooms restored, 0 failed, 109 sessions in backup
cargo run -- --tail 40
Output:
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.27s
     Running `target/debug/matrix-commander-rs --tail 40`
Message: type Encrypted: room "!sNARMdEsFZERaQAJzl:matrix.org", sender "@benjamin_loison:matrix.org", event_id "$oa2zPV4iCHAGQ6ZSW6xPbP5FLdzvlb4r0FYM3MQlvyA", message could not be decrypted
Message: type Encrypted: room "!sNARMdEsFZERaQAJzl:matrix.org", sender "@benjamin_loison:matrix.org", event_id "$6gecl88G8hjtIIaR1nWkWgB7Im4i_cocomRBXIy6KVU", message could not be decrypted
Message: type Encrypted: room "!sNARMdEsFZERaQAJzl:matrix.org", sender "@benjamin_loison:matrix.org", event_id "$1ZhcYnEVSDENI8_77r-DGPlm4OALirrHv69-EXiEgDw", message could not be decrypted
Message: type Encrypted: room "!sNARMdEsFZERaQAJzl:matrix.org", sender "@benjamin_loison:matrix.org", event_id "$hOXOhmsLehfZDf-sikhAUWOJh6_EJxJZwXRopc_s_0U", message could not be decrypted
Message: type Encrypted: room "!sNARMdEsFZERaQAJzl:matrix.org", sender "@benjamin_loison:matrix.org", event_id "$o6zgKaO0c5zB1NWZdbQ4mcJTnF56LRc6EfVii3RiGGA", message could not be decrypted
Message: type Encrypted: room "!sNARMdEsFZERaQAJzl:matrix.org", sender "@benjamin_loison:matrix.org", event_id "$6kan2Sf0lf6vfP0Xu8LturuLweFerMS9S27iLYHStE8", message could not be decrypted
Message: type Encrypted: room "!sNARMdEsFZERaQAJzl:matrix.org", sender "@benjamin_loison:matrix.org", event_id "$zVoSIb4uSG-QNpzryfi3G2PTHSctgBB82q0PzjCd0Wk", message could not be decrypted
Message: type Encrypted: room "!sNARMdEsFZERaQAJzl:matrix.org", sender "@benjamin_loison:matrix.org", event_id "$BGRs9nCMsCOcN4dp1OBKzqYIiHENDd3JmIqTDe5k9BM", message could not be decrypted
Message: type Text: body "B", room "!sNARMdEsFZERaQAJzl:matrix.org", sender "@benjamin_loison:matrix.org", event id "$gC_umVkXT4IrnI2cZtOwDgHRx5BxZ3SyTrlkkMkWm0o", formatted None, 
Message: type Text: body "C", room "!sNARMdEsFZERaQAJzl:matrix.org", sender "@benjamin_loison:matrix.org", event id "$xFzqRlw6MYKNOrprWhNyP2pKC6vY95fdavZ1UCQJLl4", formatted None, 
Message: type Text: body "D", room "!sNARMdEsFZERaQAJzl:matrix.org", sender "@benjamin_loison:matrix.org", event id "$N4opf7pYH_efkTrKx5CWusawzlffsNYg6v4N-L8ohB8", formatted None, 
Message: type Text: body "E", room "!sNARMdEsFZERaQAJzl:matrix.org", sender "@benjamin_loison:matrix.org", event id "$WiADAB8d9-eXSh6QfRPcArc0ARjMKxWmxZ3FFfZkbXU", formatted None, 
Message: type Text: body "C", room "!sNARMdEsFZERaQAJzl:matrix.org", sender "@benjamin_loison:matrix.org", event id "$tRFx-UDQlk7cmeJtzeUAbXy303xdTtdrHJwaQtfO-JA", formatted None, 
Message: type Text: body "c", room "!sNARMdEsFZERaQAJzl:matrix.org", sender "@benjamin_loison:matrix.org", event id "$2CGM1oUriVNr-RcleAFnKlVpaFhjizdopvrW8236cLM", formatted None, 
2026-06-24T22:49:52.121994Z ERROR matrix_commander_rs: Error: crate::listen_tail reported Not Implemented Yet
2026-06-24T22:49:52.123455Z ERROR matrix_commander_rs: Encountered 1 error.
Error: NotImplementedYet

It is always referring to the same encrypted room.

cargo run -- --restore-backup
Output:
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.28s
     Running `target/debug/matrix-commander-rs --restore-backup`
Restored room keys from backup version 2: 1 rooms restored, 0 failed, 109 sessions in backup

I will investigate this pull request further, once I have properly verified my devices.

Tracked at https://framagenda.org/apps/calendar/p/Dg8W8tqd5Dp9KTd3/dayGridMonth/now/view/full/L3JlbW90ZS5waHAvZGF2L3B1YmxpYy1jYWxlbmRhcnMvRGc4Vzh0cWQ1RHA5S1RkMy9mZjg5MjdiMy02YTZiLTQyMTMtOTdiYy05NjE2YzNjNjlhNDYuaWNz/1782673200.

@Benjamin-Loison

Copy link
Copy Markdown
Collaborator

A verified device can therefore only decrypt newly received messages and never reads encrypted history.

bold looks unclear to me, can you provide a precise example workflow that showcase the need for the feature you propose?

@8go

8go commented Jul 2, 2026

Copy link
Copy Markdown
Owner

Thank you @macalinao for your PR. This is a great contribution. thank you for your work and effort!!! 👏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants