#!/bin/bash

#https://support.plex.tv/articles/201100678-repair-a-corrupt-database/

docker stop plex

plex_db_path=/mnt/disks/nvme/appdata/plex/database/Library/Application\ Support/Plex\ Media\ Server/Plug-in\ Support/Databases
plex_db=com.plexapp.plugins.library.db
plex_db_fullpath="$plex_db_path"/"$plex_db"
log_file=/mnt/user/logs/plex_check.log
backup_path=/mnt/user/Backup/Plex

#Check for Corruption
#You can run commands to check how corrupt the database might be. 
#Even if the results come back “OK”, it’s still possible for there to be issues with a database, though.

echo " " >> $log_file
echo "Plex Maintenance operations" >> $log_file
echo "Check db corruption" >> $log_file


cp "$plex_db_path"/com.plexapp.plugins.library.db "$plex_db_path"/com.plexapp.plugins.library.db.check.original

sqlite3 "$plex_db_path"/com.plexapp.plugins.library.db "DROP index 'index_title_sort_naturalsort'" >> $log_file
echo "sqlite3 "$plex_db_path"/com.plexapp.plugins.library.db "DROP index 'index_title_sort_naturalsort'"" >> $log_file
sqlite3 "$plex_db_path"/com.plexapp.plugins.library.db "DELETE from schema_migrations where version='20180501000000'"  >> $log_file
echo "sqlite3 "$plex_db_path"/com.plexapp.plugins.library.db "DELETE from schema_migrations where version='20180501000000'""  >> $log_file
sqlite3 "$plex_db_path"/com.plexapp.plugins.library.db "PRAGMA integrity_check"  >> $log_file
echo "sqlite3 "$plex_db_path"/com.plexapp.plugins.library.db "PRAGMA integrity_check""  >> $log_file




#Run a Repair
#Even if the integrity check doesn’t return errors, 
#it can still sometimes help to try and repair the database. 
#In order to try and “repair” the database, 
#you can perform a dump followed by a restore to get it back to normal. #
#Run the following commands in the Terminal in order.

echo " " >> $log_file
echo "Run Repair"  >> $log_file

cp "$plex_db_path"/com.plexapp.plugins.library.db "$plex_db_path"/com.plexapp.plugins.library.db.repair.original
echo "cp "$plex_db_path"/com.plexapp.plugins.library.db "$plex_db_path"/com.plexapp.plugins.library.db.repair.original" >> $log_file
sqlite3 "$plex_db_path"/com.plexapp.plugins.library.db "DROP index 'index_title_sort_naturalsort'"
echo "sqlite3 "$plex_db_path"/com.plexapp.plugins.library.db "DROP index 'index_title_sort_naturalsort'"" >> $log_file
sqlite3 "$plex_db_path"/com.plexapp.plugins.library.db "DELETE from schema_migrations where version='20180501000000'"
echo "sqlite3 "$plex_db_path"/com.plexapp.plugins.library.db "DELETE from schema_migrations where version='20180501000000'"" >> $log_file
sqlite3 "$plex_db_path"/com.plexapp.plugins.library.db .dump > "$plex_db_path"/dump.sql
echo "sqlite3 "$plex_db_path"/com.plexapp.plugins.library.db .dump > "$plex_db_path"/dump.sql" >> $log_file
rm  "$plex_db_path"/com.plexapp.plugins.library.db
echo "rm  "$plex_db_path"/com.plexapp.plugins.library.db" >> $log_file
sqlite3 "$plex_db_path"/com.plexapp.plugins.library.db < "$plex_db_path"/dump.sql
echo "sqlite3 "$plex_db_path"/com.plexapp.plugins.library.db < "$plex_db_path"/dump.sql" >> $log_file

chown -R nobody:users "$plex_db_path"
chmod -R 777 "$plex_db_path"

cp --force --backup=numbered "$plex_db_path"/dump.sql "$backup_path"/dump.sql
#tar -cvpzf "$backup_path"/db-wal.`date +\%b`.tar.gz -C "$plex_db_path"/com.plexapp.plugins.library.db-wal
#tar -cvpzf "$backup_path"/db-shm.`date +\%b`.tar.gz -C "$plex_db_path"/com.plexapp.plugins.library.db-shm

docker start plex

