forked from aljazceru/mattermost-retention
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mattermost-retention.sh
95 lines (76 loc) · 2.87 KB
/
mattermost-retention.sh
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/bin/bash
###
# configure vars
####
# Database user name
DB_USER="mmuser"
# Database name
DB_NAME="mattermost"
# Database password
DB_PASS=""
# Database hostname
DB_HOST="127.0.0.1"
# How many days to keep of messages/files?
RETENTION="30"
# Mattermost data directory
DATA_PATH="/opt/mattermost/data/"
# Database drive (postgres OR mysql)
DB_DRIVE="mysql"
###
# calculate epoch in milisec
###
delete_before=$(date --date="$RETENTION day ago" "+%s%3N")
echo $(date --date="$RETENTION day ago")
case $DB_DRIVE in
postgres)
echo "Using postgres database."
export PGPASSWORD=$DB_PASS
###
# get list of files to be removed
###
psql -h "$DB_HOST" -U"$DB_USER" "$DB_NAME" -t -c "select path from fileinfo where createat < $delete_before;" > /tmp/mattermost-paths.list
psql -h "$DB_HOST" -U"$DB_USER" "$DB_NAME" -t -c "select thumbnailpath from fileinfo where createat < $delete_before;" >> /tmp/mattermost-paths.list
psql -h "$DB_HOST" -U"$DB_USER" "$DB_NAME" -t -c "select previewpath from fileinfo where createat < $delete_before;" >> /tmp/mattermost-paths.list
###
# cleanup db
###
psql -h "$DB_HOST" -U"$DB_USER" "$DB_NAME" -t -c "delete from posts where createat < $delete_before;"
psql -h "$DB_HOST" -U"$DB_USER" "$DB_NAME" -t -c "delete from fileinfo where createat < $delete_before;"
;;
mysql)
echo "Using mysql database."
###
# get list of files to be removed
###
mysql --password=$DB_PASS --user=$DB_USER --host=$DB_HOST --database=$DB_NAME --execute="select path from FileInfo where createat < $delete_before;" > /tmp/mattermost-paths.list
mysql --password=$DB_PASS --user=$DB_USER --host=$DB_HOST --database=$DB_NAME --execute="select thumbnailpath from FileInfo where createat < $delete_before;" >> /tmp/mattermost-paths.list
mysql --password=$DB_PASS --user=$DB_USER --host=$DB_HOST --database=$DB_NAME --execute="select previewpath from FileInfo where createat < $delete_before;" >> /tmp/mattermost-paths.list
###
# cleanup db
###
mysql --password=$DB_PASS --user=$DB_USER --host=$DB_HOST --database=$DB_NAME --execute="delete from Posts where createat < $delete_before;"
mysql --password=$DB_PASS --user=$DB_USER --host=$DB_HOST --database=$DB_NAME --execute="delete from FileInfo where createat < $delete_before;"
;;
*)
echo "Unknown DB_DRIVE option. Currently ONLY mysql AND postgres are available."
exit 1
;;
esac
###
# delete files
###
while read -r fp; do
if [ -n "$fp" ]; then
echo "$DATA_PATH""$fp"
shred -u "$DATA_PATH""$fp"
fi
done < /tmp/mattermost-paths.list
###
# cleanup after script execution
###
rm /tmp/mattermost-paths.list
###
# cleanup empty data dirs
###
find $DATA_PATH -type d -empty -delete
exit 0