mirror of
https://github.com/aljazceru/mattermost-retention.git
synced 2025-12-17 05:54:26 +01:00
43
mattermost-retention.sh
Normal file → Executable file
43
mattermost-retention.sh
Normal file → Executable file
@@ -20,10 +20,18 @@ DB_HOST="127.0.0.1"
|
|||||||
RETENTION="30"
|
RETENTION="30"
|
||||||
|
|
||||||
# Mattermost data directory
|
# Mattermost data directory
|
||||||
DATA_PATH="/opt/mattermost/data/"
|
DATA_PATH="/mattermost/data/"
|
||||||
|
|
||||||
# Database drive (postgres OR mysql)
|
# Database drive (postgres OR mysql)
|
||||||
DB_DRIVE="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
|
# calculate epoch in milisec
|
||||||
@@ -40,15 +48,15 @@ case $DB_DRIVE in
|
|||||||
###
|
###
|
||||||
# get list of files to be removed
|
# 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
|
$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
|
||||||
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 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
|
$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
|
# cleanup db
|
||||||
###
|
###
|
||||||
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 posts where createat < $delete_before;"
|
||||||
psql -h "$DB_HOST" -U"$DB_USER" "$DB_NAME" -t -c "delete from fileinfo 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)
|
mysql)
|
||||||
@@ -57,15 +65,15 @@ case $DB_DRIVE in
|
|||||||
###
|
###
|
||||||
# get list of files to be removed
|
# 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
|
$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
|
||||||
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 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
|
$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
|
# cleanup db
|
||||||
###
|
###
|
||||||
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 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;"
|
$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."
|
echo "Unknown DB_DRIVE option. Currently ONLY mysql AND postgres are available."
|
||||||
@@ -73,15 +81,18 @@ case $DB_DRIVE in
|
|||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
|
|
||||||
###
|
###
|
||||||
# delete files
|
# delete files
|
||||||
###
|
###
|
||||||
while read -r fp; do
|
for fp in `cat /tmp/mattermost-paths.list`
|
||||||
|
do
|
||||||
if [ -n "$fp" ]; then
|
if [ -n "$fp" ]; then
|
||||||
echo "$DATA_PATH""$fp"
|
echo "$DATA_PATH""$fp"
|
||||||
shred -u "$DATA_PATH""$fp"
|
$MM_DOCKER_CMD shred -u "$DATA_PATH""$fp"
|
||||||
fi
|
fi
|
||||||
done < /tmp/mattermost-paths.list
|
done
|
||||||
|
|
||||||
|
|
||||||
###
|
###
|
||||||
# cleanup after script execution
|
# cleanup after script execution
|
||||||
@@ -91,5 +102,5 @@ rm /tmp/mattermost-paths.list
|
|||||||
###
|
###
|
||||||
# cleanup empty data dirs
|
# cleanup empty data dirs
|
||||||
###
|
###
|
||||||
find $DATA_PATH -type d -empty -delete
|
$MM_DOCKER_CMD find $DATA_PATH -type d -empty -delete
|
||||||
exit 0
|
exit 0
|
||||||
|
|||||||
Reference in New Issue
Block a user