Change DBRepair.sh to support Apple Mac.

This is the primary commit for PlexDBRepair that adds Mac support
to `DBRepair.sh`

Add all the PMS paths unique to Mac in `HostConfig()`.
Include code to create a `plexmediaserver.pid` file.
That concept needs more attention to guard against PMS running.

Deal with MacOS quirks in shell utils and builtins like
- sed -i
- echo -n
- pidof
- awk
- colons & forward slashes in filenames
This commit is contained in:
2bits 2022-11-14 17:50:07 -08:00
parent 4f581d251a
commit fac2aa6db4

View File

@ -246,7 +246,7 @@ RestoreSaved() {
# Get the size of the given DB in MB # Get the size of the given DB in MB
GetSize() { GetSize() {
Size=$(stat -c %s "$1") Size=$(stat $STATFMT $STATBYTES "$1")
Size=$(expr $Size / 1048576) Size=$(expr $Size / 1048576)
[ $Size -eq 0 ] && Size=1 [ $Size -eq 0 ] && Size=1
echo $Size echo $Size
@ -255,6 +255,11 @@ GetSize() {
# Determine which host we are running on and set variables # Determine which host we are running on and set variables
HostConfig() { HostConfig() {
# On all hosts except Mac
PIDOF="pidof"
STATFMT="-c"
STATBYTES="%s"
# Synology (DSM 7) # Synology (DSM 7)
if [ -d /var/packages/PlexMediaServer ] && \ if [ -d /var/packages/PlexMediaServer ] && \
[ -d "/var/packages/PlexMediaServer/shares/PlexMediaServer/AppData/Plex Media Server" ]; then [ -d "/var/packages/PlexMediaServer/shares/PlexMediaServer/AppData/Plex Media Server" ]; then
@ -419,6 +424,31 @@ HostConfig() {
HostType="Western Digital" HostType="Western Digital"
return 0 return 0
# Apple Mac
elif [ -d "/Applications/Plex Media Server.app" ] && \
[ -d "$HOME/Library/Application Support/Plex Media Server" ]; then
# Where is the software
PLEX_SQLITE="/Applications/Plex Media Server.app/Contents/MacOS/Plex SQLite"
AppSuppDir="$HOME/Library/Application Support"
DBDIR="$AppSuppDir/Plex Media Server/Plug-in Support/Databases"
PID_FILE="$AppSuppDir/Plex Media Server/plexmediaserver.pid"
LOGFILE="$DBDIR/DBRepair.log"
LOG_TOOL="logger"
# MacOS uses pgrep and uses different stat options
PIDOF="pgrep"
STATFMT="-f"
STATBYTES="%z"
# On MacOS PMS is not coded to create a plexmediaserver.pid.
# Remove stale, and if PMS is running create a new one for script use.
[ -f "$PID_FILE" ] && rm "$PID_FILE"
PIDVALUE=$($PIDOF "Plex Media Server")
[ $PIDVALUE ] && echo $PIDVALUE > "$PID_FILE"
HostType="Mac"
return 0
fi fi
# Unknown / currently unsupported host # Unknown / currently unsupported host
@ -448,7 +478,7 @@ SetLast "" ""
# Identify this host # Identify this host
HostType="" ; LOG_TOOL="echo" HostType="" ; LOG_TOOL="echo"
if ! HostConfig; then if ! HostConfig; then
Output 'Error: Unknown host. Currently supported hosts are: QNAP, Synology, Netgear, ASUSTOR, WD (OS5) and Linux Workstation/Server' Output 'Error: Unknown host. Currently supported hosts are: QNAP, Synology, Netgear, Mac, ASUSTOR, WD (OS5) and Linux Workstation/Server'
exit 1 exit 1
fi fi
@ -497,7 +527,7 @@ while true
do do
# Is PMS already running? # Is PMS already running?
if [ -f "$PID_FILE" ] && [ "$(pidof 'Plex Media Server')" != "" ] ; then if [ -f "$PID_FILE" ] && [ "$($PIDOF 'Plex Media Server')" != "" ] ; then
Output "Plex Media Server is currently running, cannot continue." Output "Plex Media Server is currently running, cannot continue."
Output "Please stop Plex Media Server and restart this utility." Output "Please stop Plex Media Server and restart this utility."
WriteLog "PMS running. Could not continue." WriteLog "PMS running. Could not continue."
@ -728,7 +758,7 @@ do
fi fi
# Check size # Check size
Size=$(stat -c '%s' $CPPL.db) Size=$(stat $STATFMT $STATBYTES $CPPL.db)
# Exit if not valid # Exit if not valid
if [ $Size -lt 300000 ]; then if [ $Size -lt 300000 ]; then
@ -743,7 +773,7 @@ do
Fail=0 Fail=0
# Get the owning UID/GID before we proceed so we can restore # Get the owning UID/GID before we proceed so we can restore
Owner="$(stat -c '%u:%g' $CPPL.db)" Owner="$(stat $STATFMT '%u:%g' $CPPL.db)"
# Attempt to export main db to SQL file (Step 1) # Attempt to export main db to SQL file (Step 1)
printf 'Export: (main)..' printf 'Export: (main)..'
@ -1044,7 +1074,7 @@ do
done done
Output "Undo complete." Output "Undo complete."
WriteLog "Undo - Undo $LastName , TimeStamp $LastTimestamp" WriteLog "Undo - Undo ${LastName}, TimeStamp $LastTimestamp"
SetLast "Undo" "" SetLast "Undo" ""
fi fi