diff --git a/CleanupMedia.sh b/CleanupMedia.sh new file mode 100644 index 0000000..ca061bd --- /dev/null +++ b/CleanupMedia.sh @@ -0,0 +1,40 @@ +#!/bin/bash + +# +# Cleanup of local and remote media +# + +# Requirements: +# You'll need the domain of your server (variable "serverDomain"). +# +# For authentication, you will need the access token of your (admin) account. +# You can obtain it in your Element client: "All settings" > "Help & About" > scroll down > click on "" to get your access token. +# Copy this token and paste it further down (variable "token"). +# +# You can also specify another time span for deleting media. +# Default is media, which was not accessed over the last 60 days (variable "timestamp60DaysAgo") +# +# You can automate this script via cron, e.g. cleanup every night: +# 0 1 * * * /path/to/scripts/Matrix-Synapse-Helpers/CleanupMedia.sh > /dev/null 2>&1 + +serverDomain="matrix.mydomain.com" +token="insert-token-here" +timestamp60DaysAgo=`date +%s000 --date "60 days ago"` + +# Delete local media +echo "Delete local media..." +echo "" +curlUrl="http://localhost:8008/_synapse/admin/v1/media/$serverDomain/delete?before_ts=$timestamp60DaysAgo&access_token=$token" +curl -XPOST $curlUrl +echo "" +echo "Done" +echo "" + +# Purge remote media +echo "Purge remote media..." +echo "" +curlUrl="http://localhost:8008/_synapse/admin/v1/purge_media_cache?before_ts=$timestamp60DaysAgo&access_token=$token" +curl -XPOST $curlUrl +echo "" +echo "Done" +echo "" \ No newline at end of file diff --git a/GetAccessToken.sh b/GetAccessToken.sh new file mode 100644 index 0000000..ea72d1e --- /dev/null +++ b/GetAccessToken.sh @@ -0,0 +1,29 @@ +#!/bin/bash + +# +# Gets the access token of a Matrix Synapse account using curl. +# + +# The following variables have to be set individually. +serverDomain="matrix.mydomain.com" +user="MYUSER" +password="MYPASSWORD" + +json="{\"type\":\"m.login.password\", \"user\":\"$user\", \"password\":\"$password\"}" +response=$(curl --silent -w "\n%{http_code}" -XPOST -d "$json" "https://$serverDomain/_matrix/client/r0/login") +http_code=$(tail -n1 <<< "$response") +content=$(sed '$ d' <<< "$response") + +if [[ "$http_code" -ne 200 ]] ; then + echo "Error: $content" + exit 1 +fi + +echo "" +echo "Your access token:" +echo "" + +echo $content + +echo "" +echo "" diff --git a/README.md b/README.md index eb20d70..17a31a3 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ -# Matrix-Synapse-Helpers +# Helper scripts for Matrix Synapse -Helper scripts for Matrix Synapse \ No newline at end of file +* `GetAccessToken.sh`: Gets the access token with curl. +* `CleanupMedia.sh`: Deletes old local and remote media. \ No newline at end of file