Background:
On multiple occasions I experienced problems with Plex that after debug, were tracked back to a corrupt database. The solution was inevitably to erase the old database and to create a new database. This solved the problem, but was expensive time-wise to hand restore all my information and settings.
I decided to write a Perl program to backup my settings so that in the case of a database corruption I can easily restore my settings.
GitHub Link:
github.com/Reed97123/plex_db_backup
I’ve benefited often from the work of others in the Plex community and I wanted to make this pubic as a small contribution to the community (in case others have had the same problem and want to share the solution I created). This won’t work for everyone, principally because this is written for Linux, Ubuntu to be specific, but it would probably be trivial to make adjustments for other Linux distributions. Much more difficult if you wanted to adapt this for Windows.
High Level Strategy (Backup):
- Checks to see if Plex is actively streaming. Terminate if active unless forced by an override.
- Shutdown Plex with systemctl
- Copy the DB to a temporary copy to operate on (abundance of caution here, this is all reading anyway)
- Record all the data in the sections:
a) accounts (User names/settings)
b) library_sections (Your libraries)
c) section_locations (Where your library files are located)
d) media_provider_resources (Your station/channel information)
e) media_subscriptions (Shows to which you are subscribed) - Dumps the data to an XML file. Default is ~/plex_backup, but you can customize.
- Copies the Plex Preferences.xml to the same backup directory.
- Startup Plex with systemctl
High Level Strategy (Restore):
- Checks to see if Plex is actively streaming. Terminate if active unless forced by an override.
- Shutdown Plex with systemctl
- Copy the current DB to a backup marked with date/time.
- Open the current DB and delete the sections: accounts/library_sections/section_locations/media_provider_resources/media_subscriptions
- Compress the DB
- Restore the deleted sections from the XML backup file.
- Startup Plex with systemctl
Final Note:
In it’s current state I’d recommend those wanting to try this be minimally proficient at Linux, ie be able to install a Perl package (instructions included) and like tasks. If this proves to be popular with people I wouldn’t mind polishing it to make it easier for less experienced people. However, I didn’t want to do that work up-front unless there is demand, so it is currently a little manual work to get initially setup.
Even if you don’t want to restore the backup, having a human readable XML file with your settings I find to be very valuable. You could be assured that if your DB becomes corrupted and you can’t access your settings, you can at minimum browse your backup XML and can still view/hand restore or use it as a reference. I find that I never remember 100% which shows I am recording, etc.