-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmattermost-retention.sh
executable file
·106 lines (82 loc) · 3.29 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
96
97
98
99
100
101
102
103
104
105
106
#!/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="/mattermost/data/"
# Database drive (postgres OR mysql)
DB_DRIVE="postgres"
# Set the docker command prefex for accessing DB
DB_DOCKER_CMD=""
#DB_DOCKER_CMD="docker compose exec -it postgres" #for example
# Set the docker command prefex for accessing mattermost
#MM_DOCKER_CMD="docker compose exec -it mattermost" # for example
###
# 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
###
$DB_DOCKER_CMD psql -h "$DB_HOST" -U"$DB_USER" "$DB_NAME" -t -c "select path from fileinfo where createat < $delete_before;" > /tmp/mattermost-paths.list
$DB_DOCKER_CMD psql -h "$DB_HOST" -U"$DB_USER" "$DB_NAME" -t -c "select thumbnailpath from fileinfo where createat < $delete_before;" >> /tmp/mattermost-paths.list
$DB_DOCKER_CMD 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
###
$DB_DOCKER_CMD psql -h "$DB_HOST" -U"$DB_USER" "$DB_NAME" -t -c "delete from posts where createat < $delete_before;"
$DB_DOCKER_CMD 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
###
$DB_DOCKER_CMD 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
$DB_DOCKER_CMD 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
$DB_DOCKER_CMD 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
###
$DB_DOCKER_CMD mysql --password=$DB_PASS --user=$DB_USER --host=$DB_HOST --database=$DB_NAME --execute="delete from Posts where createat < $delete_before;"
$DB_DOCKER_CMD 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
###
for fp in `cat /tmp/mattermost-paths.list`
do
if [ -n "$fp" ]; then
echo "$DATA_PATH""$fp"
$MM_DOCKER_CMD shred -u "$DATA_PATH""$fp"
fi
done
###
# cleanup after script execution
###
rm /tmp/mattermost-paths.list
###
# cleanup empty data dirs
###
$MM_DOCKER_CMD find $DATA_PATH -type d -empty -delete
exit 0