5
0

initial commit

This commit is contained in:
sceox 2021-07-05 11:15:48 -07:00
commit cd23c39201
4 changed files with 142 additions and 0 deletions

9
Makefile Normal file
View File

@ -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

22
README.md Normal file
View File

@ -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.

36
synapse-compress.sh Executable file
View File

@ -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)

75
synapse-purge.sh Executable file
View File

@ -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