Skip to content

Instantly share code, notes, and snippets.

@bonelifer
Forked from TehPeGaSuS/thelounge_purge.sh
Last active April 28, 2024 19:39
Show Gist options
  • Save bonelifer/6e6fcdcc46efba96aaccad126c7c9acc to your computer and use it in GitHub Desktop.
Save bonelifer/6e6fcdcc46efba96aaccad126c7c9acc to your computer and use it in GitHub Desktop.
Maintaince scripts for TheLounge IRC web client databases and text logs. Default systemd timer is 11pm. Includes installer script.
#!/bin/bash
################################################################################
# Name: installer.sh #
# Description: Install maintenance script for The Lounge IRC client. Cleans #
# SQLite db and text log files. #
# #
# Usage: ./installer.sh [options] #
# Describe how to use the script, including any options or flags. #
# #
################################################################################
# Check if dialog is installed, if not, offer to install it
if ! command -v dialog &> /dev/null; then
echo "'dialog' is not installed."
read -p "Do you want to install 'dialog' now? (y/n): " choice
case $choice in
y|Y )
sudo apt update
sudo apt install dialog
;;
* )
echo "Exiting installation."
exit 1
;;
esac
fi
# Function to display the menu
show_menu() {
clear
dialog --clear --title "Choose Maintenance Script" \
--menu "Choose which maintenance script to install:" 15 60 3 \
1 "Use thelounge_purge_multi_user (Multi-user)" \
2 "Use thelounge_purge_single_user.sh (Single-user)" \
3 "Exit" 2> /tmp/menu_choice.txt
# Check if the user canceled the dialog
if [ $? -ne 0 ]; then
clear
exit
fi
}
# Function to copy the selected script
copy_script() {
choice=$1
case $choice in
1) SCRIPT="thelounge_purge_multi_user.sh" ;;
2) SCRIPT="thelounge_purge_single_user.sh" ;;
esac
# Destination directory for the script
SCRIPT_DIR="$HOME/bin"
# Source directory where the script is located
SOURCE_DIR="$(pwd)"
# Check if the destination directory exists, if not, create it
if [ ! -d "$SCRIPT_DIR" ]; then
mkdir -p "$SCRIPT_DIR"
fi
# Copy the script to the destination directory
cp "$SOURCE_DIR/$SCRIPT" "$SCRIPT_DIR/"
# Create the service unit file only if a valid choice was made
if [ "$choice" == "1" ] || [ "$choice" == "2" ]; then
# Create the service unit file
SERVICE_FILE="/etc/systemd/system/thelounge_purge.service"
echo "[Unit]" > "$SERVICE_FILE"
echo "Description=Cleanup script for The Lounge" >> "$SERVICE_FILE"
echo "" >> "$SERVICE_FILE"
echo "[Service]" >> "$SERVICE_FILE"
echo "Type=oneshot" >> "$SERVICE_FILE"
echo "ExecStart=$SCRIPT_DIR/$SCRIPT" >> "$SERVICE_FILE"
echo "" >> "$SERVICE_FILE"
echo "[Install]" >> "$SERVICE_FILE"
echo "WantedBy=multi-user.target" >> "$SERVICE_FILE"
# Create the timer unit file
TIMER_FILE="/etc/systemd/system/thelounge_purge.timer"
echo "[Unit]" > "$TIMER_FILE"
echo "Description=Run cleanup script for The Lounge" >> "$TIMER_FILE"
echo "" >> "$TIMER_FILE"
echo "[Timer]" >> "$TIMER_FILE"
echo "OnCalendar=23:00:00" >> "$TIMER_FILE" # Hardcoded time (e.g., 11:00 PM)
echo "Persistent=true" >> "$TIMER_FILE"
echo "" >> "$TIMER_FILE"
echo "[Install]" >> "$TIMER_FILE"
echo "WantedBy=timers.target" >> "$TIMER_FILE"
# Reload systemd and enable/start the timer
sudo systemctl daemon-reload
sudo systemctl enable --now thelounge_purge.timer
else
echo "Skipping service installation and timer activation."
fi
echo "Installation complete."
}
# Main function
main() {
while true; do
show_menu
choice=$(cat /tmp/menu_choice.txt)
case $choice in
1) copy_script 1 ;;
2) copy_script 2 ;;
3) clear; exit ;;
*) echo "Invalid option"; exit 1 ;;
esac
done
}
main
#!/usr/bin/env bash
#################################################################################
# Description: #
# This script cleans up The Lounge's SQLite database, plaintext log files, #
# and uploaded files. #
# #
# Compatibility: #
# Tested on Ubuntu 20.04 and newer. #
# #
# Explainer: #
# - SQLite database cleanup is performed using a loop that iterates over all #
# SQLite database files found in the logs directory. #
# - Plaintext files cleanup is also performed using a loop, iterating over #
# all plaintext log files found in the logs directory. #
# #
# WARNING: #
# The Lounge owns the SQLite database. It assumes it is the sole program #
# that writes to the database and may not handle it well if a different #
# process is taking a write lock on the database. Stop The Lounge whenever #
# you mess with the database. #
# #
# ATTENTION: YOU HAVE BEEN WARNED! #
# #
# Requirements: #
# - Assumes The Lounge is managed by Docker Compose. #
# - Assumes Docker Compose project is named "thelounge" #
# - Assumes the data directory is mounted to "/data" #
# #
# Edit the paths/commands to fit your needs. #
#################################################################################
#-----------------------#
# TheLounge data folder #
#-----------------------#
theloungeData="$(pwd)/data/config/"
#-------------------------------------------------------#
# Max number of lines to be kept on plaintext log files #
#-------------------------------------------------------#
maxLinesLogs=1000
#-----------------------------------------------#
# Max number of days to keep on SQLite database #
#-----------------------------------------------#
maxSqliteDays=30
#-------------------------------------------------------------------------#
# Keep only chat messages? It removes messages like join, part, quit, etc #
# #
# 0 = no, 1 = yes #
#-------------------------------------------------------------------------#
keepOnlyMsg=0
#-----------------------------------------------------------#
# How many days to keep of uploaded files #
# #
# If you don't have file uploading enabled, set this to "0" #
#-----------------------------------------------------------#
maxUploadDays=7
#------------------------------------------------------------------------#
# Stop TheLounge Docker Compose project for cleanup #
# Assuming The Lounge is managed by Docker Compose and named "thelounge" #
#------------------------------------------------------------------------#
docker-compose down
#----------------------------------------------------#
# Cleanup of the SQLite database and plaintext files #
#----------------------------------------------------#
if [[ "$keepOnlyMsg" == 1 ]]; then
# SQLite database cleanup
sqlite3 "$theloungeData"/logs/thelounge.sqlite3 "delete from messages where type not in ('action', 'message', 'notice'); VACUUM;"
sqlite3 "$theloungeData"/logs/thelounge.sqlite3 "delete from messages where time < strftime('%s', datetime('now', '-$maxSqliteDays day'))*1000; VACUUM;"
# Plaintext files cleanup
find "$theloungeData"/logs/ -name "*.log" -print0 | while IFS= read -r -d $'\0' file; do
sed -i '/^\[.*\] \*\*\*/d' "$file"
tail -n "$maxLinesLogs" "$file" > /tmp/file.tmp
mv /tmp/file.tmp "$file"
done
else
# SQLite database cleanup
sqlite3 "$theloungeData"/logs/thelounge.sqlite3 "delete from messages where time < strftime('%s', datetime('now', '-$maxSqliteDays day'))*1000; VACUUM;"
# Plaintext files cleanup
find "$theloungeData"/logs/ -name "*.log" -print0 | while IFS= read -r -d $'\0' file; do
tail -n "$maxLinesLogs" "$file" > /tmp/file.tmp
mv /tmp/file.tmp "$file"
done
fi
#---------------------------------------#
# Cleanup of uploaded files, if enabled #
#---------------------------------------#
if [ "$maxUploadDays" -ge 1 ]; then
find "$theloungeData"/uploads/ -mindepth 1 -type f -ctime +"$maxUploadDays" -delete
find "$theloungeData"/uploads/ -mindepth 1 -type d -empty -delete
else
exit
fi
#-------------------------------------------------------------------#
# Start TheLounge Docker Compose project after all has been completed #
# Assuming The Lounge is managed by Docker Compose and named "thelounge" #
#-------------------------------------------------------------------#
docker-compose up -d
#!/usr/bin/env bash
#################################################################################
# Description: #
# This script is designed to clean up The Lounge's SQLite database, plaintext #
# log files, and uploaded files. It ensures the maintenance of a clean and #
# efficient environment for The Lounge messaging platform. #
# #
# Compatibility: #
# Tested on Ubuntu 20.04 and newer. #
# #
# Explainer: #
# - SQLite database cleanup is performed directly on a single SQLite #
# database file without using a loop. #
# - Plaintext files cleanup is performed similarly to thelounge_purge.sh, #
# using a loop to iterate over all plaintext log files found in the #
# logs directory. #
# #
# WARNING: #
# The Lounge owns the SQLite database. It assumes it is the sole program #
# that writes to the database and may not handle it well if a different #
# process is taking a write lock on the database. Stop The Lounge whenever #
# you mess with the database. #
# #
# ATTENTION: YOU HAVE BEEN WARNED! #
# #
# Requirements: #
# - Assumes The Lounge is managed by Docker Compose. #
# - Assumes Docker Compose project is named "thelounge" #
# - Assumes the data directory is mounted to "/data" #
# #
# Edit the paths/commands to fit your needs. #
#################################################################################
#-----------------------#
# TheLounge data folder #
#-----------------------#
theloungeData="$(pwd)/data/config/"
#-------------------------------------------------------#
# Max number of lines to be kept on plaintext log files #
#-------------------------------------------------------#
maxLinesLogs=1000
#-----------------------------------------------#
# Max number of days to keep on SQLite database #
#-----------------------------------------------#
maxSqliteDays=30
#-------------------------------------------------------------------------#
# Keep only chat messages? It removes messages like join, part, quit, etc #
# #
# 0 = no, 1 = yes #
#-------------------------------------------------------------------------#
keepOnlyMsg=0
#-----------------------------------------------------------#
# How many days to keep of uploaded files #
# #
# If you don't have file uploading enabled, set this to "0" #
#-----------------------------------------------------------#
maxUploadDays=7
#------------------------------------------------------------------------#
# Stop TheLounge Docker Compose project for cleanup #
# Assuming The Lounge is managed by Docker Compose and named "thelounge" #
#------------------------------------------------------------------------#
docker-compose down
#----------------------------------------------------#
# Cleanup of the SQLite database and plaintext files #
#----------------------------------------------------#
if [[ "$keepOnlyMsg" == 1 ]]; then
# SQLite database cleanup
for filename in "$theloungeData"/logs/*.sqlite3; do
sqlite3 "$filename" "delete from messages where type not in ('action', 'message', 'notice'); VACUUM;"
sqlite3 "$filename" "delete from messages where time < strftime('%s', datetime('now', '-$maxSqliteDays day'))*1000; VACUUM;"
done
# Plaintext files cleanup
find "$theloungeData"/logs/ -name "*.log" -print0 | while IFS= read -r -d $'\0' file; do
sed -i '/^\[.*\] \*\*\*/d' "$file"
tail -n "$maxLinesLogs" "$file" > /tmp/file.tmp
mv /tmp/file.tmp "$file"
done
else
# SQLite database cleanup
for filename in "$theloungeData"/logs/*.sqlite3; do
sqlite3 "$filename" "delete from messages where time < strftime('%s', datetime('now', '-$maxSqliteDays day'))*1000; VACUUM;"
done
# Plaintext files cleanup
find "$theloungeData"/logs/ -name "*.log" -print0 | while IFS= read -r -d $'\0' file; do
tail -n "$maxLinesLogs" "$file" > /tmp/file.tmp
mv /tmp/file.tmp "$file"
done
fi
#---------------------------------------#
# Cleanup of uploaded files, if enabled #
#---------------------------------------#
if [ "$maxUploadDays" -ge 1 ]; then
find "$theloungeData"/uploads/ -mindepth 1 -type f -ctime +"$maxUploadDays" -delete
find "$theloungeData"/uploads/ -mindepth 1 -type d -empty -delete
else
exit
fi
#-------------------------------------------------------------------#
# Start TheLounge Docker Compose project after all has been completed #
# Assuming The Lounge is managed by Docker Compose and named "thelounge" #
#-------------------------------------------------------------------#
docker-compose up -d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment