commit cd23c39201e8a67123f38f9909414b3e77407759 Author: sceox Date: Mon Jul 5 11:15:48 2021 -0700 initial commit diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..86e33ae --- /dev/null +++ b/Makefile @@ -0,0 +1,9 @@ +all: install + +install: + install -m 755 synapse-compress.sh /usr/local/bin/synapse-compress.sh + install -m 755 synapse-purge.sh /usr/local/bin/synapse-purge.sh + +uninstall: + rm /usr/local/bin/synapse-compress.sh + rm /usr/local/bin/synapse-purge.sh diff --git a/README.md b/README.md new file mode 100644 index 0000000..0fc504c --- /dev/null +++ b/README.md @@ -0,0 +1,22 @@ +Synapse (matrix server) scripts for Anarchy Planet. + +## Dependencies + +[curl](https://curl.se/) for GET and POST to the synapse API. + +[jq](https://stedolan.github.io/jq/) for JSON parsing. + +psql utility, which is normally automatically installed with the +postgresql server. + +[synapse-compress-state](https://github.com/matrix-org/rust-synapse-compress-state) +for compressing the synapse state tables. + +## Installing + +With this repository as your working directory, run `make` as root. + +## Using the scripts + +The scripts can only be run interactively, as they prompt for certain +passwords and parameters. They do not parse any arguments. diff --git a/synapse-compress.sh b/synapse-compress.sh new file mode 100755 index 0000000..ba28e0b --- /dev/null +++ b/synapse-compress.sh @@ -0,0 +1,36 @@ +#!/bin/sh + +# don't bother compressing unless we will save this much: +min_compression_percent=20 + +read -p "synapse admin API access token: " token +read -p "postgres synapse user password: " db_password + +get_synapse_version () { + curl --silent --ssl -H "Authorization: Bearer $token" \ + "localhost:8008/_synapse/admin/v1/server_version" +} + +get_all_rooms () { + curl --silent -H "Authorization: Bearer $token" \ + "localhost:8008/_synapse/admin/v1/rooms" \ + | jq '.rooms[].room_id' \ + | sed 's/"//g' +} + +compress_state () { + for room_id ; do + sqlf="$HOME/$(echo $room_id | tr -c -d '[:alpha:]').sql" + repl=$(synapse-compress-state -t -o $sqlf -p \ + "host=localhost user=synapse password=${db_password} dbname=synapse" \ + -r "$room_id" | sed -n '/%/s/.*(\([0-9]*\).[0-9]*%).*/\1/p') + + if [ "$repl" -le "$((100 - $min_compression_percent))" ]; then + echo "compressing room" $room_id "..." + psql -q -U 'synapse' -f $sqlf 'synapse' + fi + rm $sqlf + done +} + +compress_state $(get_all_rooms) diff --git a/synapse-purge.sh b/synapse-purge.sh new file mode 100755 index 0000000..72d88c8 --- /dev/null +++ b/synapse-purge.sh @@ -0,0 +1,75 @@ +#!/bin/sh + +# guide: https://levans.fr/shrink-synapse-database.html +# doc: https://matrix-org.github.io/synapse/develop/admin_api/purge_history_api.html + +synapse_log='/var/log/matrix-synapse/homeserver.log' + +read -p "synapse admin API access_token: " token +read -p "history to keep (date range with no spaces, eg 1month, 1day): " range + +# timestamps are in milliseconds since the epoch +ts_history="$(date -d-${range} +%s)000" +ts_media="$(date -d-1month +%s)000" + +get_obsolete_rooms () { + tmpf=$(mktemp) + curl --silent -H "Authorization: Bearer $token" \ + "localhost:8008/_synapse/admin/v1/rooms" \ + | jq '.rooms[] | select(.joined_local_members == 0) | .room_id' \ + | sed 's/"//g' +} + +get_synapse_version () { + curl --silent --ssl -H "Authorization: Bearer $token" \ + "localhost:8008/_synapse/admin/v1/server_version" +} + +get_all_rooms () { + curl --silent -H "Authorization: Bearer $token" \ + "localhost:8008/_synapse/admin/v1/rooms" \ + | jq '.rooms[].room_id' \ + | sed 's/"//g' +} + +purge_obsolete_rooms () { + for room_id in $(get_obsolete_rooms); do + curl --silent --ssl -X POST --header "Content-Type: application/json" \ + --header "Authorization: Bearer $token" \ + -d "{ \"room_id\": \"$room_id\" }" \ + "localhost:8008/_synapse/admin/v1/purge_room" \ + | jq + done +} + +purge_history () { + # note: to delete local events as well, do: + # -d "{ \"delete_local_events\": true, ... }" + for room_id ; do + json=$(curl --silent --ssl -X POST \ + --header "Content-Type: application/json" \ + --header "Authorization: Bearer $token" \ + -d "{ \"delete_local_events\": false, \"purge_up_to_ts\": $ts_history }" \ + "localhost:8008/_synapse/admin/v1/purge_history/${room_id}") + echo $json | jq + if echo $json | grep -q 'purge_id'; then + echo "waiting for purge to complete..." + tail -n 0 -f $synapse_log \ + | sed '/\[purge\] complete/q' > /dev/null + echo "purge completed." + fi + done + echo "it is recommended to run VACUUM FULL on the database now" + echo "(note: VACUUM FULL requires up to 50% of the disk be available)" +} + +purge_media_cache () { + curl --silent -X POST --ssl \ + --header "Authorization: Bearer $token" -d '' \ + "localhost:8008/_synapse/admin/v1/purge_media_cache?before_ts=${ts_media}" \ + | jq +} + +purge_obsolete_rooms +purge_history $(get_all_rooms) +purge_media_cache