From 27fdb084f9793c117270268b2b42a2c984e3eeec Mon Sep 17 00:00:00 2001
From: Clarth <clarth@admin@claytonia.net>
Date: Thu, 15 Feb 2024 09:30:08 -0500
Subject: [PATCH] Add Jellyfin_Fix_and_Scan.sh

I got tired of messing with corrupt library.db files and Jellyfin not starting.
This automates "fixing" the issue.
---
 Jellyfin_Fix_and_Scan.sh | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
 create mode 100644 Jellyfin_Fix_and_Scan.sh

diff --git a/Jellyfin_Fix_and_Scan.sh b/Jellyfin_Fix_and_Scan.sh
new file mode 100644
index 0000000..9cceb18
--- /dev/null
+++ b/Jellyfin_Fix_and_Scan.sh
@@ -0,0 +1,34 @@
+#!/bin/bash
+
+# Define your Jellyfin server URL and API key
+JELLYFIN_URL="http://127.0.0.1:8096"
+API_KEY="API KEY"
+
+# Run journalctl command and search for the specified error messages
+error_message_1="database disk image is malformed"
+error_message_2="database or disk is full"
+
+error_found_1=$(journalctl -u jellyfin --since "1 hour ago" | grep "$error_message_1")
+error_found_2=$(journalctl -u jellyfin --since "1 hour ago" | grep "$error_message_2")
+
+# If either error message is found, delete the database file and trigger library scan
+if [ -n "$error_found_1" ] || [ -n "$error_found_2" ]; then
+    echo "Error found in journal, deleting database file..."
+    rm -f /var/lib/jellyfin/data/library.db
+    echo "Database file deleted."
+
+    # Trigger library scan
+    echo "Triggering library scan..."
+    curl -X POST \
+        -H "Content-Type: application/json" \
+        -H "X-Emby-Token: $API_KEY" \
+        -d '{"Recursive":true}' \
+        "$JELLYFIN_URL/library/refresh"
+    echo "Library scan triggered."
+    
+    # Send Telegram message to admin
+    curl -s -X POST -d "chat_id=CHAT ID" -d text="Jellyfin library.db had issues today. Check the logs!" https://api.telegram.org/botBOT:TOKEN/sendMessage
+else
+    echo "No error found in journal."
+fi
+exit 0
\ No newline at end of file