Compare commits

..

4 commits

Author SHA1 Message Date
1837e81c3d
Update README.md 2024-05-03 13:26:46 +03:00
3904c76478
Support database backup and restore 2024-05-03 13:26:46 +03:00
bba57e4648
Add the container_name to postgresql 2024-05-03 13:26:46 +03:00
0945e9359e
Update README.md 2024-05-03 13:26:45 +03:00
3 changed files with 45 additions and 25 deletions

View file

@ -7,19 +7,42 @@
# Stop the instance [aliases: s] # Stop the instance [aliases: s]
@stop: @stop:
sudo docker-compose rm -f -s sudo docker-compose stop
# Restart the instance # Restart the instance
@restart: @restart:
sudo docker-compose restart sudo docker-compose restart
# Create Backup file [aliases: b] # Create Backup file [aliases: b]
@backup backup_name: stop && run @backup backup_name:
#!/usr/bin/env bash #!/usr/bin/env bash
FILES="data postgresdata Justfile docker-compose.yml README.md" DATABASE_BACKUP_FILE="synapse-postgres-backup-$(date +%d-%m-%Y"_"%H%M%S).sql.gz"
sudo docker-compose exec postgresql bash -c "export PGPASSWORD=somepassword && pg_dump -U synapse synapse" | \
gzip -9 > $DATABASE_BACKUP_FILE
FILES="data postgresdata Justfile docker-compose.yml README.md $DATABASE_BACKUP_FILE"
7z a -t7z -m0=lzma2 -mx=9 -mfb=64 -md=32m -ms=on -mhe=on -p {{backup_name}}.7z $FILES 7z a -t7z -m0=lzma2 -mx=9 -mfb=64 -md=32m -ms=on -mhe=on -p {{backup_name}}.7z $FILES
rm -fr $DATABASE_BACKUP_FILE
echo "Backup done..." echo "Backup done..."
# Restore Backup file
@restore backup_name: stop && run
#!/usr/bin/env bash
7z x {{backup_name}}.7z -aoa
DATABASE_BACKUP_FILE=$(ls synapse-postgres-backup-*.sql.gz)
DATABASE_FILE=$(echo $DATABASE_BACKUP_FILE | sed 's/.gz//')
# Run the database to write the sql file to the container
sudo docker-compose up -d postgresql
gunzip -k $DATABASE_BACKUP_FILE && \
sudo docker cp $DATABASE_FILE postgresql_synapse:/pg-backup.sql && \
sudo docker-compose exec postgresql bash -c "export PGPASSWORD=somepassword && dropdb -U synapse synapse --force && createdb -U synapse synapse && psql -U synapse synapse < pg-backup.sql"
rm -fr $DATABASE_FILE
rm -fr $DATABASE_BACKUP_FILE
# Stop the database container (it will be started by the run command)
sudo docker-compose stop postgresql
echo "Restore done..."
[private] [private]
alias r := run alias r := run
@ -27,4 +50,5 @@ alias r := run
alias s := stop alias s := stop
[private] [private]
alias b := backup alias b := backup
[private]
alias re := restore

View file

