mattermost_ynh icon indicating copy to clipboard operation
mattermost_ynh copied to clipboard

Data rentention management

Open anubister opened this issue 4 years ago • 5 comments

Data retention management is provided only in the Enterprise Edition : https://docs.mattermost.com/administration/data-retention.html I think it is however a important function for the privacy management and also to control the disk usage of Mattermost, therefore I would suggest to provide the following script to help Yunohost's administrators (it deletes all messages and medias older than a given number of days) :

#!/bin/bash

# configure vars
DB_USER="mattermost"
DB_NAME="mattermost"
DB_PASS=""
DB_HOST="localhost"
RETENTION="93" #number of days to *keep*; 93 ~ 3 months
DATA_PATH="/home/yunohost.app/mattermost/"

# calculate epoch in milisec
delete_before=$(date  --date="$RETENTION day ago"  "+%s%3N")
echo $(date  --date="$RETENTION day ago")
export PGPASSWORD=$DB_PASS

# get list of files to be removed
mysql -h "$DB_HOST" -u"$DB_USER" -p"$DB_PASS" "$DB_NAME" --disable-column-names -e "SELECT Path FROM FileInfo WHERE CreateAt < $delete_before;" > /tmp/mattermost-paths.list
mysql -h "$DB_HOST" -u"$DB_USER" -p"$DB_PASS" "$DB_NAME" --disable-column-names -e "SELECT ThumbnailPath FROM FileInfo WHERE CreateAt < $delete_before;" >> /tmp/mattermost-paths.list
mysql -h "$DB_HOST" -u"$DB_USER" -p"$DB_PASS" "$DB_NAME" --disable-column-names -e "SELECT PreviewPath FROM FileInfo WHERE CreateAt < $delete_before;" >> /tmp/mattermost-paths.list

# get list of posts to be removed
mysql -h "$DB_HOST" -u"$DB_USER" -p"$DB_PASS" "$DB_NAME" -e "SELECT * FROM Posts WHERE CreateAt < $delete_before;" 

# cleanup db 
mysql -h "$DB_HOST" -u"$DB_USER" -p"$DB_PASS" "$DB_NAME" -e "DELETE FROM Posts WHERE CreateAt < $delete_before;"
mysql -h "$DB_HOST" -u"$DB_USER" -p"$DB_PASS" "$DB_NAME" -e "DELETE FROM FileInfo WHERE CreateAt < $delete_before;"

# delete files
while read -r fp; do
        if [ -n "$fp" ]; then
                echo "$DATA_PATH""$fp"
                #shred -u "$DATA_PATH""$fp"
		mv "$DATA_PATH""$fp" /tmp/backup_mattermost/
        fi
done < /tmp/mattermost-paths.list

#TODO: delete empty folders

#cleanup after yourself
rm /tmp/mattermost-paths.list
exit 0

Based on https://github.com/aljazceru/mattermost-retention I personally stop the Mattermost service before running this script but I don't know if it is a strong requirement. I don't know what's the best way to integrate it.

anubister avatar Oct 11 '20 11:10 anubister