Provide means to ignore selected DB recovery errors

When recovering a damaged database,  it is sometimes necessary to ignore constraint errors resulting from damaged indexes.

This update provides command line options to support this:
1.   -i  or -f  -   Ignore DB check errors / Force acceptance
2.   -p  -  When importing viewstate from another DB, purge duplicate counts.
               (Use with extreme caution).
This commit is contained in:
ChuckPa 2023-06-25 16:20:32 -04:00
parent 77415c7580
commit 94a3c3923b
No known key found for this signature in database
GPG Key ID: 3CE28A0F6BC31B5B
2 changed files with 63 additions and 11 deletions

View File

@ -2,12 +2,12 @@
######################################################################### #########################################################################
# Plex Media Server database check and repair utility script. # # Plex Media Server database check and repair utility script. #
# Maintainer: ChuckPa # # Maintainer: ChuckPa #
# Version: v1.0.8 # # Version: v1.0.9 #
# Date: 01-Jun-2023 # # Date: 25-Jun-2023 #
######################################################################### #########################################################################
# Version for display purposes # Version for display purposes
Version="v1.0.8" Version="v1.0.9"
# Flag when temp files are to be retained # Flag when temp files are to be retained
Retain=0 Retain=0
@ -23,6 +23,12 @@ StopCommand=""
# By default, require root privilege # By default, require root privilege
RootRequired=1 RootRequired=1
# By default, Errors are fatal.
IgnoreErrors=0
# By default, Duplicate view states not purged
PurgeDuplicates=0
# Keep track of how many times the user's hit enter with no command (implied EOF) # Keep track of how many times the user's hit enter with no command (implied EOF)
NullCommands=0 NullCommands=0
@ -690,6 +696,7 @@ DoIndex() {
Damaged=1 Damaged=1
CheckedDB=1 CheckedDB=1
Fail=1 Fail=1
[ $IgnoreErrors -eq 1 ] && Fail=0
fi fi
@ -703,6 +710,8 @@ DoIndex() {
Output "Backing up of databases" Output "Backing up of databases"
MakeBackups "Reindex" MakeBackups "Reindex"
Result=$? Result=$?
[ $IgnoreErrors -eq 1 ] && Result=0
if [ $Result -eq 0 ]; then if [ $Result -eq 0 ]; then
WriteLog "Reindex - MakeBackup - PASS" WriteLog "Reindex - MakeBackup - PASS"
else else
@ -716,6 +725,8 @@ DoIndex() {
Output "Reindexing main database" Output "Reindexing main database"
"$PLEX_SQLITE" $CPPL.db 'REINDEX;' "$PLEX_SQLITE" $CPPL.db 'REINDEX;'
Result=$? Result=$?
[ $IgnoreErrors -eq 1 ] && Result=0
if SQLiteOK $Result; then if SQLiteOK $Result; then
Output "Reindexing main database successful." Output "Reindexing main database successful."
WriteLog "Reindex - Reindex: $CPPL.db - PASS" WriteLog "Reindex - Reindex: $CPPL.db - PASS"
@ -728,6 +739,8 @@ DoIndex() {
Output "Reindexing blobs database" Output "Reindexing blobs database"
"$PLEX_SQLITE" $CPPL.blobs.db 'REINDEX;' "$PLEX_SQLITE" $CPPL.blobs.db 'REINDEX;'
Result=$? Result=$?
[ $IgnoreErrors -eq 1 ] && Result=0
if SQLiteOK $Result; then if SQLiteOK $Result; then
Output "Reindexing blobs database successful." Output "Reindexing blobs database successful."
WriteLog "Reindex - Reindex: $CPPL.blobs.db - PASS" WriteLog "Reindex - Reindex: $CPPL.blobs.db - PASS"
@ -818,6 +831,7 @@ DoRepair() {
Output "Exporting Main DB" Output "Exporting Main DB"
"$PLEX_SQLITE" $CPPL.db ".output '$TMPDIR/library.plexapp.sql-$TimeStamp'" .dump "$PLEX_SQLITE" $CPPL.db ".output '$TMPDIR/library.plexapp.sql-$TimeStamp'" .dump
Result=$? Result=$?
[ $IgnoreErrors -eq 1 ] && Result=0
if ! SQLiteOK $Result; then if ! SQLiteOK $Result; then
# Cannot dump file # Cannot dump file
@ -832,6 +846,8 @@ DoRepair() {
Output "Exporting Blobs DB" Output "Exporting Blobs DB"
"$PLEX_SQLITE" $CPPL.blobs.db ".output '$TMPDIR/blobs.plexapp.sql-$TimeStamp'" .dump "$PLEX_SQLITE" $CPPL.blobs.db ".output '$TMPDIR/blobs.plexapp.sql-$TimeStamp'" .dump
Result=$? Result=$?
[ $IgnoreErrors -eq 1 ] && Result=0
if ! SQLiteOK $Result; then if ! SQLiteOK $Result; then
# Cannot dump file # Cannot dump file
@ -869,6 +885,8 @@ DoRepair() {
Output "Importing Blobs DB." Output "Importing Blobs DB."
"$PLEX_SQLITE" "$TMPDIR/$CPPL.blobs.db-REPAIR-$TimeStamp" < "$TMPDIR/blobs.plexapp.sql-$TimeStamp" "$PLEX_SQLITE" "$TMPDIR/$CPPL.blobs.db-REPAIR-$TimeStamp" < "$TMPDIR/blobs.plexapp.sql-$TimeStamp"
Result=$? Result=$?
[ $IgnoreErrors -eq 1 ] && Result=0
if ! SQLiteOK $Result ; then if ! SQLiteOK $Result ; then
Output "Error $Result from Plex SQLite while importing from '$TMPDIR/blobs.plexapp.sql-$TimeStamp'" Output "Error $Result from Plex SQLite while importing from '$TMPDIR/blobs.plexapp.sql-$TimeStamp'"
WriteLog "Repair - Cannot import blobs database from '$TMPDIR/blobs.plexapp.sql-$TimeStamp' - FAIL ($Result)" WriteLog "Repair - Cannot import blobs database from '$TMPDIR/blobs.plexapp.sql-$TimeStamp' - FAIL ($Result)"
@ -1275,13 +1293,15 @@ DoImport(){
printf 'Importing Viewstate & History data...' printf 'Importing Viewstate & History data...'
"$PLEX_SQLITE" "$TMPDIR/$CPPL.db-IMPORT-$TimeStamp" < "$TMPDIR/Viewstate.sql-$TimeStamp" 2> /dev/null "$PLEX_SQLITE" "$TMPDIR/$CPPL.db-IMPORT-$TimeStamp" < "$TMPDIR/Viewstate.sql-$TimeStamp" 2> /dev/null
# # Purge duplicates (violations of unique constraint) # Purge duplicates (violations of unique constraint)
# cat <<EOF | "$PLEX_SQLITE" "$TMPDIR/$CPPL.db-IMPORT-$TimeStamp" if [ $PurgeDuplicates -eq 1 ]; then
# DELETE FROM metadata_item_settings cat <<EOF | "$PLEX_SQLITE" "$TMPDIR/$CPPL.db-IMPORT-$TimeStamp"
# WHERE id in (SELECT MIN(id) DELETE FROM metadata_item_settings
# FROM metadata_item_settings WHERE id in (SELECT MIN(id)
# GROUP BY guid HAVING COUNT(guid) > 1); FROM metadata_item_settings
#EOF GROUP BY guid HAVING COUNT(guid) > 1);
EOF
fi
# Make certain the resultant DB is OK # Make certain the resultant DB is OK
Output " done." Output " done."
@ -1385,6 +1405,18 @@ DoStop(){
return $Result return $Result
} }
# Do command line switches
DoOptions() {
for i in $@
do
Opt="$(echo $i | cut -c1-2 | tr [A-Z] [a-z])"
[ "$Opt" = "-i" ] && IgnoreErrors=1 && WriteLog "Opt: Database error checking ignored."
[ "$Opt" = "-f" ] && IgnoreErrors=1 && WriteLog "Opt: Database error checking ignored."
[ "$Opt" = "-p" ] && PurgeDuplicates=1 && WriteLog "Opt: Purge duplidate watch history viewstates."
done
}
##### UpdateTimestamp ##### UpdateTimestamp
DoUpdateTimestamp() { DoUpdateTimestamp() {
TimeStamp="$(date "+%Y-%m-%d_%H.%M.%S")" TimeStamp="$(date "+%Y-%m-%d_%H.%M.%S")"
@ -1429,6 +1461,13 @@ echo " "
WriteLog "============================================================" WriteLog "============================================================"
WriteLog "Session start: Host is $HostType" WriteLog "Session start: Host is $HostType"
# Command line hidden options must come before commands
while [ "$(echo $1 | cut -c1)" = "-" ]
do
DoOptions "$1"
shift
done
# Make sure we have a logfile # Make sure we have a logfile
touch "$LOGFILE" touch "$LOGFILE"
@ -1445,7 +1484,6 @@ mkdir -p "$DBDIR/$DBTMP"
export TMPDIR="$DBTMP" export TMPDIR="$DBTMP"
export TMP="$DBTMP" export TMP="$DBTMP"
# If command line args then set flag # If command line args then set flag
Scripted=0 Scripted=0
[ "$1" != "" ] && Scripted=1 [ "$1" != "" ] && Scripted=1

View File

@ -9,6 +9,20 @@
# Release Info: # Release Info:
v1.0.9
- When recovering a damaged database, it is sometimes necessary to ignore constraint errors resulting from damaged indexes.
This update provides command line options to support this:
1. -i or -f - Ignore DB check errors / Force acceptance.
2. -p - When importing viewstate from another DB, purge duplicate counts.
(Use with extreme caution).
Usage: DBRepair.sh [Options] commands
Example: DBRepair.sh -i -p start auto stop exit
v1.0.8 v1.0.8
- Require root UID (super user). - Require root UID (super user).
Requiring root UID gives the script the privilege necessary to set the database ownership Requiring root UID gives the script the privilege necessary to set the database ownership