Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data rentention management #189

Open
anubister opened this issue Oct 11, 2020 · 5 comments
Open

Data rentention management #189

anubister opened this issue Oct 11, 2020 · 5 comments
Labels
enhancement help wanted Contributions welcome

Comments

@anubister
Copy link

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.

@kemenaran kemenaran added enhancement help wanted Contributions welcome labels Oct 14, 2020
@kemenaran
Copy link
Collaborator

This script looks pretty neat. I guess it could be integrated pretty easily, by putting it into a cron directory, and have it look at the yunohost settings to get the number of days of retention.

I don't really have the bandwidth to do improvements (other than bug fixes) right now (because life™), but I can definitely mentor someone willing to give it a shot :)

@freddewitt
Copy link

Do you think that it's could be possible to just deleted old attached files ?
I would love to deleted 2 years old attached files then 4 yrs old messages at the same time (or choose to)
Great work

@ericgaspar
Copy link
Member

ericgaspar commented Sep 11, 2023

quick implantation https://github.com/YunoHost-Apps/mattermost_ynh/tree/retention
I just changed mysql with psql

@anubister
Copy link
Author

quick implantation https://github.com/YunoHost-Apps/mattermost_ynh/tree/retention I just changed mysql with psql

I had to use a slightly different syntax:
psql -h "$DB_HOST" -U"$DB_USER" -d "$DB_NAME" -c ""
to be confirmed with more tests... (yunohost 11 only for the moment), but this is in good way!

@anubister
Copy link
Author

Do you think that it's could be possible to just deleted old attached files ? I would love to deleted 2 years old attached files then 4 yrs old messages at the same time (or choose to) Great work

Yes! I can make sense also from my point of view on a server with low resources to purge files and keep text messages which are much less consuming. You just have to comment this line https://github.com/YunoHost-Apps/mattermost_ynh/blob/retention/conf/retention.sh#L25 .

Probably we could split this script with 2 different parameters for posts and for files, add a cron task to automatise, and the job is done :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement help wanted Contributions welcome
Projects
None yet
Development

No branches or pull requests

4 participants