Skip to content

Instantly share code, notes, and snippets.

@007revad
Last active January 24, 2023 06:57
Show Gist options
  • Save 007revad/0ffa5994b72ff9a570804b1dbb712a6b to your computer and use it in GitHub Desktop.
Save 007revad/0ffa5994b72ff9a570804b1dbb712a6b to your computer and use it in GitHub Desktop.
Bash script to test replacing Machine IDs in Plex's Preferences.xml from a backup Preferences.xml
#!/usr/bin/env bash
#-------------------------------------------------------------------------
# https://gist.github.com/007revad
# Script verified at https://www.shellcheck.net/
#--------------------------------------------------------------------------
# Note: The echo lines are just for verifying it's doing what it should
#--------------------------------------------------------------------------
path="~/plex_sync_test"
path_bak="~/plex_sync_bak_test"
file_bak=Preferences-bak.xml # backup file
file_new=Preferences.xml # file to edit
echo # debug
# Get backup Preferences.xml file's ID values
echo "Backup File: ${path_bak}/${file_bak}" # debug
AnonymousMachineIdentifier=$(grep -oP '(?<=\bAnonymousMachineIdentifier=").*?(?=(" |"/>))' "${path_bak}/${file_bak}")
echo "Back_AnonymousMachineIdentifier = $AnonymousMachineIdentifier" # debug
CertificateUUID=$(grep -oP '(?<=\bCertificateUUID=").*?(?=(" |"/>))' "${path_bak}/${file_bak}")
echo "Back_CertificateUUID = $CertificateUUID" # debug
FriendlyName=$(grep -oP '(?<=\bFriendlyName=").*?(?=(" |"/>))' "${path_bak}/${file_bak}")
echo "FriendlyName = $FriendlyName" # debug
LastAutomaticMappedPort=$(grep -oP '(?<=\bLastAutomaticMappedPort=").*?(?=(" |"/>))' "${path_bak}/${file_bak}")
echo "LastAutomaticMappedPort = $LastAutomaticMappedPort" # debug
MachineIdentifier=$(grep -oP '(?<=\bMachineIdentifier=").*?(?=(" |"/>))' "${path_bak}/${file_bak}")
echo "Back_MachineIdentifier = $MachineIdentifier" # debug
ManualPortMappingPort=$(grep -oP '(?<=\bManualPortMappingPort=").*?(?=(" |"/>))' "${path_bak}/${file_bak}")
echo "ManualPortMappingPort = $ManualPortMappingPort" # debug
PlexOnlineToken=$(grep -oP '(?<=\bPlexOnlineToken=").*?(?=(" |"/>))' "${path_bak}/${file_bak}")
echo "PlexOnlineToken = $PlexOnlineToken" # debug
ProcessedMachineIdentifier=$(grep -oP '(?<=\bProcessedMachineIdentifier=").*?(?=(" |"/>))' "${path_bak}/${file_bak}")
echo "Back_ProcessedMachineIdentifier = $ProcessedMachineIdentifier" # debug
echo # debug
# Get synced Preferences.xml file's ID values (so we can replace them)
echo "Main File: ${path}/${file_new}" # debug
Main_AnonymousMachineIdentifier=$(grep -oP '(?<=\bAnonymousMachineIdentifier=").*?(?=(" |"/>))' "${path}/${file_new}")
echo "Main_AnonymousMachineIdentifier = $Main_AnonymousMachineIdentifier" # debug
Main_CertificateUUID=$(grep -oP '(?<=\bCertificateUUID=").*?(?=(" |"/>))' "${path}/${file_new}")
echo "Main_CertificateUUID = $Main_CertificateUUID" # debug
Main_FriendlyName=$(grep -oP '(?<=\bFriendlyName=").*?(?=(" |"/>))' "${path}/${file_new}")
echo "Main_FriendlyName = $Main_FriendlyName" # debug
Main_LastAutomaticMappedPort=$(grep -oP '(?<=\bLastAutomaticMappedPort=").*?(?=(" |"/>))' "${path}/${file_new}")
echo "Main_LastAutomaticMappedPort = $Main_LastAutomaticMappedPort" # debug
Main_MachineIdentifier=$(grep -oP '(?<=\bMachineIdentifier=").*?(?=(" |"/>))' "${path}/${file_new}")
echo "Main_MachineIdentifier = $Main_MachineIdentifier" # debug
Main_ManualPortMappingPort=$(grep -oP '(?<=\bManualPortMappingPort=").*?(?=(" |"/>))' "${path}/${file_new}")
echo "Main_ManualPortMappingPort = $Main_ManualPortMappingPort" # debug
Main_PlexOnlineToken=$(grep -oP '(?<=\bPlexOnlineToken=").*?(?=(" |"/>))' "${path}/${file_new}")
echo "Main_PlexOnlineToken = $Main_PlexOnlineToken" # debug
Main_ProcessedMachineIdentifier=$(grep -oP '(?<=\bProcessedMachineIdentifier=").*?(?=(" |"/>))' "${path}/${file_new}")
echo "Main_ProcessedMachineIdentifier = $Main_ProcessedMachineIdentifier" # debug
echo # debug
# Change synced Preferences.xml ID values to backed up ID values
sed -i "s/ AnonymousMachineIdentifier=\"AnonymousMachineIdentifier=\"${Main_AnonymousMachineIdentifier}/ AnonymousMachineIdentifier=\"${AnonymousMachineIdentifier}/g" "${path}/${file_new}"
sed -i "s/ CertificateUUID=\"${Main_CertificateUUID}/ CertificateUUID=\"${CertificateUUID}/g" "${path}/${file_new}"
sed -i "s/ FriendlyName=\"${Main_FriendlyName}/ FriendlyName=\"${FriendlyName}/g" "${path}/${file_new}"
sed -i "s/ LastAutomaticMappedPort=\"${Main_LastAutomaticMappedPort}/ LastAutomaticMappedPort=\"${LastAutomaticMappedPort}/g" "${path}/${file_new}"
sed -i "s/ MachineIdentifier=\"${Main_MachineIdentifier}/ MachineIdentifier=\"${MachineIdentifier}/g" "${path}/${file_new}"
sed -i "s/ ManualPortMappingPort=\"${Main_ManualPortMappingPort}/ ManualPortMappingPort=\"${ManualPortMappingPort}/g" "${path}/${file_new}"
sed -i "s/ PlexOnlineToken=\"${Main_PlexOnlineToken}/ PlexOnlineToken=\"${PlexOnlineToken}/g" "${path}/${file_new}"
sed -i "s/ ProcessedMachineIdentifier=\"${Main_ProcessedMachineIdentifier}/ ProcessedMachineIdentifier=\"${ProcessedMachineIdentifier}/g" "${path}/${file_new}"
echo # debug
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment