Skip to content

Commit d728430

Browse files
authored
Add some documentation about backing up Synapse (#17931)
Fixes: element-hq/element-meta#2155 Fixes: #2046
1 parent e80dad5 commit d728430

File tree

6 files changed

+147
-3
lines changed

6 files changed

+147
-3
lines changed

changelog.d/17931.doc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add documentation about backing up Synapse.

docs/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
- [Using `synctl` with Workers](synctl_workers.md)
5555
- [Systemd](systemd-with-workers/README.md)
5656
- [Administration](usage/administration/README.md)
57+
- [Backups](usage/administration/backups.md)
5758
- [Admin API](usage/administration/admin_api/README.md)
5859
- [Account Validity](admin_api/account_validity.md)
5960
- [Background Updates](usage/administration/admin_api/background_updates.md)

docs/postgres.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ database:
100100
keepalives_count: 3
101101
```
102102

103+
## Backups
104+
105+
Don't forget to [back up](./usage/administration/backups.md#database) your database!
106+
103107
## Tuning Postgres
104108

105109
The default settings should be fine for most deployments. For larger

docs/setup/installation.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,10 @@ This also requires the optional `lxml` python dependency to be installed. This
656656
in turn requires the `libxml2` library to be available - on Debian/Ubuntu this
657657
means `apt-get install libxml2-dev`, or equivalent for your OS.
658658

659+
### Backups
660+
661+
Don't forget to take [backups](../usage/administration/backups.md) of your new server!
662+
659663
### Troubleshooting Installation
660664

661665
`pip` seems to leak *lots* of memory during installation. For instance, a Linux

docs/usage/administration/backups.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# How to back up a Synapse homeserver
2+
3+
It is critical to maintain good backups of your server, to guard against
4+
hardware failure as well as potential corruption due to bugs or administrator
5+
error.
6+
7+
This page documents the things you will need to consider backing up as part of
8+
a Synapse installation.
9+
10+
## Configuration files
11+
12+
Keep a copy of your configuration file (`homeserver.yaml`), as well as any
13+
auxiliary config files it refers to such as the
14+
[`log_config`](../configuration/config_documentation.md#log_config) file,
15+
[`app_service_config_files`](../configuration/config_documentation.md#app_service_config_files).
16+
Often, all such config files will be kept in a single directory such as
17+
`/etc/synapse`, which will make this easier.
18+
19+
## Server signing key
20+
21+
Your server has a [signing
22+
key](../configuration/config_documentation.md#signing_key_path) which it uses
23+
to sign events and outgoing federation requests. It is easiest to back it up
24+
with your configuration files, but an alternative is to have Synapse create a
25+
new signing key if you have to restore.
26+
27+
If you do decide to replace the signing key, you should add the old *public*
28+
key to
29+
[`old_signing_keys`](../configuration/config_documentation.md#old_signing_keys).
30+
31+
## Database
32+
33+
Synapse's support for SQLite is only suitable for testing purposes, so for the
34+
purposes of this document, we'll assume you are using
35+
[PostgreSQL](../../postgres.md).
36+
37+
A full discussion of backup strategies for PostgreSQL is out of scope for this
38+
document; see the [PostgreSQL
39+
documentation](https://www.postgresql.org/docs/current/backup.html) for
40+
detailed information.
41+
42+
### Synapse-specfic details
43+
44+
* Be very careful not to restore into a database that already has tables
45+
present. At best, this will error; at worst, it will lead to subtle database
46+
inconsistencies.
47+
48+
* The `e2e_one_time_keys_json` table should **not** be backed up, or if it is
49+
backed up, should be
50+
[`TRUNCATE`d](https://www.postgresql.org/docs/current/sql-truncate.html)
51+
after restoring the database before Synapse is started.
52+
53+
[Background: restoring the database to an older backup can cause
54+
used one-time-keys to be re-issued, causing subsequent [message decryption
55+
errors](https://github.com/element-hq/element-meta/issues/2155). Clearing
56+
all one-time-keys from the database ensures that this cannot happen, and
57+
will prompt clients to generate and upload new one-time-keys.]
58+
59+
### Quick and easy database backup and restore
60+
61+
Typically, the easiest solution is to use `pg_dump` to take a copy of the whole
62+
database. We recommend `pg_dump`'s custom dump format, as it produces
63+
significantly smaller backup files.
64+
65+
```shell
66+
sudo -u postgres pg_dump -Fc --exclude-table-data e2e_one_time_keys_json synapse > synapse.dump
67+
```
68+
69+
There is no need to stop Postgres or Synapse while `pg_dump` is running: it
70+
will take a consistent snapshot of the databse.
71+
72+
To restore, you will need to recreate the database as described in [Using
73+
Postgres](../../postgres.md#set-up-database),
74+
then load the dump into it with `pg_restore`:
75+
76+
```shell
77+
sudo -u postgres createdb --encoding=UTF8 --locale=C --template=template0 --owner=synapse_user synapse
78+
sudo -u postgres pg_restore -d synapse < synapse.dump
79+
```
80+
81+
(If you forgot to exclude `e2e_one_time_keys_json` during `pg_dump`, remember
82+
to connect to the new database and `TRUNCATE e2e_one_time_keys_json;` before
83+
starting Synapse.)
84+
85+
To reiterate: do **not** restore a dump over an existing database.
86+
87+
Again, if you plan to run your homeserver at any sort of production level, we
88+
recommend studying the PostgreSQL documentation on backup options.
89+
90+
## Media store
91+
92+
Synapse keeps a copy of media uploaded by users, including avatars and message
93+
attachments, in its [Media
94+
store](../configuration/config_documentation.md#media-store).
95+
96+
It is a directory on the local disk, containing the following directories:
97+
98+
* `local_content`: this is content uploaded by your local users. As a general
99+
rule, you should back this up: it may represent the only copy of those
100+
media files anywhere in the federation, and if they are lost, users will
101+
see errors when viewing user or room avatars, and messages with attachments.
102+
103+
* `local_thumbnails`: "thumbnails" of images uploaded by your users. If
104+
[`dynamic_thumbnails`](../configuration/config_documentation.md#dynamic_thumbnails)
105+
is enabled, these will be regenerated if they are removed from the disk, and
106+
there is therefore no need to back them up.
107+
108+
If `dynamic_thumbnails` is *not* enabled (the default): although this can
109+
theoretically be regenerated from `local_content`, there is no tooling to do
110+
so. We recommend that these are backed up too.
111+
112+
* `remote_content`: this is a cache of content that was uploaded by a user on
113+
another server, and has since been requested by a user on your own server.
114+
115+
Typically there is no need to back up this directory: if a file in this directory
116+
is removed, Synapse will attempt to fetch it again from the remote
117+
server.
118+
119+
* `remote_thumbnails`: thumbnails of images uploaded by users on other
120+
servers. As with `remote_content`, there is normally no need to back this
121+
up.
122+
123+
* `url_cache`, `url_cache_thumbnails`: temporary caches of files downloaded
124+
by the [URL previews](../../setup/installation.md#url-previews) feature.
125+
These do not need to be backed up.

docs/usage/configuration/config_documentation.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3128,6 +3128,15 @@ it was last used.
31283128
It is possible to build an entry from an old `signing.key` file using the
31293129
`export_signing_key` script which is provided with synapse.
31303130

3131+
If you have lost the private key file, you can ask another server you trust to
3132+
tell you the public keys it has seen from your server. To fetch the keys from
3133+
`matrix.org`, try something like:
3134+
3135+
```
3136+
curl https://matrix-federation.matrix.org/_matrix/key/v2/query/myserver.example.com |
3137+
jq '.server_keys | map(.verify_keys) | add'
3138+
```
3139+
31313140
Example configuration:
31323141
```yaml
31333142
old_signing_keys:
@@ -4391,9 +4400,9 @@ It is possible to scale the processes that handle sending outbound federation re
43914400
by running a [`generic_worker`](../../workers.md#synapseappgeneric_worker) and adding it's [`worker_name`](#worker_name) to
43924401
a `federation_sender_instances` map. Doing so will remove handling of this function from
43934402
the main process. Multiple workers can be added to this map, in which case the work is
4394-
balanced across them.
4403+
balanced across them.
43954404

4396-
The way that the load balancing works is any outbound federation request will be assigned
4405+
The way that the load balancing works is any outbound federation request will be assigned
43974406
to a federation sender worker based on the hash of the destination server name. This
43984407
means that all requests being sent to the same destination will be processed by the same
43994408
worker instance. Multiple `federation_sender_instances` are useful if there is a federation
@@ -4750,7 +4759,7 @@ This setting has the following sub-options:
47504759
* `only_for_direct_messages`: Whether invites should be automatically accepted for all room types, or only
47514760
for direct messages. Defaults to false.
47524761
* `only_from_local_users`: Whether to only automatically accept invites from users on this homeserver. Defaults to false.
4753-
* `worker_to_run_on`: Which worker to run this module on. This must match
4762+
* `worker_to_run_on`: Which worker to run this module on. This must match
47544763
the "worker_name". If not set or `null`, invites will be accepted on the
47554764
main process.
47564765

0 commit comments

Comments
 (0)