summaryrefslogtreecommitdiff
path: root/mxclean/clean_local_store.sh
blob: 12402e7e3015bd3a96913d6bbd7f90b28c1b0eaf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/bin/sh

set -e

purge_ts=$(($(date +%s)-$((3600*24*30))))000
server=matrixim.cc
content_dir=/var/lib/matrix-synapse/media_store/local_content
tmp=/tmp/mxclean
CLEANER="$(pwd)/clean_files"

make "${CLEANER}"
cd "${content_dir}"
mkdir -p "${tmp}"
find -type f > ${tmp}/filelist

# for avatars in state events
# E2EE messages doesn't have the URI in the events, so do not rely on it on all events
echo 'select content from events' | psql -d synapse -Aqt | grep -F "mxc://${server}/" > ${tmp}/file_in_db
jq -r .avatar_url ${tmp}/file_in_db | awk -F '/' '{print $NF}' | sed '/null/d' > ${tmp}/dbfiles
jq -r .url ${tmp}/file_in_db | awk -F '/' '{print $NF}' | sed '/null/d' >> ${tmp}/dbfiles

# profile avatars
echo 'select avatar_url from profiles' | psql -d synapse -Aqt | sed '/^$/d' | awk -F '/' '{print $NF}' > ${tmp}/avatars

echo "select media_id from local_media_repository where created_ts > ${purge_ts}" \
	| psql -d synapse -Aqt > ${tmp}/recent_media

cat ${tmp}/dbfiles ${tmp}/avatars ${tmp}/recent_media | sort -u > ${tmp}/files_to_keep
${CLEANER}

echo "Please run 'cd ${content_dir} && sh ${tmp}/remove.sh' to clean the local content."