Created
December 2, 2021 18:18
-
-
Save csett86/0f2ccc9b4c6e4afb3094894da498431f to your computer and use it in GitHub Desktop.
Compress Synapse
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# All thanks to https://levans.fr/shrink-synapse-database.html | |
# adapt as needed | |
API_TOKEN=<ADD API TOKEN> | |
HOST=http://localhost:8008 | |
ROOMLIMIT=10000 | |
PSQL_SYSTEM_USER=postgres | |
PSQL_USER=synapse | |
PSQL_PASSWORD=password | |
PSQL_HOST=localhost | |
PSQL_DB=synapse | |
ROOMLIST=$(curl --silent --header "Authorization: Bearer $API_TOKEN" $HOST/_synapse/admin/v1/rooms?limit=$ROOMLIMIT) | |
ROOMS_WITHOUT_LOCAL_USERS=$(echo $ROOMLIST | jq '.rooms[] | select(.joined_local_members == 0) | .room_id') | |
for room in $ROOMS_WITHOUT_LOCAL_USERS; do | |
curl --silent --header "Authorization: Bearer $API_TOKEN" --header "Content-Type: application/json" -d "{ \"room_id\": $room }" --output /dev/null $HOST/_synapse/admin/v1/purge_room | |
done | |
# Requires https://github.com/matrix-org/rust-synapse-compress-state | |
# change to /tmp to avoid 'could not change directory to "/root": Permission denied' | |
cd /tmp | |
ROOMS_TO_COMPRESS=$(sudo -u $PSQL_SYSTEM_USER psql --quiet -t -c 'SELECT room_id FROM state_groups_state GROUP BY room_id HAVING count(*) > 100000;' $PSQL_DB) | |
for room in $ROOMS_TO_COMPRESS; do | |
/usr/local/bin/synapse-compress-state -t -o /tmp/state-compressor.sql -p "host=$PSQL_HOST user=$PSQL_USER password=$PSQL_PASSWORD dbname=$PSQL_DB" -r "$room" >/dev/null 2>&1 | |
sudo -u $PSQL_SYSTEM_USER psql --quiet $PSQL_DB < /tmp/state-compressor.sql | |
done | |
systemctl stop matrix-synapse.service | |
# change to /tmp to avoid 'could not change directory to "/root": Permission denied' | |
cd /tmp | |
sudo -u $PSQL_SYSTEM_USER psql --quiet -c 'REINDEX DATABASE $PSQL_DB;' $PSQL_DB | |
sudo -u $PSQL_SYSTEM_USER psql --quiet -c 'VACUUM FULL;' $PSQL_DB | |
systemctl start matrix-synapse.service |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment