GithubHelp home page GithubHelp logo

plexdbrepair's Introduction

PlexDBRepair

GitHub issues Release Download latest release Download total master Maintenance

Introduction

DBRepair provides database repair and maintenance for the most common Plex Media Server database problems. It is a simple menu-driven utility with a command line backend.

DBRepair is run from a command line (terminal or ssh/putty session) which has sufficient privilege to read/write the databases (minimum). If sufficient privleges exist (root), and supported by the environment, the options to start and stop PMS are presented as well. (Some envionments require DBRepair to run as the 'root' user.)

Situations and errors commonly seen include:

    1. Searching is sluggish
    2. Database is malformed / damaged / corrupted
    3. Database has bloated from media addition or changes
    4. Damaged indexes damaged
    5. Importing data from other databases (watch history)
    6. Cleaning up operational temp files which PMS has forgotten to clean up

Functions provided

The utility accepts command names. Command names may be upper/lower case and may also be abbreviated (4 character minimum).

The following commands (or their number), listed in alphabetical order, are accepted as input.

   AUTO(matic)  - Automatically check, repair/optimize, and reindex the databases in one step.
   CHEC(k)      - Check the main and blob databases integrity
   EXIT         - Exit the utility
   IGNOre/HONOr - Ignore/Honor constraint errors when IMPORTing additional data into DB.
   IMPO(rt)     - Import viewstate / watch history from another database
   PRUN(e)      - Prune (remove) old image files from transcoder cache diretory
   REIN(dex)    - Rebuild the database indexes
   REPL(ace)    - Replace the existing databases with a PMS-generated backup
   SHOW         - Show the log file
   STAR(t)      - Start PMS (not available on all platforms)
   STOP         - Stop PMS  (not available on all platforms)
   UNDO         - UNDO the last operation
   VACU(um)     - Vacuum the databases

The menu

The menu gives you the option to enter either a 'command number' or the 'command name/abbreviation'. For clarity, each command's name is 'quoted'.

      Plex Media Server Database Repair Utility (_host_configuration_name_)
                       Version v1.03.00

  Select

  1 - 'stop'      - Stop PMS.
  2 - 'automatic' - Check, Repair/Optimize, and Reindex Database in one step.
  3 - 'check'     - Perform integrity check of database.
  4 - 'vacuum'    - Remove empty space from database without optimizing.
  5 - 'repair'    - Repair/Optimize databases.
  6 - 'reindex'   - Rebuild database database indexes.
  7 - 'start'     - Start PMS

  8 - 'import'    - Import watch history from another database independent of Plex. (risky).
  9 - 'replace'   - Replace current databases with newest usable backup copy (interactive).
 10 - 'show'      - Show logfile.
 11 - 'status'    - Report status of PMS (run-state and databases).
 12 - 'undo'      - Undo last successful command.

 21 - 'prune'     - Prune (remove) old image files (jpeg,jpg,png) from PhotoTranscoder cache.
 42 - 'ignore'    - Ignore duplicate/constraint errors.

 88 - 'update'    - Check for updates.
 99 - 'quit'      - Quit immediately.  Keep all temporary files.
      'exit'      - Exit with cleanup options.

Enter command # -or- command name (4 char min) :



## Hosts currently supported

        1. Apple (MacOS)
        2. ASUSTOR
        3. Docker containers via 'docker exec' command (inside the running container environment)
           - Plex,inc.
           - Linuxserver.io
           - BINHEX
           - HOTIO
           - Podman (libgpod)
        4. Linux workstation & server
        5. MacOS
        6. Netgear (OS5 Linux-based systems)
        7. QNAP (QTS & QuTS)
        8. Synology (DSM 6 & DSM 7)
        9. Western Digital (OS5)

Installation

Where to place the utility varies from host to host.
Please use this table as a reference.

Some hosts will not be listed here by name (e.g. Unraid, Proxmox).
They will likely be supported by the container/VM PMS runs in.
    Vendor             | Shared folder name  |  Recommended directory
    -------------------+---------------------+------------------------------------------
    Apple              | Downloads           |  ~/Downloads
    Arch Linux         | N/A                 |  Anywhere
    ASUSTOR            | Public              |  /volume1/Public
    Binhex             | N/A                 |  Container root (adjacent /config)
    Docker (Plex,LSIO) | N/A                 |  Container root (adjacent /config)
    Hotio              | N/A                 |  Container root (adjacent /config)
    Kubernetes         | N/A                 |  Container root (adjacent /config)
    Linux (wkstn/svr)  | N/A                 |  Anywhere
    MacOS              | N/A                 |  Anywhere
    Netgear (ReadyNAS) | "your_choice"       |  "/data/your_choice"
    QNAP (QTS/QuTS)    | Public              |  /share/Public
    SNAP               | N/A                 |  Anywhere
    Synology (DSM 6)   | Plex                |  /volume1/Plex             (change volume as required)
    Synology (DSM 7)   | PlexMediaServer     |  /volume1/PlexMediaServer  (change volume as required)
    Western Digital    | Public              |  /mnt/HD/HD_a2/Public      (Does not support 'MyCloudHome' series)
Plex,inc and LSIO docker images are included in "Docker" platform category independent of the actual host.

General installation and usage instructions

    1. Open your browser to https://github.com/ChuckPa/PlexDBRepair/releases/latest
    2. Download the source code (tar.gz or ZIP) file

    3. Knowing the file name will always be of the form 'PlexDBRepair-X.Y.Z.tar.gz'
       --  where X.Y.Z is the release number.  Use the real values in place of X, Y, and Z.
    4. Place the tar.gz file in the appropriate directory on the system you'll use it.
    5. Open a command line session (usually Terminal or SSH)
    6. Elevate privilege level to root (sudo) if needed.
    7. Extract the utility from the tar or zip file
    8. 'cd' into the extraction directory
    9. Give DBRepair.sh 'execute' permission  (chmod +x)
   10. Invoke ./DBRepair.sh

EXAMPLE: To install & launch on Synology DSM 6 / DSM 7

    cd /volume1/Plex    # use /volume1/PlexMediaServer on DSM 7
    sudo bash
    tar xf PlexDBRepair-x.y.z.tar.gz
    cd PlexDBRepair-x.y.z
    chmod +x DBRepair.sh
    ./DBRepair.sh

EXAMPLE: Using DBRepair inside containers (manual start/stop included)

(Select containers allow stopping/starting PMS from the menu. See menu for details)

    sudo docker exec -it plex /bin/bash

    # extract from downloaded version file name then cd into directory
    tar xf PlexDBRepair-1.0.0.tar.gz
    cd PlexDBRepair-1.0.0
    chmod +x DBRepair.sh
    ./DBRepair.sh
###    EXAMPLE:  Using DBRepair on regular Linux native host (Workstation/Server)
    sudo bash
    cd /path/to/DBRepair.tar
    tar xf PlexDBRepair-1.0.0.tar.gz
    cd PlexDBRepair-1.0.0
    chmod +x DBRepair.sh
    ./DBRepair.sh stop auto start exit

###    EXAMPLE: Using DBRepair from the command line on MacOS (on the administrator account)
    osascript -e 'quit app "Plex Media Server"'
    cd ~/Downloads
    tar xvf PlexDBRepai PlexDBRepair-1.0.0.tar.gz
    cd PlexDBRepair-1.0.0

    chmod +x DBRepair.sh
    ./DBRepair.sh

Typical usage

This utility can only operate on PMS when PMS is in the stopped state. If PMS is running when you startup the utility, it will tell you.

These examples

A. The most common usage will be the "Automatic" function.

Automatic mode is where DBRepair determines which steps are needed to make your database run optimally.
For most users, Automatic is equivalent to 'Check, Repair, Reindex'.
This repairs minor damage, vacuums out all the unused records, and rebuilds search indexes in one step.

B. Database is malformed (Backups of com.plexapp.plugins.library.db and com.plexap.plugins.library.blobs.db available) Note: You may attempt "Repair" sequence

1. (3)  Check   - Confirm either main or blobs database is damaged
2. (9)  Replace - Use the most recent valid backup -- OR -- (5) Repair.  Check date/time stamps for best action.
                -- If Replace fails, use Repair (5)
                -- (Replace can fail if the database has been damaged for a long time.)
3. (6)  Reindex - Generate new indexes so PMS doesn't need to at startup
4. (99) Exit

C. Database is malformed - No Backups 1. (3) Check - Confirm either main or blobs database is damaged 2. (5) Repair - Salavage as much as possible from the databases and rebuild them into a usable database. 3. (6) Reindex - Generate new indexes so PMS doesn't need to at startup 4. (99) Exit

C. Database sizes excessively large when compared to amount of media indexed (item count) 1. (3) Check - Make certain both databases are fully intact (repair if needed) 2. (4) Vacuum - Instruct SQLite to rebuild its tables and recover unused space. 3. (6) Reindex - Rebuild Indexes. 4. (99) Exit

D. User interface has become 'sluggish' as more media was added 1. (3) Check - Confirm there is no database damage 2. (5) Repair - You are not really repairing. You are rebuilding the DB in perfect sorted order. 3. (6) Reindex - Rebuild Indexes. 4. (99) Exit

E. Undo Undo is a special case where you need the utility to backup ONE step. This is rarely needed. The only time you might want/need to backup one step is if Replace leaves you worse off than you were before. In this case, UNDO then Repair. Undo can only undo the single most-recent action. (Note: In a future release, you will be able to 'undo' every action taken until the DBs are in their original state)

Special considerations:

1. As stated above, this utility requires PMS to be stopped in order to do what it does.
2. - This utility CAN sit at the menu prompt with PMS running.
   - You did a few things and want to check BEFORE exiting the utility
   - If you don't like how it worked out,
    -- STOP PMS
    -- UNDO the last action and do something else
    -- OR do more things to the databases
3. When satisfied,  Exit the utility.
   - There is no harm in keeping the database temp files (except for space used)
   - ALL database temps are named with date-time stamps in the name to avoid confusion.
4. The Logfile ('show' command) shows all actions performed WITH timestamp so you can locate
   intermediate databases if desired for special / manual recovery cases.

Attention:

The behavior of command "99" is different than command "Exit" This is intentional.

"99" is the "Get out now, Keep all intermediate/temp files. -- This is for when DB operations keep getting worse and you don't know what to do. "99" is an old 'Get Smart' TV series reference where agent 99 would try to save agent 86 from harm.

"99" was originally going to be "Quit immediately save all files" but development feedback resulted in this configuration

"Exit" is the preferred method to leave.

"Quit" was desired instead of "99" but there are those who didn't understand the difference or references.

If community feedback wants both "Quit. save temps" and "Exit, delete temps", behavior is easily changed.

Also please be aware the script understands interactive versus scripted mode.

Scripting support

Certain platforms don't provide for each command line access. To support those products, this utility can be operated by adding command line arguments.

Another use of this feature is to automate Plex Database maintenance44 ( Stop Plex, Run this sequence, Start Plex ) at a time when the server isn't busy

The command line arguments are the same as if typing at the menu.

Example: ./DBRepair.sh stop auto start exit

This executes: Stop PMS, Automatic (Check, Repair, Reindex), Start PMS, and Exit commands

Exiting

When exiting, you will be asked whether to keep the interim temp files created during this session. If you've encountered any difficulties or aren't sure what to do, don't delete them. You'll be able to ask in the Plex forums about what to do. Be prepared to present the log file to them.

Sample interactive session

This is a typical manual session if you aren't sure what to do and want the tool to decide.

bash-4.4# #=======================================================================================================
bash-4.4# ./DBRepair.sh



      Plex Media Server Database Repair Utility (Ubuntu 20.04.6 LTS)
                       Version v1.03.01

Select

  1 - 'stop'      - Stop PMS.
  2 - 'automatic' - Check, Repair/Optimize, and Reindex Database in one step.
  3 - 'check'     - Perform integrity check of database.
  4 - 'vacuum'    - Remove empty space from database without optimizing.
  5 - 'repair'    - Repair/Optimize databases.
  6 - 'reindex'   - Rebuild database database indexes.
  7 - 'start'     - Start PMS

  8 - 'import'    - Import watch history from another database independent of Plex. (risky).
  9 - 'replace'   - Replace current databases with newest usable backup copy (interactive).
 10 - 'show'      - Show logfile.
 11 - 'status'    - Report status of PMS (run-state and databases).
 12 - 'undo'      - Undo last successful command.

 21 - 'prune'     - Prune (remove) old image files (jpeg,jpg,png) from PhotoTranscoder cache.
 42 - 'ignore'    - Ignore duplicate/constraint errors.

 88 - 'update'    - Check for updates.
 99 - 'quit'      - Quit immediately.  Keep all temporary files.
      'exit'      - Exit with cleanup options.

Enter command # -or- command name (4 char min) :  1

Stopping PMS.
Stopped PMS.

Select

  1 - 'stop'      - Stop PMS.
  2 - 'automatic' - Check, Repair/Optimize, and Reindex Database in one step.
  3 - 'check'     - Perform integrity check of database.
  4 - 'vacuum'    - Remove empty space from database without optimizing.
  5 - 'repair'    - Repair/Optimize databases.
  6 - 'reindex'   - Rebuild database database indexes.
  7 - 'start'     - Start PMS

  8 - 'import'    - Import watch history from another database independent of Plex. (risky).
  9 - 'replace'   - Replace current databases with newest usable backup copy (interactive).
 10 - 'show'      - Show logfile.
 11 - 'status'    - Report status of PMS (run-state and databases).
 12 - 'undo'      - Undo last successful command.

 21 - 'prune'     - Prune (remove) old image files (jpeg,jpg,png) from PhotoTranscoder cache.
 42 - 'ignore'    - Ignore duplicate/constraint errors.

 88 - 'update'    - Check for updates.
 99 - 'quit'      - Quit immediately.  Keep all temporary files.
      'exit'      - Exit with cleanup options.

Enter command # -or- command name (4 char min) : auto


Checking the PMS databases
Check complete.  PMS main database is OK.
Check complete.  PMS blobs database is OK.

Exporting current databases using timestamp: 2023-02-25_16.15.11
Exporting Main DB
Exporting Blobs DB
Successfully exported the main and blobs databases.  Proceeding to import into new databases.
Importing Main DB.
Importing Blobs DB.
Successfully imported data from SQL files.
Verifying databases integrity after importing.
Verification complete.  PMS main database is OK.
Verification complete.  PMS blobs database is OK.
Saving current databases with '-BKUP-2023-02-25_16.15.11'
Making imported databases active
Import complete. Please check your library settings and contents for completeness.
Recommend:  Scan Files and Refresh all metadata for each library section.

Backing up of databases
Backup current databases with '-BKUP-2023-02-25_16.20.41' timestamp.
Reindexing main database
Reindexing main database successful.
Reindexing blobs database
Reindexing blobs database successful.
Reindex complete.
Automatic Check,Repair/optimize,Index successful.

Select

  1 - 'stop'      - Stop PMS.
  2 - 'automatic' - Check, Repair/Optimize, and Reindex Database in one step.
  3 - 'check'     - Perform integrity check of database.
  4 - 'vacuum'    - Remove empty space from database without optimizing.
  5 - 'repair'    - Repair/Optimize databases.
  6 - 'reindex'   - Rebuild database database indexes.
  7 - 'start'     - Start PMS

  8 - 'import'    - Import watch history from another database independent of Plex. (risky).
  9 - 'replace'   - Replace current databases with newest usable backup copy (interactive).
 10 - 'show'      - Show logfile.
 11 - 'status'    - Report status of PMS (run-state and databases).
 12 - 'undo'      - Undo last successful command.

 21 - 'prune'     - Prune (remove) old image files (jpeg,jpg,png) from PhotoTranscoder cache.
 42 - 'ignore'    - Ignore duplicate/constraint errors.

 88 - 'update'    - Check for updates.
 99 - 'quit'      - Quit immediately.  Keep all temporary files.
      'exit'      - Exit with cleanup options.

Enter command # -or- command name (4 char min) : start

Starting PMS.
Started PMS

Select

  1 - 'stop'      - Stop PMS.
  2 - 'automatic' - Check, Repair/Optimize, and Reindex Database in one step.
  3 - 'check'     - Perform integrity check of database.
  4 - 'vacuum'    - Remove empty space from database without optimizing.
  5 - 'repair'    - Repair/Optimize databases.
  6 - 'reindex'   - Rebuild database database indexes.
  7 - 'start'     - Start PMS

  8 - 'import'    - Import watch history from another database independent of Plex. (risky).
  9 - 'replace'   - Replace current databases with newest usable backup copy (interactive).
 10 - 'show'      - Show logfile.
 11 - 'status'    - Report status of PMS (run-state and databases).
 12 - 'undo'      - Undo last successful command.

 21 - 'prune'     - Prune (remove) old image files (jpeg,jpg,png) from PhotoTranscoder cache.
 42 - 'ignore'    - Ignore duplicate/constraint errors.

 88 - 'update'    - Check for updates.
 99 - 'quit'      - Quit immediately.  Keep all temporary files.
      'exit'      - Exit with cleanup options.

Enter command # -or- command name (4 char min) : stat


Status report: Sat Feb 25 04:38:50 PM EST 2023
  PMS is running.
  Databases are OK.


Select

  1 - 'stop'      - Stop PMS.
  2 - 'automatic' - Check, Repair/Optimize, and Reindex Database in one step.
  3 - 'check'     - Perform integrity check of database.
  4 - 'vacuum'    - Remove empty space from database without optimizing.
  5 - 'repair'    - Repair/Optimize databases.
  6 - 'reindex'   - Rebuild database database indexes.
  7 - 'start'     - Start PMS

  8 - 'import'    - Import watch history from another database independent of Plex. (risky).
  9 - 'replace'   - Replace current databases with newest usable backup copy (interactive).
 10 - 'show'      - Show logfile.
 11 - 'status'    - Report status of PMS (run-state and databases).
 12 - 'undo'      - Undo last successful command.

 21 - 'prune'     - Prune (remove) old image files (jpeg,jpg,png) from PhotoTranscoder cache.
 42 - 'ignore'    - Ignore duplicate/constraint errors.

 88 - 'update'    - Check for updates.
 99 - 'quit'      - Quit immediately.  Keep all temporary files.
      'exit'      - Exit with cleanup options.

Enter command # -or- command name (4 char min) : exit

Ok to remove temporary databases/workfiles for this session? (Y/N) ? y
Are you sure (Y/N) ? y
Deleting all temporary work files.
bash-4.4#

Sample (typical) scripted session (e.g. via 'cron')

root@lizum:/sata/plex/Plex Media Server/Plug-in Support/Databases# ./DBRepair.sh stop check auto start exit



      Plex Media Server Database Repair Utility (Ubuntu 20.04.5 LTS)
                       Version v1.0.0


[2023-03-05 18.53.49] Stopping PMS.
[2023-03-05 18.53.49] Stopped PMS.

[2023-03-05 18.53.49] Checking the PMS databases
[2023-03-05 18.54.22] Check complete.  PMS main database is OK.
[2023-03-05 18.54.22] Check complete.  PMS blobs database is OK.

[2023-03-05 18.54.22] Automatic Check,Repair,Index started.
[2023-03-05 18.54.22]
[2023-03-05 18.54.22] Checking the PMS databases
[2023-03-05 18.54.56] Check complete.  PMS main database is OK.
[2023-03-05 18.54.56] Check complete.  PMS blobs database is OK.
[2023-03-05 18.54.56]
[2023-03-05 18.54.56] Exporting current databases using timestamp: 2023-03-05_18.54.22
[2023-03-05 18.54.56] Exporting Main DB
[2023-03-05 18.55.30] Exporting Blobs DB
[2023-03-05 18.55.33] Successfully exported the main and blobs databases.  Proceeding to import into new databases.
[2023-03-05 18.55.33] Importing Main DB.
[2023-03-05 18.57.04] Importing Blobs DB.
[2023-03-05 18.57.05] Successfully imported SQL data.
[2023-03-05 18.57.05] Verifying databases integrity after importing.
[2023-03-05 18.57.40] Verification complete.  PMS main database is OK.
[2023-03-05 18.57.40] Verification complete.  PMS blobs database is OK.
[2023-03-05 18.57.40] Saving current databases with '-BACKUP-2023-03-05_18.54.22'
[2023-03-05 18.57.40] Making repaired databases active
[2023-03-05 18.57.40] Repair complete. Please check your library settings and contents for completeness.
[2023-03-05 18.57.40] Recommend:  Scan Files and Refresh all metadata for each library section.
[2023-03-05 18.57.40]
[2023-03-05 18.57.40] Backing up of databases
[2023-03-05 18.57.40] Backup current databases with '-BACKUP-2023-03-05_18.57.40' timestamp.
[2023-03-05 18.57.41] Reindexing main database
[2023-03-05 18.58.17] Reindexing main database successful.
[2023-03-05 18.58.17] Reindexing blobs database
[2023-03-05 18.58.17] Reindexing blobs database successful.
[2023-03-05 18.58.17] Reindex complete.
[2023-03-05 18.58.17] Automatic Check, Repair/optimize, & Index successful.

[2023-03-05 18.58.17] Starting PMS.
[2023-03-05 18.58.17] Started PMS

root@lizum:/sata/plex/Plex Media Server/Plug-in Support/Databases#

======================


Logfile

The logfile (DBRepair.log) keeps track of all commands issues and their status (PASS/FAIL) with timestamp. This can be useful when recovering from an interrupted session because temporary files are timestamped.

2023-02-25 16.14.39 - ============================================================
2023-02-25 16.14.39 - Session start: Host is Synology (DSM 7)
2023-02-25 16.14.56 - StopPMS  - PASS
2023-02-25 16.16.06 - Check   - Check com.plexapp.plugins.library.db - PASS
2023-02-25 16.16.06 - Check   - Check com.plexapp.plugins.library.blobs.db - PASS
2023-02-25 16.16.06 - Check   - PASS
2023-02-25 16.17.20 - Repair  - Export databases - PASS
2023-02-25 16.19.52 - Repair  - Import - PASS
2023-02-25 16.20.41 - Repair  - Verify main database - PASS (Size: 399MB/399MB).
2023-02-25 16.20.41 - Repair  - Verify blobs database - PASS (Size: 1MB/1MB).
2023-02-25 16.20.41 - Repair  - Move files - PASS
2023-02-25 16.20.41 - Repair  - PASS
2023-02-25 16.20.41 - Repair  - PASS
2023-02-25 16.20.46 - Reindex - MakeBackup com.plexapp.plugins.library.db - PASS
2023-02-25 16.20.46 - Reindex - MakeBackup com.plexapp.plugins.library.blobs.db - PASS
2023-02-25 16.20.46 - Reindex - MakeBackup - PASS
2023-02-25 16.21.34 - Reindex - Reindex: com.plexapp.plugins.library.db - PASS
2023-02-25 16.21.35 - Reindex - Reindex: com.plexapp.plugins.library.blobs.db - PASS
2023-02-25 16.21.35 - Reindex - PASS
2023-02-25 16.21.35 - Reindex - PASS
2023-02-25 16.21.35 - Auto    - PASS
2023-02-25 16.38.35 - StartPMS  - PASS
2023-02-25 16.38.57 - Exit    - Delete temp files.
2023-02-25 16.38.58 - Session end.
2023-02-25 16.38.58 - ============================================================
2023-02-25 16.40.10 - ============================================================
2023-02-25 16.40.10 - Session start: Host is Synology (DSM 7)
2023-02-25 16.40.27 - StopPMS  - PASS
2023-02-25 16.42.23 - Check   - Check com.plexapp.plugins.library.db - PASS
2023-02-25 16.42.24 - Check   - Check com.plexapp.plugins.library.blobs.db - PASS
2023-02-25 16.42.24 - Check   - PASS
2023-02-25 16.43.39 - Repair  - Export databases - PASS
2023-02-25 16.46.10 - Repair  - Import - PASS
2023-02-25 16.46.58 - Repair  - Verify main database - PASS (Size: 399MB/399MB).
2023-02-25 16.46.58 - Repair  - Verify blobs database - PASS (Size: 1MB/1MB).
2023-02-25 16.46.59 - Repair  - Move files - PASS
2023-02-25 16.46.59 - Repair  - PASS
2023-02-25 16.46.59 - Repair  - PASS
2023-02-25 16.47.03 - Reindex - MakeBackup com.plexapp.plugins.library.db - PASS
2023-02-25 16.47.03 - Reindex - MakeBackup com.plexapp.plugins.library.blobs.db - PASS
2023-02-25 16.47.03 - Reindex - MakeBackup - PASS
2023-02-25 16.47.52 - Reindex - Reindex: com.plexapp.plugins.library.db - PASS
2023-02-25 16.47.52 - Reindex - Reindex: com.plexapp.plugins.library.blobs.db - PASS
2023-02-25 16.47.52 - Reindex - PASS
2023-02-25 16.47.52 - Reindex - PASS
2023-02-25 16.47.52 - Auto    - PASS
2023-02-25 16.48.04 - StartPMS  - PASS
2023-02-25 16.48.05 - Exit    - Delete temp files.
2023-02-25 16.48.05 - Session end.

Command Reference:

Automatic

Automatic provides automated processing of most checks and repairs. (Check, Repair/Resequence, Index) In its current state, it will not automatically replace a damaged database from a backup (future)

It will not stop PMS as not all systems support stopping PMS from within this tool.

Check

Checks the integrity of the Plex main and blobs databases.

Exit

Exits the utility and removes all temporary database files created during processing. To save all intermediate databases, use the 'Quit' command.

Ignore / Honor

Toggle the state (ON/OFF) of the IGNORE flag. When ON, Duplicates and UNIQUE constraint errors will be ignored. Caution is advised as other errors will be ignored during initial processing.

In ALL cases, DBRepair will never allow a bad database to be created.

Import

Imports (raw) watch history from another PMS database without ability to check validity ( This can have side effects of "negative watch count" being displayed. Caution is advised. )

Prune (Remove)

Checks the PhotoTransoder cache directory for JPG, JPEG, and PNG files older than 30 days and removes them. Under normal operation, PMS manages this automatically. Under certain conditions, PMS will fail to prune them (which is run during Scheduled Maintenance) This command allows you to manually remove what PMS would do normally during that Scheduled Maintenance.

Warning: Initial pruning might take longer than expected.

Execution time, using a Synology DS418 as benchmark, is approximately 100,000 image files per 2 minutes.

For now, Both forms "Prune" and "Remove" are accepted.

Reindex

Rebuilds the database indexes after an import, repair, or replace operation. These indexes are used by PMS for searching (both internally and your typed searches)

Repair

Extracts/recovers all the usable data from the existing databases into text (SQL ascii) form. Repair then creates new SQLite-valid databases from the extracted/recovered data.

The side effect of this process is a fully defragmented database (optimal for Plex use).

100% validity/usability by Plex is not guaranteed as the tool cannot validate each individual record contained in the database. It can only validate at the SQLite level.

In most cases, Repair is the preferred option as the records extracted are only those SQLite deemed valid.

Replace

Looks through the list of available PMS backups.

Starting with the most recent PMS backup, 1. Check the both db files 2. If valid, offer as a replacement choice 3. If accepted (Y/N question) then use as the replacement else advance to the next available backup 4. Upon completion, validate one final time.

Quit

Exits the utility but leaves the temporary databases intact (useful for making exhaustive backups)

Show

Shows the activity log. The activity log is date/time stamped of all activity.

Start

On platform environments which support it, and when invoked by the 'root' user, the tool can start PMS. If not the 'root' user or on a platform which doesn't support it, "Not available" will be indicated.

Stop

On platform environments which support it, and when invoked by the 'root' user, the tool can stop PMS. If not the 'root' user or on a platform which doesn't support it, "Not available" will be indicated.

PMS must be in the stopped state in order to operate on the database files.

Stopping / Starting in Containers (Special Considerations)

Stopping/starting PMS in containers depends on the container execution control mechanism

Some images are designed with an "Always Running" philosophy and do not allow the tool to stop/ start PMS while under program control.

In these image types, the only mechanism, subject to time constraints of any health check, is to type: kill -15 $(pidof 'Plex Media Server') at the container command line prior to invoking DBRepair.sh and waiting for PMS to shutdown.

After DB tasks are completed, and you've exited the container, restart it normally through your normal 'docker start' mechanism.

If your container (Image) supports start/stop , it will be shown in the menu for you to use. If not, you'll need to disable health checks before safely running this tool.

Undo

Undo allows you to "Undo" the last Import, Repair, Replace, or Vacuum command. At present, it only allows the ONE most recent operation. (Future will support undoing more actions)

Vacuum

Instructs SQLite to remove the empty/deleted records and gaps from the databases. This is most beneficial after deleting whole library sections.

For most users, the "automatic" command is the best method. It will regenerate the SQLite indexes as part of the process.

Environment Variables

DBRepair now supports the use of environment variables to allow customization of some operations.

WARNING: Use of these variables may adverse impact PMS operation or performance. USE WITH CAUTION.

DBREPAIR_CACHEAGE - Specify the maximum age for PhotoTrancoder Cache images to be retained

Default DBREPAIR_CACHEAGE is set at 30 days.

You may override this by setting DBREPAIR_CACHEAGE=N, where N is the number of days worth of cache image you wish to retain.

When using interactively, DBRepair will prompt you to confirm OK to remove and show you the cache age

Example: export DBREPAIR_CACHEAGE=20

Enter command # -or- command name (4 char min) : remove

Counting how many files are more than 20 days old.
OK to prune 4497 files?  (Y/N) ?

DBREPAIR_PAGESIZE - Allows setting the Plex SQLite 'page_size'.

Normal Linux (ext4, xfs) filesystems do not need this customization because the filesystem block size = 4096. ZFS users sometimes need to customize their datasets to improve I/O performance (HDDs vs SSDs). This capability allows them to compensate for some of those losses.

If present, sets the Plex SQLite 'page_size' for both Main and Blobs databases. If not present, the default page size for the host OS is used (typically 4096 to match the OS page size).

When in use, you will see the message: "Setting Plex SQLite page size ($DbPageSize)" This will be shown on the console output and reported in the logfile.

Constraints:

1.  Must be a power of 2, with 1024 (2^10) as the Plex default.  (per SQLite 3.12.0 documentation).
    Any invalid value provided will be rounded up to the next integral value (1024, 2048, 4096 ... 65536)
    This may or may not be the value you intended.  Caution is advised.

2.  May not exceed 65536  (per SQLite 3.12.0 documentation).
    Any value exceeding 65536 will be truncated to 65536.

Validation

If the value of DBREPAIR_PAGESIZE is not compliant with requirements, a new value will be selected.

Typing errors will be rounded up (e.g 65535 vs 65536) to the next multiple of 1024 before validation is performed.

If the value is invalid, an error will be printed and recorded in the logfile. The next higher power of two will be selected.

If the value is too large, it will be reduced to the SQLite maximum of 65536.

Management

If you attempt to optimize your database but find the resultant performance is not to your liking, you may try another value and run "automatic" again.

If you ultimately decide to run with the default values (4096),

  1. Remove the environment variable.
  2. Run DBRepair again using "automatic". Your databases will revert to the host OS's default.

Usage: (Linux example shown)

# export DBREPAIR_PAGESIZE=65534
# ./DBRepair.sh stop auto start exit



      Plex Media Server Database Repair Utility (Ubuntu 22.04.3 LTS)
                       Version v1.03.01


[2024-01-14 17.25.35] Stopping PMS.
[2024-01-14 17.25.35] Stopped PMS.

[2024-01-14 17.25.35] Automatic Check,Repair,Index started.
[2024-01-14 17.25.35]
[2024-01-14 17.25.35] Checking the PMS databases
[2024-01-14 17.25.48] Check complete.  PMS main database is OK.
[2024-01-14 17.25.48] Check complete.  PMS blobs database is OK.
[2024-01-14 17.25.48]
[2024-01-14 17.25.48] Exporting current databases using timestamp: 2024-01-14_17.25.35
[2024-01-14 17.25.48] Exporting Main DB
[2024-01-14 17.25.59] Exporting Blobs DB
[2024-01-14 17.26.00] Successfully exported the main and blobs databases.  Proceeding to import into new databases.
[2024-01-14 17.26.00] Importing Main DB.
[2024-01-14 17.26.00] Setting Plex SQLite page size (65536)
[2024-01-14 17.26.29] Importing Blobs DB.
[2024-01-14 17.26.29] Setting Plex SQLite page size (65536)
[2024-01-14 17.26.30] Successfully imported databases.
[2024-01-14 17.26.30] Verifying databases integrity after importing.
[2024-01-14 17.27.43] Verification complete.  PMS main database is OK.
[2024-01-14 17.27.43] Verification complete.  PMS blobs database is OK.
[2024-01-14 17.27.43] Saving current databases with '-BACKUP-2024-01-14_17.25.35'
[2024-01-14 17.27.43] Making repaired databases active
[2024-01-14 17.27.43] Repair complete. Please check your library settings and contents for completeness.
[2024-01-14 17.27.43] Recommend:  Scan Files and Refresh all metadata for each library section.
[2024-01-14 17.27.43]
[2024-01-14 17.27.43] Backing up of databases
[2024-01-14 17.27.43] Backup current databases with '-BACKUP-2024-01-14_17.27.43' timestamp.
[2024-01-14 17.27.44] Reindexing main database
[2024-01-14 17.28.08] Reindexing main database successful.
[2024-01-14 17.28.08] Reindexing blobs database
[2024-01-14 17.28.08] Reindexing blobs database successful.
[2024-01-14 17.28.08] Reindex complete.
[2024-01-14 17.28.08] Automatic Check, Repair/optimize, & Index successful.

[2024-01-14 17.28.08] Starting PMS.
[2024-01-14 17.28.08] Started PMS

#

Special considerations - Synology DSM 7

Using DBRepair on Synology DSM 7 systems with Task Scheduler requires special handling. DSM 7 has additional security (app-armor). Care must be taken to not violate this.

One exception must be implemented. Care must be used to implement.

DSM 7 - Step 1 - Designate a DSM username which will run DBRepair

- Creating a to-task username with a complex password is best practice
- Create a Scheduled task, user-script:
  -  Runs as root
  -  Emails you the result
  -  Is not scheduled
  -  Is disabled in the Task Scheduler task list

Contents of the user-script are:
#!/bin/bash
#
# This script grants the given syno username (your username)
# the ability to elevate to 'root' privilege for use with DBRepair.sh
#
# Set your desired Syno username here (no spaces)
MyUsername=chuck

# Confirm username exists
if [ "$(id $MyUsername)" = "" ]; then
  echo ERROR:  No such user \'$MyUsername\'
  exit 1
fi

# Remove old record
sed -i s/^${MyUsername}.\*$// /etc/sudoers

# Add myself to sudoers
echo "$MyUsername" 'ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers

DSM 7 - Step 2 - Run DBRepair as the designated username

With the security now set, DBRepair can be invoked from Task Scheduler.

Download and place DBRepair.sh in the desired location (PlexMediaServer shared folder ok) Make certain it's executable.

Create Scheduled Task - User-Script to run DBRepair

  • Runs as the selected username
  • Emails you the result
  • Runs on the schedule you desire (Weekly after PMS scheduled tasks completed is optimal)
#!/bin/bash

# Go to the PlexMediaServer shared folder
cd /var/packages/PlexMediaServer/shares/PlexMediaServer

# Run classic  Stop PMS, Automatic optimization/repair, Start PMS, and exit  sequence
sudo ./DBRepair.sh stop auto start exit

plexdbrepair's People

Contributors

007revad avatar 2bits avatar brettpetch avatar causefx avatar chuckpa avatar clhatch avatar dracon09 avatar psitem avatar ukdtom avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

plexdbrepair's Issues

Typing `exit` allows temp files deletion, `99` does not.

Just as the subject says, when typing commands at the main menu, it states that you can use the number or the word. But 99 - exit produces different results when each is used. 99 automatically retains the temp files, while typing exit gives you the option to remove them. This is on version 1.0.4.

Test Watchlist import function in preparation for releasing.

Purpose here is to verify import from one DB to another works as described and handles errors encountered in the process.

Initial testing looks good.
Would now like second set of eyes; both testing and general comments before merging into master.

Kubernetes (Microk8s) Support

Not so much of a bug, but awareness for a possible enhancement. At the very least, for others who encounter the same issue. While attempting to run the script on a Plex Inc. container within a Microk8s environment (instead of Docker) was encountering the following error:

Error: Unknown host. Current supported hosts are: QNAP, Syno, Netgear, Mac, ASUSTOR, WD (OS5), Linux wkstn/svr
Current supported container images: Plexinc, LinuxServer, HotIO, & BINHEX

The following code is checking specifically for Docker:
elif [ "$(grep docker /proc/1/cgroup | wc -l)" -gt 0 ] || [ "$(grep 0::/ /proc/1/cgroup)" = "0::/" ] ||

By simply modifying that line to grep for microk8s I was able to get this to run normally:
elif [ "$(grep microk8s /proc/1/cgroup | wc -l)" -gt 0 ] || [ "$(grep 0::/ /proc/1/cgroup)" = "0::/" ] ||

Identified the need to grep for microk8s by running this command noted in other posted other issues:
root@plex-b56f7f9b-jwzf6:/PlexDBRepair-1.0.9# cat /proc/1/cgroup 12:hugetlb:/kubepods/besteffort/podbd1d424b-7a5a-4c4d-be2d-e04edbc7f2f3/b4d34630860fd6cc820bef7481828c86e44e44dfcc9d85dfd056baf52e831d41 11:rdma:/ 10:devices:/kubepods/besteffort/podbd1d424b-7a5a-4c4d-be2d-e04edbc7f2f3/b4d34630860fd6cc820bef7481828c86e44e44dfcc9d85dfd056baf52e831d41 9:perf_event:/kubepods/besteffort/podbd1d424b-7a5a-4c4d-be2d-e04edbc7f2f3/b4d34630860fd6cc820bef7481828c86e44e44dfcc9d85dfd056baf52e831d41 8:pids:/kubepods/besteffort/podbd1d424b-7a5a-4c4d-be2d-e04edbc7f2f3/b4d34630860fd6cc820bef7481828c86e44e44dfcc9d85dfd056baf52e831d41 7:freezer:/kubepods/besteffort/podbd1d424b-7a5a-4c4d-be2d-e04edbc7f2f3/b4d34630860fd6cc820bef7481828c86e44e44dfcc9d85dfd056baf52e831d41 6:blkio:/kubepods/besteffort/podbd1d424b-7a5a-4c4d-be2d-e04edbc7f2f3/b4d34630860fd6cc820bef7481828c86e44e44dfcc9d85dfd056baf52e831d41 5:cpu,cpuacct:/kubepods/besteffort/podbd1d424b-7a5a-4c4d-be2d-e04edbc7f2f3/b4d34630860fd6cc820bef7481828c86e44e44dfcc9d85dfd056baf52e831d41 4:memory:/kubepods/besteffort/podbd1d424b-7a5a-4c4d-be2d-e04edbc7f2f3/b4d34630860fd6cc820bef7481828c86e44e44dfcc9d85dfd056baf52e831d41 3:net_cls,net_prio:/kubepods/besteffort/podbd1d424b-7a5a-4c4d-be2d-e04edbc7f2f3/b4d34630860fd6cc820bef7481828c86e44e44dfcc9d85dfd056baf52e831d41 2:cpuset:/kubepods/besteffort/podbd1d424b-7a5a-4c4d-be2d-e04edbc7f2f3/b4d34630860fd6cc820bef7481828c86e44e44dfcc9d85dfd056baf52e831d41 1:name=systemd:/kubepods/besteffort/podbd1d424b-7a5a-4c4d-be2d-e04edbc7f2f3/b4d34630860fd6cc820bef7481828c86e44e44dfcc9d85dfd056baf52e831d41 0::/system.slice/snap.microk8s.daemon-containerd.service

[Feature Requests] Timestamps

Very simple request, add in some timestamps for task start/end and calculate the total runtime of the task and output at the end.

CLI?

Is there a way to just run this as a CLI on docker startup?

Per your post on plex forums

(With PMS stopped, running as root)

Run options: 1 - 4 - 3

Give this a shot.

The best way to test the impact is has is to run a search after completing this optimization sequence.

PMS gets slow because of the internal links / chains SQLite creates.
It takes time to walk it. When you compact the DB (option 4 above) and place the records adjacent each other, it gets profoundly quicker

Change directory for Backup and Maintenance

Gooodday Chuck,

First of all, awesome tool you made there. It made everything alot easier for alot of people.
and im very gratefull for it thanks :).

I would very much like it if i could change the directory that all operations happen in.
My Plex has its Databases directory on a ramdisk, and i dont want it to have to be as large as it is now.
My sqlite files are about 3.9gb together, and with all the operations the ramdisk needs to be over 10gb.

Also, if there could be a way to have it create backups and have us choose where that would be great.
Im using this in a Hotio container.

DBRepair sometimes reports DB corruption when there is none.

Reported by a user:

  • Issuing docker exec plex_container DBRepair.sh stop auto start exec
    results in "DB Corrupt" error during initial check only".

Investigation:

  • Stop/Start logic incorrectly handles the condition where it takes more than 10 seconds for PMS to write out all pending DB changes and exit.

Action:

  • This GHI fixes this behavior by replacing existing code with better code

Support Mac

The DBRepair script can support Apple Mac.

Closes: #9

With a couple of allowances for pidof and stat I have a working fork.
Testing was done on a Mac Studio and a QNAP, the first few options so far.
Okay, thanks for making it ๐Ÿบ

The path to "Plex SQLLite" is missing the extension

set "PlexSQL=%PROGRAMFILES%\Plex\Plex Media Server\Plex SQLite"

Had an issue where it said the file could not be found. I noticed the extension was missing from the path. However, my actual issue was that the plex application data is in C:\Program Files (x86), so I switched to %PROGRAMFILES% to %PROGRAMFILES(x86)% as well.

Checking both paths might be helpful.

Unknown Host with current lscr.io/linuxserver/plex:latest and current v0.6.0 PlexDBRepair

Last release of the script worked without a hitch. On the newest version:

$ docker exec -it plex /bin/bash
root@Server-Zero:/# cd /var/run/service
root@Server-Zero:/var/run/service# s6-svc -d svc-plex
root@Server-Zero:/var/run/service# cd "/config/Library"
root@Server-Zero:/config/Library# ./DBRepair.sh
Error: Unknown host. Currently supported hosts are: QNAP, Synology, Netgear, Mac, ASUSTOR, WD (OS5) and Linux Workstation/Server
Error: Unknown host. Currently supported hosts are: QNAP, Synology, Netgear, Mac, ASUSTOR, WD (OS5) and Linux Workstation/Server

Thank you for the script by the way!

Run @Synology DSM 7 Docker "lscr.io/linuxserver/plex"

"Error: Unknown host. Currently supported hosts are: QNAP, Synology, Netgear, ASUSTOR, and Linux Workstation/Server"

SSH via Windows 10 Pro x64 Desktop with Command Prompt
I am the owner and sole user of the Synology NAS > I have full Admin / Sudo rights and permissions

I have:

  1. Downloaded and extracted to tool as instructed.
  2. CHMOD the file.
  3. Placed under multiple directories under the home folder location for the plex container:
  • /volume1/docker/plex/
  • /volume1/docker/plex/Library
  • /volume1/docker/plex/Library/Application Support/
  • /volume1/docker/plex/Library/Application Support/Plex Media Server
  • Docker Config Home Directory for "plex" = /volume1/docker/plex

Now looking into the script (without editing because I have not obtained your permssion to modify the "Host Config" section to reflect my setup.

  • My config / dir locations are different; I believe in the script I need to adjust the directory locations to match my setup?

@ChuckPa I appreciate all your time, hard work you have done for the community. Please review this when you can.

Synology DS1019+
DSM 7
16GB RAM
5x 14TB 7200 RPM NAS Drives in Raid 5 = ~50TB

Importing an old view history (option 8) results in issues

Hello and thank you very much for providing this tool. It's god damn useful!

I only now found this useful tool and tried to use it to recover old view historys of plex into my new setup (server broke down etc.).

Now there's a good thing: the merge works, all the old view history appears BUT it looks like we have double entries in the db. For example, I watched "The Americans" on my old server. On the new server I did of course mark them as "played" so I can start watching the series at the same episode.

This results in the following:

grafik

It looks like it counts all episodes twice. The first time I actually watched it (on the old server) and the second time I marked it as watched on the new server. This results in minus 13 :D

Now this is kind of a problem... it would be cool to remove these double entries and only use the most recent ones. I tried to repair db, also clean up in plex itself, redownload metadata/rescan etc. Didn't help

I can provide you with my .db file if you wish. But only in private of course

Thanks again and wish you a nice day!

Database is OK and auto terminates?

Hey guys,

I have Plex running in Docker on my Ubuntu system. Copied the .sh file to the bind mount in the /config directory as per the instructions and docker exec'ed into the container as per the instructions on this page and just thought it was either strange that it found zero issues with my DB or that it auto terminated the script after running the Automatic option.

Just wanting to make sure all is OK or if anything else needs to be checked?

shannon@Krypton:~/docker$ sudo docker exec -it plex /bin/bash
[sudo] password for shannon:
root@Krypton:/# dir
4kmovies bin cams concerts defaults docker-mods home kidsmovies lib lib64 media movies1 music package proc run srv tmp tv1 tv3 var
app boot command config dev etc init kidstv lib32 libx32 mnt movies2 opt plextranscode root sbin sys transcode tv2 usr
root@Krypton:/# cd config
root@Krypton:/config# ./DBRepair.sh

  Plex Media Server Database Repair Utility (Docker)
                   Version v1.0.5

Select

1 - 'stop' - Stop PMS
2 - 'automatic' - database check, repair/optimize, and reindex in one step.
3 - 'check' - Perform integrity check of database
4 - 'vacuum' - Remove empty space from database
5 - 'repair' - Repair/Optimize databases
6 - 'reindex' - Rebuild database database indexes
7 - 'start' - Start PMS

8 - 'import' - Import watch history from another database independent of Plex. (risky)
9 - 'replace' - Replace current databases with newest usable backup copy (interactive)
10 - 'show' - Show logfile
11 - 'status' - Report status of PMS (run-state and databases)
12 - 'undo' - Undo last successful command

99 - 'quit' - Quit immediately. Keep all temporary files.
'exit' - Exit with cleanup options.

Enter command # -or- command name (4 char min) : 1

Stopping PMS.
Stopped PMS.

Select

1 - 'stop' - Stop PMS
2 - 'automatic' - database check, repair/optimize, and reindex in one step.
3 - 'check' - Perform integrity check of database
4 - 'vacuum' - Remove empty space from database
5 - 'repair' - Repair/Optimize databases
6 - 'reindex' - Rebuild database database indexes
7 - 'start' - Start PMS

8 - 'import' - Import watch history from another database independent of Plex. (risky)
9 - 'replace' - Replace current databases with newest usable backup copy (interactive)
10 - 'show' - Show logfile
11 - 'status' - Report status of PMS (run-state and databases)
12 - 'undo' - Undo last successful command

99 - 'quit' - Quit immediately. Keep all temporary files.
'exit' - Exit with cleanup options.

Enter command # -or- command name (4 char min) : 2

Automatic Check,Repair,Index started.

Checking the PMS databases
Check complete. PMS main database is OK.
Check complete. PMS blobs database is OK.

Exporting current databases using timestamp: 2023-05-20_18.22.25
Exporting Main DB
Terminated
root@Krypton:/config# shannon@Krypton:~/docker$

[Feature Request] Database Information Output option

It might be useful to add an additional option

0) Database Information Output

This option would then output quantities of database entries, database size, and a suggested Plex cache size. There could be other potential information here.

This idea stems from the debug option that Pi-hole has, which outputs some very useful information for troubleshooting.

Truenas/Freebsd

Is there anyway to get this to work on Truenas/Freebsd?
Currently running Version:
TrueNAS-13.0-U4
It runs but gives me Error: Unknown host. Current supported hosts are: QNAP, Syno, Netgear, Mac, ASUSTOR, WD (OS5), Linux wkstn/svr
Current supported container images: Plexinc, LinuxServer, HotIO, & BINHEX
Sorry if this is in the wrong place.

Query: Powershell?

Rather than batch file, would it make sense to switch across to PS for the windows version? Or possibly python, since python and sqlite play nicely together.

A lot of the PS commands have aliases assigned for the common 'nix ones (like wget and Invoke-WebRequest).
Just thinking it could help with multi-arch support.

Update DBRepair to include new feature

DBRepair.sh -

v 1.01.00

  1. Change version numbering for auto update mechanism
  2. Now support update-in-place menu option , keeping current version as .bak

With thanks to @causefx for his major contributions to this release.

Cannot override script error handling when Databases badly damaged

Cases exist where a database's indexes are damaged to the point the tool does not allow successful recovery without PMS assistance.

In these cases, Normal flow is:

  1. Recover as much of the databases as possible and create a valid SQLite3 DB structure
  2. Start PMS
  3. Allow PMS to rebuild data content in those damaged records
  4. Complete repair / optimization after all PMS internal data repairs are complete.

This tool needs the ability to IGNORE selected errors, knowing the resultant DB will be acceptable to PMS.

latest version of tool Version="v1.0.13" not working with hotio plex image

currently running hotio image: cr.hotio.dev/hotio/plex:release-1.31.2.6810-a607d384f

when I run the latest version Version="v1.0.13", I get the message:
`Error: Unknown host. Current supported hosts are: QNAP, Syno, Netgear, Mac, ASUSTOR, WD (OS5), Linux wkstn/svr
Current supported container images: Plexinc, LinuxServer, HotIO, & BINHEX

Are you trying to run the tool from outside the container environment ?`

Running Version="v1.0.10" is fine.
image

Options to stop and start Plex?

It might be an idea to have options to stop and start Plex from the menu/command-line. I see in the script that it already has to determine how Plex is installed (LSIO, HOTIO, etc), so could use that information to use the correct stop/start commands.

Error repairing database

Seems to be the same issue as #48, but apparently I have no valid backups ๐Ÿ˜ฌ

Exporting current databases using timestamp: 2023-10-08_10.56.59
Exporting Main DB
Exporting Blobs DB
Successfully exported the main and blobs databases. Proceeding to import into new databases.
Importing Main DB.
Importing Blobs DB.
Runtime error near line 49883: UNIQUE constraint failed: blobs.linked_type, blobs.linked_id, blobs.blob_type (19)
Error 1 from Plex SQLite while importing from './dbtmp/blobs.plexapp.sql-2023-10-08_10.56.59'
Cannot continue.

Could you add the modification mentioned in #48 to allow handling this situation?

Thanks

Podman support needed

Current support for Docker cgroup v1 & v2 is insufficient.
Also need Podman (libpod) cgroup support.

Binhex - ERROR: Cannot write to Databases directory. Insufficient privilege.

Hello ! First, thanks for your work.

I have some issue to run the DBRepair on my binhex-plexpass container hosted on Unraid.

Run as root inside the container : Error
Run as the nobody user : Error
I even tried to reset the perms file to redo all the permissions on the folder, still an error.

The script is in / alongside to /config folder.
Is there any obvious things i miss maybe ?

Thanks for your time !

Run @Ubuntu Docker

Hello,

having problems to run this script with my Ubuntu based Plex Server. Plex runs by docker

~/.docker-conf/Library/Application Support/Plex Media Server/Plug-in Support/Databases$ ./DBRepair.sh 
Error: Unknown host. Currently supported hosts are: QNAP, Synology, Netgear, ASUSTOR, and Linux Workstation/Server
Error: Unknown host. Currently supported hosts are: QNAP, Synology, Netgear, ASUSTOR, and Linux Workstation/Server

What can i do to runs this script?

Issues running script in docker container

Hello,
This script recently will exit with "Terminated" for me randomly.

I had used the previous version with success on October 13 but now whether using the latest or previous version the script will randomly exit.

if the stop PMS option is taking a while it can terminate, or most of the time during the db checks it will terminate and not pass that section. Here is my log:

==================================================================================
2023-10-13 21.26.49 - ============================================================
2023-10-13 21.26.49 - Session start: Host is Docker
2023-10-13 21.26.57 - Auto - FAIL - PMS runnning
2023-10-13 21.37.30 - Stop - FAIL (Timeout)
2023-10-13 21.38.20 - ============================================================
2023-10-13 21.38.20 - Session start: Host is Docker
2023-10-13 21.38.25 - Stop - PASS
2023-10-13 21.38.30 - Auto - START
2023-10-13 21.38.48 - Check - Check com.plexapp.plugins.library.db - PASS
2023-10-13 21.38.50 - Check - Check com.plexapp.plugins.library.blobs.db - PASS
2023-10-13 21.38.50 - Check - PASS
2023-10-13 21.41.22 - Repair - Export databases - PASS
2023-10-13 21.42.28 - Repair - Import - PASS
2023-10-13 21.42.40 - Repair - Verify main database - PASS (Size: 1782MB/1597MB).
2023-10-13 21.42.40 - Repair - Verify blobs database - PASS (Size: 1067MB/1076MB).
2023-10-13 21.42.40 - Repair - Move files - PASS
2023-10-13 21.42.40 - Repair - PASS
2023-10-13 21.42.40 - Repair - PASS
2023-10-13 21.42.41 - Reindex - MakeBackup com.plexapp.plugins.library.db - PASS
2023-10-13 21.42.41 - Reindex - MakeBackup com.plexapp.plugins.library.blobs.db - PASS
2023-10-13 21.42.41 - Reindex - MakeBackup - PASS
2023-10-13 21.42.58 - Reindex - Reindex: com.plexapp.plugins.library.db - PASS
2023-10-13 21.42.58 - Reindex - Reindex: com.plexapp.plugins.library.blobs.db - PASS
2023-10-13 21.42.58 - Reindex - PASS
2023-10-13 21.42.58 - Reindex - PASS
2023-10-13 21.42.58 - Auto - COMPLETED
2023-10-13 21.43.23 - Start - PASS
2023-10-13 21.52.11 - Exit - Delete temp files.
2023-10-13 21.52.11 - Session end. Fri Oct 13 09:52:11 PM UTC 2023
2023-10-13 21.52.11 - ============================================================
2023-10-21 11.07.14 - ============================================================
2023-10-21 11.07.14 - Session start: Host is Docker
2023-10-21 11.07.24 - Stop - PASS
2023-10-21 11.07.29 - Auto - START
2023-10-21 11.08.47 - ============================================================
2023-10-21 11.08.47 - Session start: Host is Docker
2023-10-21 11.08.51 - Auto - FAIL - PMS runnning
2023-10-21 11.09.07 - Stop - PASS
2023-10-21 11.09.11 - Auto - START
2023-10-21 11.09.47 - ============================================================
2023-10-21 11.09.47 - Session start: Host is Docker
2023-10-21 11.09.52 - Stop - PASS
2023-10-21 11.09.55 - Auto - START
2023-10-21 11.10.18 - ============================================================
2023-10-21 11.10.18 - Session start: Host is Docker
2023-10-21 11.10.21 - Check - FAIL - PMS runnning
2023-10-21 11.10.26 - Stop - PASS
2023-10-21 11.10.57 - ============================================================
2023-10-21 11.10.57 - Session start: Host is Docker
2023-10-21 11.11.39 - Unknown command: 'sudo'
2023-10-21 11.11.44 - Exit - Delete temp files.
2023-10-21 11.11.44 - Session end. Sat Oct 21 11:11:44 AM UTC 2023
2023-10-21 11.11.44 - ============================================================
2023-10-21 11.14.19 - ============================================================
2023-10-21 11.14.19 - Session start: Host is Docker
2023-10-21 11.14.24 - Stop - PASS
2023-10-21 11.14.27 - Auto - START
2023-10-21 11.14.38 - Check - Check com.plexapp.plugins.library.db - PASS
2023-10-21 11.14.59 - ============================================================
2023-10-21 11.14.59 - Session start: Host is Docker
2023-10-21 11.15.55 - Stop - PASS
2023-10-21 11.16.18 - ============================================================
2023-10-21 11.16.18 - Session start: Host is Docker
2023-10-21 11.16.24 - Repair - FAIL - PMS runnning
2023-10-21 11.16.32 - Stop - PASS
2023-10-21 11.17.00 - ============================================================
2023-10-21 11.17.00 - Session start: Host is Docker
2023-10-21 11.17.52 - ============================================================
2023-10-21 11.17.52 - Session start: Host is Docker
2023-10-21 11.17.58 - Stop - PASS
2023-10-21 11.18.00 - Auto - START
2023-10-21 11.18.11 - Check - Check com.plexapp.plugins.library.db - PASS
2023-10-21 11.21.56 - ============================================================
2023-10-21 11.21.56 - Session start: Host is Docker
2023-10-21 11.22.01 - Stop - PASS
2023-10-21 11.22.11 - Auto - START
2023-10-21 11.22.20 - ============================================================
2023-10-21 11.22.20 - Session start: Host is Docker
2023-10-21 11.26.36 - ============================================================
2023-10-21 11.26.36 - Session start: Host is Docker
2023-10-21 11.26.42 - Stop - PASS
2023-10-21 11.26.45 - Auto - START
2023-10-21 11.26.55 - Check - Check com.plexapp.plugins.library.db - PASS
2023-10-21 11.26.57 - Check - Check com.plexapp.plugins.library.blobs.db - PASS
2023-10-21 11.26.57 - Check - PASS
2023-10-21 11.27.10 - ============================================================
2023-10-21 11.27.10 - Session start: Host is Docker
2023-10-21 11.28.09 - Exit - Delete temp files.
2023-10-21 11.28.09 - Session end. Sat Oct 21 11:28:09 AM UTC 2023
2023-10-21 11.28.09 - ============================================================
2023-10-21 11.32.33 - ============================================================
2023-10-21 11.32.33 - Session start: Host is Docker
2023-10-21 11.32.36 - Auto - START
2023-10-21 11.40.59 - ============================================================
2023-10-21 11.40.59 - Session start: Host is Docker
2023-10-21 11.41.26 - Stop - PASS
2023-10-21 11.41.44 - ============================================================
2023-10-21 11.41.44 - Session start: Host is Docker
2023-10-21 11.41.48 - Stop - PASS
2023-10-21 11.41.54 - Auto - START
2023-10-21 11.42.12 - ============================================================

I am using the linuxserver.io container. Any ideas on what the issue is?

Thanks,
Chris

Require root UID on selective platforms

There are instances where users are not following the published instructions which show elevating the command line to 'root' (sudo) privilege level before invoking the script.

On certain hosts, like Synology, this leaves the database owned by a username other than PlexMediaServer and with the wrong permissions.

The result is that PMS cannot start.

This task will enforce root user ID on Synology , QNAP, and in those environments where it is necessary

Can't Stop Plex From Within Container Binhex-Unraid

Running the binhex-plexpass instance of Plex on Unraid and whenever I run PlexDBRepair it won't let me stop Plex. I get the following option for "1".

1 - 'stop' - (Not available. Stop manually)

If I stop the container it also won't work as I get the following question:

Are you trying to run the tool from outside the container environment ?

But it doesn't allow me to go further.

Any help would be greatly appriciated!

Repair failed

hello, getting following log entry:

2023-04-20 12.17.29 - Session start: Host is Docker 2023-04-20 12.17.30 - Stop - PASS 2023-04-20 12.17.30 - Auto - START 2023-04-20 12.17.37 - Check - Check com.plexapp.plugins.library.db - PASS 2023-04-20 12.17.57 - Check - Check com.plexapp.plugins.library.blobs.db - FAIL (DBRepair.log com.plexapp.plugins.library.blobs.db com.plexapp.plugins.library.blobs.db-2022-05-16 com.plexapp.plugins.library.blobs.db-2022-05-19 com.plexapp.plugins.library.blobs.db-2022-05-22 com.plexapp.plugins.library.blobs.db-2022-05-25 com.plexapp.plugins.library.db com.plexapp.plugins.library.db-2022-05-16 com.plexapp.plugins.library.db-2022-05-19 com.plexapp.plugins.library.db-2022-05-22 com.plexapp.plugins.library.db-2022-05-25 dbtmp in database main DBRepair.log com.plexapp.plugins.library.blobs.db com.plexapp.plugins.library.blobs.db-2022-05-16 com.plexapp.plugins.library.blobs.db-2022-05-19 com.plexapp.plugins.library.blobs.db-2022-05-22 com.plexapp.plugins.library.blobs.db-2022-05-25 com.plexapp.plugins.library.db com.plexapp.plugins.library.db-2022-05-16 com.plexapp.plugins.library.db-2022-05-19 com.plexapp.plugins.library.db-2022-05-22 com.plexapp.plugins.library.db-2022-05-25 dbtmp On tree page 1915565 cell 0: overflow list length is 1 but should be 66) 2023-04-20 12.17.57 - Check - FAIL 2023-04-20 12.27.01 - Repair - Export databases - PASS 2023-04-20 12.28.10 - Repair - Cannot import blobs database from './dbtmp/blobs.plexapp.sql-2023-04-20_12.17.30' - FAIL (1) 2023-04-20 12.28.10 - Repair - FAIL 2023-04-20 12.28.10 - Auto - FAIL 2023-04-20 12.28.10 - Start - PASS 2023-04-20 12.28.11 - Exit - Delete temp files. 2023-04-20 12.28.11 - Session end. Thu Apr 20 12:28:11 PM CEST 2023

And this is my .sh Output:
[2023-04-20 12.27.29] Importing Blobs DB. Runtime error near line 105175: UNIQUE constraint failed: blobs.linked_type, blobs.linked_id, blobs.blob_type (19) [2023-04-20 12.28.10] Error 1 from Plex SQLite while importing from './dbtmp/blobs.plexapp.sql-2023-04-20_12.17.30' [2023-04-20 12.28.10] Cannot continue. [2023-04-20 12.28.10] Repair failed. Automatic mode cannot continue. Please repair with individual commands

What can i do about it?

Readme needs update

Darn, I added checks for some GitHub actions, that are not running on this Repo

Need to remove them

Runtime error near line 11251 after importing Blobs DB

Hi, I have a problem with the utilisation of the script, in automatic mode or in repair I have this:
Exporting current databases using timestamp: 2023-03-15_12.26.29
Exporting Main DB
Exporting Blobs DB
Successfully exported the main and blobs databases. Proceeding to import into new databases.
Importing Main DB.
Importing Blobs DB.
Runtime error near line 11251: UNIQUE constraint failed: blobs.linked_type, blobs.linked_id, blobs.blob_type (19)
Error 1 from Plex SQLite while importing from './dbtmp/blobs.plexapp.sql-2023-03-15_12.26.29'
Cannot continue.

How to fix this ?
Thanks

Error: Unable to set up server: sqlite3_statement_backend::loadOne: attempt to write a readonly database (N4soci10soci_errorE)

binhex-plexpass on UnRaid 6.11.5

Ran

  1. Check database
  2. Attempt database repair
  3. Reindex database
  4. Exit

When restarting the docker, the log file was full of this error

"Error: Unable to set up server: sqlite3_statement_backend::loadOne: attempt to write a readonly database (N4soci10soci_errorE)"

Based on google search it lead me to change the permissions on the database using this command.

stop the binhex-plexpass docker conatainer, then from the UnRaid main terminal interface run
"sudo chmod -R 777 /mnt/user/appdata/binhex-plexpass/Plex\ Media\ Server/Plug-in\ Support/"

Once that was done I was able to restart the docker container and access plex as normal.

Looks like when the "DBRepair.sh" script re-builds the database the permissions are changed to "root" and they need to be "nobody"

Support for arch-based distributions

Problem

Currently the bash script is hard coded to find "Arch Linux" in the os-release file

PlexDBRepair/DBRepair.sh

Lines 621 to 624 in 40a8873

# Arch Linux (must check for native Arch after binhex)
elif [ -e /etc/os-release ] && [ "$(grep 'Arch Linux' /etc/os-release)" != "" ] && \
[ -d /usr/lib/plexmediaserver ] && \
[ -d /var/lib/plex ]; then

This creates the problem that only the official Arch Linux distribution is allowed run the script, even though it should also work on arch-based distros.

Proposal

I did some digging to maybe find a better way to identify arch-based distributions and learned about the "ID_LIKE" parameter.

https://www.freedesktop.org/software/systemd/man/os-release.html#ID_LIKE=

ID_LIKE=

A space-separated list of operating system identifiers in the same syntax as the ID= setting. It should list identifiers of operating systems that are closely related to the local operating system in regards to packaging and programming interfaces, for example listing one or more OS identifiers the local OS is a derivative from. An OS should generally only list other OS identifiers it itself is a derivative of, and not any OSes that are derived from it, though symmetric relationships are possible. Build scripts and similar should check this variable if they need to identify the local operating system and the value of ID= is not recognized. Operating systems should be listed in order of how closely the local operating system relates to the listed ones, starting with the closest. This field is optional.

Examples: for an operating system with "ID=centos", an assignment of "ID_LIKE="rhel fedora"" would be appropriate. For an operating system with "ID=ubuntu", an assignment of "ID_LIKE=debian" is appropriate.

Notes

If you are using this file to determine the OS or a specific version of it, use the ID and VERSION_ID fields, possibly with ID_LIKE as fallback for ID.

/etc/os-release files of several arch-based distros

Manjaro - https://gitlab.manjaro.org/packages/core/filesystem/-/blob/master/os-release

NAME="Manjaro Linux"
PRETTY_NAME="Manjaro Linux"
ID=manjaro
ID_LIKE=arch
BUILD_ID=rolling
ANSI_COLOR="32;1;24;144;200"
HOME_URL="https://manjaro.org/"
DOCUMENTATION_URL="https://wiki.manjaro.org/"
SUPPORT_URL="https://forum.manjaro.org/"
BUG_REPORT_URL="https://docs.manjaro.org/reporting-bugs/"
PRIVACY_POLICY_URL="https://manjaro.org/privacy-policy/"
LOGO=manjarolinux

Manjaro ARM - https://gitlab.manjaro.org/manjaro-arm/packages/core/filesystem/-/blob/master/os-release

NAME="Manjaro ARM"
ID="manjaro-arm"
ID_LIKE="manjaro arch"
PRETTY_NAME="Manjaro ARM"
ANSI_COLOR="1;32"
HOME_URL="https://www.manjaro.org/"
SUPPORT_URL="https://forum.manjaro.org/c/arm/"
LOGO=manjarolinux

EndeavourOS

NAME=EndeavourOS
PRETTY_NAME=EndeavourOS
ID=endeavouros
ID_LIKE=arch
BUILD_ID=rolling
ANSI_COLOR="38;2;23;147;209"
HOME_URL='https://endeavouros.com'
DOCUMENTATION_URL='https://discovery.endeavouros.com'
SUPPORT_URL='https://forum.endeavouros.com'
BUG_REPORT_URL='https://forum.endeavouros.com/c/arch-based-related-questions/bug-reports'
PRIVACY_POLICY_URL="https://terms.archlinux.org/docs/privacy-policy/"
LOGO=endeavouros

ArcoLinux - https://github.com/arcolinux/arcolinux-system-config/blob/master/usr/local/share/arcolinux/release/os-release-arco

NAME=ArcoLinux
ID=arcolinux
ID_LIKE=arch
BUILD_ID=rolling
ANSI_COLOR="0;36"
HOME_URL="https://arcolinux.info/"
SUPPORT_URL="https://arcolinuxforum.com/"
BUG_REPORT_URL="https://github.com/arcolinux"
LOGO=arcolinux-hello

Note: ID_LIKE is optional so there will likely be arch-based distros that miss this check but at least the majority will be covered :).

Personal Test

Successfully ran this on my Manjaro machine.

2023-06-08 12.50.32 - ============================================================
2023-06-08 12.50.32 - Session start: Host is Arch Linux
2023-06-08 12.50.58 - Stop    - PASS
2023-06-08 12.51.10 - Vacuum  - Check com.plexapp.plugins.library.db - PASS
2023-06-08 12.51.12 - Vacuum  - Check com.plexapp.plugins.library.blobs.db - PASS
2023-06-08 12.51.12 - Vacuum  - MakeBackup com.plexapp.plugins.library.db - PASS
2023-06-08 12.51.12 - Vacuum  - MakeBackup com.plexapp.plugins.library.blobs.db - PASS
2023-06-08 12.51.12 - Vacuum  - MakeBackups - PASS
2023-06-08 12.51.13 - Vacuum  - Vacuum main database - PASS (Size: 77MB/77MB).
2023-06-08 12.51.16 - Vacuum  - Vacuum blobs database - PASS (Size: 146MB/146MB).
2023-06-08 12.51.16 - Vacuum  - PASS
2023-06-08 12.51.24 - Check   - Check com.plexapp.plugins.library.db - PASS
2023-06-08 12.51.24 - Check   - Check com.plexapp.plugins.library.blobs.db - PASS
2023-06-08 12.51.24 - Check   - PASS
2023-06-08 12.51.38 - Exit    - Retain temp files.

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.