2020-06-19 10:05:02 +02:00
|
|
|
#!/bin/bash
|
|
|
|
## author : Dryusdan
|
|
|
|
## date : 17/06/2020
|
|
|
|
## description : A Synapse cleaner
|
|
|
|
## usage : ./synapse-cleaner.sh PARTIAL|FULL
|
|
|
|
|
|
|
|
## Import ####################################
|
|
|
|
source utils/strictmode
|
|
|
|
source utils/log
|
|
|
|
source utils/config
|
|
|
|
#######################################################
|
|
|
|
|
2020-09-18 10:06:48 +02:00
|
|
|
CURLBIN="/usr/bin/env curl"
|
|
|
|
|
2020-09-18 10:07:32 +02:00
|
|
|
function purge_img {
|
2020-09-18 10:06:48 +02:00
|
|
|
${CURLBIN} -X POST "${HS_URL}${API_URL}purge_media_cache?before_ts=$(($(date --date="1 month ago" +%s%N)/1000000))&access_token=${ACCESS_TOKEN}"
|
2020-09-05 17:35:05 +02:00
|
|
|
}
|
2020-06-19 10:05:02 +02:00
|
|
|
function get_room {
|
|
|
|
info "Get total room"
|
2020-09-18 10:06:48 +02:00
|
|
|
total_room=$(${CURLBIN} -s --header "Authorization: Bearer ${ACCESS_TOKEN}" "${HS_URL}${API_URL}rooms?limit=1" | jq --raw-output ".total_rooms")
|
2020-06-19 10:05:02 +02:00
|
|
|
|
|
|
|
info "Get ${total_room} rooms"
|
2020-09-18 10:06:48 +02:00
|
|
|
${CURLBIN} -s --header "Authorization: Bearer ${ACCESS_TOKEN}" "${HS_URL}${API_URL}rooms?limit=${total_room}" > "/tmp/${CLEAN_FOLDER}/roomlist.json"
|
2020-06-19 10:05:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function get_purge {
|
2020-09-18 10:03:58 +02:00
|
|
|
jq --raw-output '.rooms[] | select(.joined_local_members == 0) | .room_id' < "/tmp/${CLEAN_FOLDER}/roomlist.json" > "/tmp/${CLEAN_FOLDER}/to_purge.txt"
|
2020-06-19 10:05:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function get_id {
|
2020-09-18 10:03:58 +02:00
|
|
|
jq --raw-output '.rooms[] | .room_id' < "/tmp/${CLEAN_FOLDER}/roomlist.json" > "/tmp/${CLEAN_FOLDER}/roomids.txt"
|
2020-06-19 10:05:02 +02:00
|
|
|
}
|
|
|
|
info "Starting at $(date)"
|
|
|
|
|
2020-09-18 10:03:58 +02:00
|
|
|
info "Create /tmp/${CLEAN_FOLDER}"
|
|
|
|
mkdir -p "/tmp/${CLEAN_FOLDER}"
|
2020-06-19 10:05:02 +02:00
|
|
|
|
2020-09-05 17:35:05 +02:00
|
|
|
purge_img
|
2020-06-19 10:05:02 +02:00
|
|
|
get_room
|
|
|
|
get_purge
|
|
|
|
info "Pruning empty room"
|
2020-09-18 10:03:58 +02:00
|
|
|
for room_id in $(cat "/tmp/${CLEAN_FOLDER}/to_purge.txt")
|
2020-06-19 10:05:02 +02:00
|
|
|
do
|
|
|
|
info "Pruning ${room_id}"
|
|
|
|
curl -X POST -d '{"room_id": "'${room_id}'"}' "${HS_URL}${API_URL}purge_room?access_token=${ACCESS_TOKEN}"
|
|
|
|
sleep 1
|
|
|
|
done
|
2020-09-18 10:03:58 +02:00
|
|
|
info "clean /tmp/${CLEAN_FOLDER}"
|
|
|
|
rm -rf "/tmp/${CLEAN_FOLDER}"/*
|
2020-06-19 10:05:02 +02:00
|
|
|
|
|
|
|
get_room
|
|
|
|
get_id
|
|
|
|
|
|
|
|
info "Start purging"
|
2020-09-18 10:03:58 +02:00
|
|
|
for room_id in $(cat "/tmp/${CLEAN_FOLDER}/roomids.txt")
|
2020-06-19 10:05:02 +02:00
|
|
|
do
|
|
|
|
info "Purge ${room_id}"
|
|
|
|
purge_id=$(curl -s -X POST -d '{"purge_up_to_ts":'$(($(date --date="1 month ago" +%s%N)/1000000))', "delete_local_events": true}' "${HS_URL}${API_URL}purge_history/${room_id}?access_token=${ACCESS_TOKEN}" | jq --raw-output '.purge_id')
|
|
|
|
info "Get state of ${purge_id}"
|
|
|
|
sleep 2
|
|
|
|
purge_state=$(/usr/bin/curl -s -X GET "${HS_URL}${API_URL}purge_history_status/${purge_id}?access_token=${ACCESS_TOKEN}" | jq --raw-output '.status')
|
|
|
|
warning "Purge ${room_id} (${purge_id}) is ${purge_state}"
|
|
|
|
while [ "${purge_state}" == "active" ]
|
|
|
|
do
|
|
|
|
sleep 2
|
|
|
|
purge_state=$(/usr/bin/curl -s -X GET "${HS_URL}${API_URL}/purge_history_status/${purge_id}?access_token=${ACCESS_TOKEN}" | jq --raw-output '.status')
|
|
|
|
done
|
|
|
|
warning "Purge ${room_id} (${purge_id}) is ${purge_state}"
|
|
|
|
done
|
|
|
|
|
|
|
|
if [ ${1} == "FULL" ]
|
|
|
|
then
|
|
|
|
get_id
|
|
|
|
|
|
|
|
info "Compress states"
|
2020-09-18 10:03:58 +02:00
|
|
|
for room_id in $(cat "/tmp/${CLEAN_FOLDER}/roomids.txt")
|
2020-06-19 10:05:02 +02:00
|
|
|
do
|
|
|
|
info "Get state compression for ${room_id}"
|
2020-09-18 10:03:58 +02:00
|
|
|
${SYNAPSE_COMPRESS_STATE_PATH} -t -o "/tmp/${CLEAN_FOLDER}/state-compressor.sql" -p "host=${HOST} user=${USER} password=${PASSWORD} dbname=${DBNAME}" -r "${room_id}"
|
2020-06-19 10:05:02 +02:00
|
|
|
|
|
|
|
info "Import state compression"
|
2020-09-18 10:03:58 +02:00
|
|
|
cat "/tmp/${CLEAN_FOLDER}/state-compressor.sql"
|
|
|
|
PGPASSWORD=${PASSWORD} psql -h "${HOST}" -U "${USER}" "${DBNAME}" < "/tmp/${CLEAN_FOLDER}/state-compressor.sql"
|
2020-06-19 10:05:02 +02:00
|
|
|
|
|
|
|
info "Ending, remove file"
|
2020-09-18 10:03:58 +02:00
|
|
|
rm -f "/tmp/${CLEAN_FOLDER}/state-compressor.sql"
|
2020-06-19 10:05:02 +02:00
|
|
|
info "Finish remove"
|
|
|
|
done
|
|
|
|
|
|
|
|
info "Cleaning"
|
2020-09-18 10:03:58 +02:00
|
|
|
rm -rf "/tmp/${CLEAN_FOLDER}"
|
2020-06-19 10:05:02 +02:00
|
|
|
|
|
|
|
PGPASSWORD=${PASSWORD} psql -h "${HOST}" -U "${USER}" "${DBNAME}" -c "REINDEX DATABASE ${DBNAME};"
|
|
|
|
PGPASSWORD=${PASSWORD} psql -h "${HOST}" -U "${USER}" "${DBNAME}" -c "VACUUM FULL VERBOSE;"
|
|
|
|
fi
|
|
|
|
info "Ending at $(date)"
|