1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/mattermost_ynh.git synced 2024-09-03 19:36:29 +02:00

add retention

This commit is contained in:
Éric Gaspar 2023-09-11 13:18:17 +02:00
parent 6e3a81f5d6
commit 5548649f1d
7 changed files with 69 additions and 0 deletions

1
conf/cron Normal file
View file

@ -0,0 +1 @@
0 0 1 * * __APP__ /__INSTALL_DIR__/retention.sh > /dev/null

41
conf/retention.sh Normal file
View file

@ -0,0 +1,41 @@
#!/bin/bash
# configure vars
DB_USER="__DB_USER__"
DB_NAME="__DB_NAME__"
DB_PASS="__DB_PWD__"
DB_HOST="localhost"
RETENTION="__RETENTION_DAY__" #number of days to *keep*; 93 ~ 3 months
DATA_DIR="__DATA_DIR__"
# 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
psql -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
psql -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
psql -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
psql -h "$DB_HOST" -u"$DB_USER" -p"$DB_PASS" "$DB_NAME" -e "SELECT * FROM Posts WHERE CreateAt < $delete_before;"
# cleanup db
psql -h "$DB_HOST" -u"$DB_USER" -p"$DB_PASS" "$DB_NAME" -e "DELETE FROM Posts WHERE CreateAt < $delete_before;"
psql -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_DIR""$fp"
#shred -u "$DATA_DIR""$fp"
mv "$DATA_DIR""$fp" /tmp/backup_mattermost/
fi
done < /tmp/mattermost-paths.list
#TODO: delete empty folders
#cleanup after yourself
rm /tmp/mattermost-paths.list
exit 0

View file

@ -35,6 +35,12 @@ ynh_backup --src_path="/etc/nginx/conf.d/$domain.d/$app.conf"
ynh_backup --src_path="/etc/logrotate.d/$app"
#=================================================
# BACKUP VARIOUS FILES
#=================================================
ynh_backup --src_path="/etc/cron.d/$app"
#=================================================
# BACKUP SYSTEMD
#=================================================

View file

@ -38,6 +38,12 @@ url=https://$domain$path
ynh_add_config --template="../conf/config.json" --destination="$install_dir/config/config.json"
# Data rentention management
ynh_add_config --template="../conf/retention.sh" --destination="$install_dir/retention.sh"
chmod 400 "$install_dir/retention.sh"
chown $app:$app "$install_dir/retention.sh"
#=================================================
# SYSTEM CONFIGURATION
#=================================================
@ -58,6 +64,10 @@ chown $app -R "/var/log/$app"
# Setup logrotate
ynh_use_logrotate
ynh_add_config --template="../conf/cron" --destination="/etc/cron.d/$app"
chown root: "/etc/cron.d/$app"
chmod 644 "/etc/cron.d/$app"
#=================================================
# START SYSTEMD SERVICE
#=================================================

View file

@ -32,6 +32,9 @@ ynh_remove_logrotate
# Remove the dedicated NGINX config
ynh_remove_nginx_config
# Remove a cron file
ynh_secure_remove --file="/etc/cron.d/$app"
# Remove the log file
ynh_secure_remove --file="/var/log/$app"

View file

@ -49,6 +49,10 @@ yunohost service add $app --description="Collaboration platform built for develo
ynh_restore_file --origin_path="/etc/logrotate.d/$app"
ynh_restore_file --origin_path="/etc/cron.d/$app"
chown root: "/etc/cron.d/$app"
chmod 644 "/etc/cron.d/$app"
#=================================================
# GENERIC FINALIZATION
#=================================================

View file

@ -111,6 +111,10 @@ yunohost service add $app --description="Collaboration platform built for develo
# Use logrotate to manage app-specific logfile(s)
ynh_use_logrotate --non-append
ynh_add_config --template="../conf/cron" --destination="/etc/cron.d/$app"
chown root: "/etc/cron.d/$app"
chmod 644 "/etc/cron.d/$app"
#=================================================
# Fix old migrations
#=================================================