@ -4,7 +4,7 @@ This is my personal matrix homeserver. You can clone this repository and run the
## Overview ## Overview
In this repository I use `4rs.nl` (my domain) as an example. You should replace this with your own domain. In this repository I use `4rs.nl` (my domain) as an example. You should replace this with your own domain.
After reading this README you should have a `/.well-known/matrix/server` and `/.well-known/matrix/client` file on your domain (4rs.nl) and your matrix subdomain (matrix.4rs.nl). The client will use `matrix.4rs.nl` as the homeserver and the displayed homeserver will be `4rs.nl`. After reading this README you should have a `/.well-known/matrix/server` and `/.well-known/matrix/client` file on your domain (4rs.nl). The client will use `matrix.4rs.nl` as the homeserver and the displayed server name will be `4rs.nl`.
## Requirements ## Requirements
- docker - docker
@ -12,14 +12,14 @@ After reading this README you should have a `/.well-known/matrix/server` and `/.
- nginx - nginx
## Domain requirements ## Domain requirements
- Have a `/.well-known/matrix/server` file on your domain that points to your homeserver. This is required for federation to work. - Have a `/.well-known/matrix/server` file on your domain (server name) that points to your homeserver. This is required for federation to work.
The content of the file should be: The content of the file should be:
``` ```
{ {
"m.server": "matrix.4rs.nl:443" "m.server": "matrix.4rs.nl:443"
} }
``` ```
- Have a `/.well-known/matrix/client` file on your domain that points to your homeserver. This is required for the client to work. - Have a `/.well-known/matrix/client` file on your domain (server name) that points to your homeserver. This is required for the client to work.
The content of the file should be: The content of the file should be:
``` ```
{ {
@ -29,7 +29,8 @@ The content of the file should be:
} }
``` ```
For me, I created the files in my static blog and then deployed it in GitHub pages. See the [justfile that I use to deploy the files to the domain](https://git.4rs.nl/awiteb/blog/src/branch/master/Justfile#L15-L17). You can use any other method to deploy the files and make them accessible on your domain, as long as they are accessible at `/.well-known/matrix/server` and `/.well-known/matrix/client`. For me, I created the files in my static blog and then deployed it in GitHub pages. See the files [here](https://git.4rs.nl/awiteb/blog/src/branch/master/static/.well-known/matrix) in my blog repository.
You can use any other method to deploy the files and make them accessible on your domain, as long as they are accessible at `/.well-known/matrix/server` and `/.well-known/matrix/client`.
## Nginx configuration of the matrix subdomain ## Nginx configuration of the matrix subdomain
@ -87,16 +88,6 @@ server {
client_body_timeout 5s; client_body_timeout 5s;
client_header_timeout 5s; client_header_timeout 5s;
location /.well-known/matrix/server {
return 200 '{"m.server": "matrix.4rs.nl:443"}';
default_type application/json;
add_header Access-Control-Allow-Origin *;
}
location /.well-known/matrix/client {
return 200 '{"m.homeserver": {"base_url": "https://matrix.4rs.nl}}';
default_type application/json;
add_header Access-Control-Allow-Origin *;
}
location / { location / {
proxy_pass http://localhost:8008; proxy_pass http://localhost:8008;
proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header X-Forwarded-For $remote_addr;
@ -116,12 +107,13 @@ After you have cloned the repository, replace all `4rs.nl` with your domain also
There is tow things only you need to change it, the first one is the secrets in `./data/homeserver.yaml` and the second one is the signing key in `./data/4rs.nl.signing.key`. There is tow things only you need to change it, the first one is the secrets in `./data/homeserver.yaml` and the second one is the signing key in `./data/4rs.nl.signing.key`.
### Homeserver.yaml ### homeserver.yaml
After replacing all `4rs.nl` with your domain, you need to generate a secret for each secret in the `homeserver.yaml` file. You can generate a secret with `openssl rand -base64 32`. Replace the secret with the generated secret. After replacing all `4rs.nl` with your domain, you need to generate a secret for each secret in the `homeserver.yaml` file. You can generate a secret with `openssl rand -base64 32`. Replace the secret with the generated secret.
### Signing key ### Signing key
> **Note** > [!NOTE]
>
> You need `signedjson` dependency to generate a signing key. You can install it with `pip3 install signedjson`. > You need `signedjson` dependency to generate a signing key. You can install it with `pip3 install signedjson`.
Change the content of the `4rs.nl.signing.key` file with a generated key. You can generate a key with `generate_signing_key` script in root of the repository. Run `python3 generate_signing_key` and replace the content of the `4rs.nl.signing.key` file with the generated key. Change the content of the `4rs.nl.signing.key` file with a generated key. You can generate a key with `generate_signing_key` script in root of the repository. Run `python3 generate_signing_key` and replace the content of the `4rs.nl.signing.key` file with the generated key.
@ -132,12 +124,15 @@ After all above steps, you can run the homeserver with `docker-compose up -d`. Y
Now you need to create an admin user with `docker exec -it synapse register_new_matrix_user http://localhost:8008 -c /data/homeserver.yaml` and follow the instructions. You should now have an admin user on the homeserver and you can login with it on the client using the homeserver `matrix.4rs.nl`. Enjoy your homeserver! Now you need to create an admin user with `docker exec -it synapse register_new_matrix_user http://localhost:8008 -c /data/homeserver.yaml` and follow the instructions. You should now have an admin user on the homeserver and you can login with it on the client using the homeserver `matrix.4rs.nl`. Enjoy your homeserver!
> [!NOTE]
>
> You need [`just`](https://just.systems/) to backup and restore your homeserver. You can install it with `cargo install just`.
## Backup ## Backup
> **Note**
> You need [`just`](https://just.systems/) to backup the homeserver. You can install it with `cargo install just`.
You can backup the homeserver with `just backup <backup-name>`. And it's will stored as encrypted AES256 7z file. You can backup the homeserver with `just backup <backup-name>`. And it's will stored as encrypted AES256 7z file.
## Restore
To restore the homeserver, you can use `just restore <backup-name>`. Make sure to run it in the root of the repository.
## Any questions? ## Any questions?
If you have any questions, you can contact with me at `@awiteb:4rs.nl` and I will try to help you. Have fun with your homeserver! If you have any questions, you can contact with me at `@awiteb:4rs.nl` and I will try to help you. Have fun with your homeserver!

View file

@ -14,6 +14,7 @@ services:
- "8008:8008/tcp" - "8008:8008/tcp"
- "8448:8448/tcp" - "8448:8448/tcp"
postgresql: postgresql:
container_name: postgresql_synapse
image: postgres:latest image: postgres:latest
restart: always restart: always
environment: environment: