alekdavis / PlexBackup

PowerShell script to back up and restore Plex application data on a Windows system.
MIT License
174 stars 22 forks source link

Folders unavailable after Plex server restart (WIN10 Home) #4

Open friisco opened 5 years ago

friisco commented 5 years ago

Hi Alek, Backup script works just fine but after a (successful) restart of Plex Server instance, all remote folders located on a Synology NAS mapped drives, content is showing as unavailable. Manual restart of Plex solve the issue. Strange because the scheduled task is configured to run as the (only) account on the Win10 Home server as described in your help section. The Plex Server was first configured to run with the unique Microsoft account, I changed to run as local account but same issue. Thank you, Francisco

alekdavis commented 5 years ago

What do you have on the Synology NAS share: backup folder, Plex app data, media?

friisco commented 5 years ago

NAS: the media (photos, music, movies...) Backup folder: on USB local drive connected to WIN10 server (Intel NUC) Plex APP and database: on nuc server

alekdavis commented 5 years ago

Hm... Then NAS is not in play in the backup process at all. I mean, the backup script does not touch the NAS shares, right? Or did I miss something?

friisco commented 5 years ago

Correct, Backup script is "unaware" of the NAS and everything goes well during the backup process. This must be due to the Task execution context because if I run the the backup script in the current session I don't have this error. Another symptom is that once I configured to write the logs somewhere in the C: drive and I got an insufficient privilege write error. I must test once again the interactive backup execution because I didn't tried again after the change to a local account setup but I bet it will work as well.

alekdavis commented 5 years ago

I am not sure I follow your Plex server account changes (described in the first post). Changing the account under which Plex runs is a big deal (since all settings and data are account-specific). But regardless, the scheduled task (or the interactive backup session) must run under the same account as Plex Media Server (so it has the same permissions, and can access the same registry key and folders). It can be local or domain account, does not matter. In my case, the configuration looks similar to yours (PMS running on a NUC, media on a NAS), although I back up also to the same NAS share (but to a different subfolder). It worked fine, but on the last run I got an error, so I am trying to figure out what is happening there (I opened an issue), but when it ran, it was fine. I do not need the NAS share or folders mapped, though, since I used UNC paths in Plex library configurations (that was a recommendation, and I had to run a manual SQL script to update settings to use UNC paths). The only thing that is a bit weird is the Plex system tray icon, which shows duplicates until the tray refreshes (I suspect it's because of the way PowerShell stops the Plex Media Server process). Anyway, if you have any additional info, or find out something, please let me know. If you have the instructions how to duplicate the problem, I'll take a look, but I need to close my other issue first.

friisco commented 5 years ago

Microsoft account can be associated with multiple devices you own, syncing files, apps, and Windows settings contrary to a local account. Once, I thought that migrating from MSA to local one would use a distinct profile, by googling around it appears this is not the case. Profile is the same on %USERPROFILE%, the UID on registry is the same and after the migration (using "Sign in with a local account instead" from "Settings / Your Info", the session was strictly the same that before. I believe that there is no root cause to look for in that area.

I've executed the script from the PMS session and, same issue with media unavailable in PMS. No error in log, all went fine

I'll implement your suggestion to use UNC instead and I'll let you know

A workaround would be to add a "reboot" switch to the script in order to let Windows load PMS in the right context

Except Win10 home the server config is quite straightforward, it's a PMS dedicated server with no customizations, user has admin rights

alekdavis commented 5 years ago

If you want to replace mapped drive setting to UNC-based paths, you can use SQL Lite. Here are the commands (make sure you back up the library file first; the following samples change library paths from the mapped Z drive to UNC for MYNAS; run the select statements before executing the update to be sure):

sqlite3 "%localappdata%\Plex Media Server\Plug-in Support\Databases\com.plexapp.plugins.library.db"
select * from section_locations where where root_path like 'Z:\%';
update section_locations set root_path=replace(root_path, 'Z:\', '\\MYNAS\') where root_path like 'Z:\%';

select * from metadata_items where guid like '%file:%';

select * from media_streams where url like '%file:%';
update media_streams set url=replace(url, 'file://Z:\', 'file://\\MYNAS\') where url like '%file://Z:\%';

select * from media_parts where file like 'Z:\%';
update media_parts set file=replace(file, 'Z:\', '\\MYNAS\') where file like 'Z:\%';
alekdavis commented 5 years ago

Regarding your suggestion to add the reboot switch, it won't do good, since Plex requires a user to be logged in (unless you run PMS as a Windows service), and while it can reboot the computer, it won't be able to get PMS started and the idea is to leave PMS in the original state. I'm still not clear what is not working in your scenario and why mapped drives would be dropped. Makes no sense to me.

alekdavis commented 5 years ago

I'll close it for now. If you have any updates, let me know or reopen it (not sure if you can, if not, I will do).

friisco commented 5 years ago

I've changed all the PMS paths to UNC and this has solved the issue. This does not explain the reason why mapped drive didn't work but anyway, that's good. Thank you Alek for your support here. The backup was quite helpful because the SQL exercise didn't came well the first time :-). I may open another issue with the log redirection that were triggering insufficient privilege write error.

alekdavis commented 5 years ago

I said you should've backed up the database before messing with it in SQL. :-) Anyway, let me know if you run into issues.

friisco commented 5 years ago

My english seems to be not clear :-). I was of course ready with PMS backups because I knew that I would mess up with SQL, it was a breeze to restore (again and again) while I was crafting the right set of SQL commands ( I have 7 PMS libs and 10 media folders so the SQL command grew up a little bit). My SQL knowledge is below entry level so I was doing "try and fail". So no worries, everything went as per the plan and now your script runs perfectly well and the PMS restart has no more issue after going UNC. Thanks Alek

alekdavis commented 5 years ago

I just had it happen to me, so reopening the issue until I can figure out what is going on. In my case, the media files on a ReadyNas share (configured via UNC paths in the Plex database). It worked fine for months and after last two backups, Plex was not able to play media files because they were unavailable. I had to stop Plex, manually access a share and start Plex to make it work. Totally bizarre.

alekdavis commented 5 years ago

I modified the network adapter settings to not allow computer to shut down the device to save power and updated the ReadyNAS OS, and it looks like either of these (probably the former) fixed the issue.

alekdavis commented 4 years ago

Oh, man. Happened again, so re-opening.

alekdavis commented 4 years ago

One thing I noticed: if I have all users (my kids, wife) logged off before the scheduled backup tasks runs, I do not see this problem (unless it's a coincidence).

alekdavis commented 4 years ago

After testing it for a few weeks, I think the issue is related to multiple accounts. My kids have their own account that they use to access the server running Plex (it runs both server and client). They do not have the right privileges to connect to the NAS share, so the problem seems to occur when the backup runs while my kids account is logged on. I added a feature to 1.6.0 to log off all users except the active one and it seems to help, although I am not 100% sure. For now, I'll close it because I cannot think of anything else I can do.

mpk99 commented 1 year ago

hey Alek,

This one's affecting me, and i don't have multiple accounts logged in. Just my 1 account for PLEX.

Anything i can do to help troubleshoot this for you? I hate having to reboot my system every time the backup occurs...

alekdavis commented 1 year ago

@mpk99 This one has been bugging me for a few years and I still have no idea what causes it. Nothing I tried worked and I could not find a meaningful explanation from Microsoft on why a script running from a scheduled task would have this issue and an interactive script would not. Makes no sense. And don't know what you can do to help. Thanks for the offer, though.

mpk99 commented 1 year ago

@mpk99 This one has been bugging me for a few years and I still have no idea what causes it. Nothing I tried worked and I could not find a meaningful explanation from Microsoft on why a script running from a scheduled task would have this issue and an interactive script would not. Makes no sense. And don't know what you can do to help. Thanks for the offer, though.

Ok, I have one more thing i'm going to try later and see if it works and let you know. It's a bit of a long shot, but worth a try nonetheless.

mpk99 commented 1 year ago

So my theory didnt' work. Here's what i tried.

Installed WSL2 and ran powershell from inside WSL2 and it ran the script perfectly fine, did the backup, sent me an email to confirm success, but STILL can't access the NAS after restart of the PLEX service.

So it seems it's not only task scheduler that's the problem, i was gonna try to run it thru cron in wsl2, but seems that's not an option either.

alekdavis commented 1 year ago

So you have an issue even if you run the script interactively (not via a scheduled task)?

mpk99 commented 1 year ago

So you have an issue even if you run the script interactively (not via a scheduled task)?

In windows, no, it works fine Interactively. I just ran it in WSL2 (Ubuntu) and it gave that same error as Scheduled Task does...

alekdavis commented 1 year ago

Ah, okay, then we're back to square one.

54ty1BkRU5zyEvsQ commented 3 months ago

I'm having the same issue. Once the script runs and restarts plex, PMS has no access to any NAS content. I've made sure both windows and the NAS folder have read permissions via both Guest and "Everyone". Restarting PMS has always fixed the problem. Is there a way to trigger the restart as the currently logged in user or manually specified user instead of general system account when running it as a scheduled task?

runas /trustlevel:0x20000 "$Script:PlexServerPath" - i think this is where its getting messy

alekdavis commented 3 months ago

@54ty1BkRU5zyEvsQ: I'm still not sure what causes it and why. The runas command in the scheduler (I assume you have this issue when runnning backup as a scheduled job), is supposed to run as the specific user you configure it for (under the scheduler security options). Technically, you can explicitly define the credentials of the user you want to run PMS as, but then you need to save the password somwehere and I'm not sure I want to do it (it will complicate an already complex script). I suspect it's some sort of bug in the Windows scheduler, but at this point, I have no good solution other than restarting the computer (that's what I do).

54ty1BkRU5zyEvsQ commented 2 months ago

My workaround is to turn off automatic PMS restart in your script and have a batch file run PMS restart when I can be reasonably sure the backup script has finished. For some reason it can access my NAS if I do it that way.

alekdavis commented 2 months ago

@54ty1BkRU5zyEvsQ What's in your batch file? Do you start it manually?

54ty1BkRU5zyEvsQ commented 2 months ago

Ah, sorry the batch file & powershell scripts didnt fix it. I have windows task scheduler to start PMS as my logged in user about a half hour after backup starts.