PSA: Script to back up and restore Plex app data on a Windows system

If you are interested in simplifying the Plex backup and restore process, check out the PowerShell script I wrote:

To back Plex app data up, the script does the following:

  • Stops Plex services (if any).
  • Stops Plex Media Server.
  • Backs up essential Plex app data folders.
  • Backs up Plex Windows registry key.
  • Starts previously stopped Plex services.
  • Starts Plex Media Server.
  • Sends email with backup summary (optional).

To restore Plex app data, the script, goes over similar motions: it stops the Plex services and media server process, restores Plex app data folders and files, restores the registry, and restarts the services and media server.

You can chose an option to (a) compress Plex app data folders into ZIP files (default) or (b) copy the whole Plex app data folder structure (without the non-essential folders) using Robocopy. If the script fails, you can fix the problem and run it again in the Continue mode. In this case, the script will resume from the previously failed step. You can override the default settings in-line or specify them via a config file. To make sure the restore process does not break anything, you can tell the script to not start the Plex Media Server process upon completion.

You must run the script as Administrator and may need to adjust the PowerShell script execution policy as described in the comments (standard PowerShell stuff).

For a more detailed description, see the script comments or run the PowerShell Get-Help command (e.g. Get-Help .\PlexBackup.ps1).

I have not extensively tested it, but I used the script to move my Plex app data from a NAS share to a local drive and it worked fine. If you see any problems, let me know. I hope it helps someone.

Cheers.

15 Likes

Updated the readme file with comprehensive documentation.

1 Like

Thanks for sharing this.

1 Like

Added an option to send email notification after the script finishes the backup or restore job.

1 Like

Wow. Nice work. I just had a server failure at home. I do backups with Backblaze, but it was failing on all the open files for Plex. I was going to write quick script with 7 zip to stop the services, grab the important directories and a reg export. I googled to see of there were any samples and found yours. Great work. It is the perfect mix of it will basically work out of the box but has every option you could want. It worked perfectly for me to dump to a secondary drive that will the be picked up by Backblaze. I hope you work in an industry where you are putting this type of thought to use. Again, thanks and nice work.

1 Like

Hello, I just realized I’m having an issue when running this as a scheduled task in Windows. It looks like it runs perfectly when running from the command line. I used the same command to run as a task.
powershell -file c:\script\PlexBackup.ps1 -Type 7zip -BackupRootDir D:\Files\PlexBackups
It created the folder structure, and I assumed it worked. I then noticed my backup folders were only about 5 KB instead of 1+ GB. The 0 folder has a 2 KB Plex.7z file and the reg export worked. The other directories are empty.

C:\Users\<name>\AppData\Local\Temp

is full of large files named PlexBackup-…-.7z (I can show the specifics if needed)
So, run from a cmd prompt works, running from a scheduled task as the same user with elevated rights fails. I tried adding these 2 switches to get more info
-Log -Errorlog
It failed in the same way, and left the .7z files in the temp folder.

PlexBackup v1.5.3 (c) 2019 Alek Davis
Script started at:
  04/24/2019 12:39:05
Operation mode:
  BACKUP
Backup type:
  7ZIP
Log file:
  D:\Files\PlexBackups\20190424123905\Backup.log
Error log file:
  D:\Files\PlexBackups\20190424123905\Backup.err.log
Plex Media Server version (CURRENT):
  1.15.3.876
Stopping Plex service(s):
  PlexService
  Plex Update Service
Backup will be saved in:
  D:\Files\PlexBackups\20190424123905
Deleting old backup folder(s):
  20190424103442
  20190424102859
  20190424102313
  20190424101751
Creating task-specific subfolder(s) in:
  D:\Files\PlexBackups\20190424123905
Backing up special subfolders.
Moving folder:
  C:\Users\Matthew <name>\AppData\Local\Plex Media Server\Plug-in Support\Data\com.plexapp.system\DataItems\Deactivated
to:
  D:\Files\PlexBackups\20190424123905\3\Plug-in Support\Data\com.plexapp.system\DataItems\Deactivated
at:
  2019/04/24 12:39:06.178
Completed at:
  2019/04/24 12:39:06.197
Backing up Plex app data files from:
  C:\Users\Matthew <name>\AppData\Local\Plex Media Server
to:
  D:\Files\PlexBackups\20190424123905\0\Plex.7z
at:
  2019/04/24 12:39:06.213
Completed at:
  2019/04/24 12:39:06.287
Backing up Plex app data folders from:
  C:\Users\Matthew <name>\AppData\Local\Plex Media Server
to:
  D:\Files\PlexBackups\20190424123905\1
Archiving:
  C:\Users\Matthew <name>\AppData\Local\Plex Media Server\Cache
to temp file:
  C:\Users\MATTHE~1\AppData\Local\Temp\PlexBackup-e8f3c1c0-7b25-46a6-b296-4f8d35832dc9.7z
at:
  2019/04/24 12:39:06.325
Completed at:
  2019/04/24 12:40:16.495
Copying temp file:
  C:\Users\MATTHE~1\AppData\Local\Temp\PlexBackup-e8f3c1c0-7b25-46a6-b296-4f8d35832dc9.7z
to:
  D:\Files\PlexBackups\20190424123905\1\Cache.7z
at:
  2019/04/24 12:40:16.503
Start-BitsTransfer : The operation being requested was not performed because the user has not 
logged on to the network. The specified service does not exist. (Exception from HRESULT: 
0x800704DD)
At C:\script\PlexBackup.ps1:2488 char:17
+ ...             Start-BitsTransfer -Source $tempZipFilePath -Destination  ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Start-BitsTransfer], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException,Microsoft.BackgroundInte 
   lligentTransfer.Management.NewBitsTransferCommand
 
Archiving:
  C:\Users\Matthew <name>\AppData\Local\Plex Media Server\Codecs
to temp file:
  C:\Users\MATTHE~1\AppData\Local\Temp\PlexBackup-41d6c000-b133-4d4a-815b-75860d169c03.7z
at:
  2019/04/24 12:40:18.892
Completed at:
  2019/04/24 12:40:19.436
Copying temp file:
  C:\Users\MATTHE~1\AppData\Local\Temp\PlexBackup-41d6c000-b133-4d4a-815b-75860d169c03.7z
to:
  D:\Files\PlexBackups\20190424123905\1\Codecs.7z
at:
  2019/04/24 12:40:19.443
Start-BitsTransfer : The operation being requested was not performed because the user has not 
logged on to the network. The specified service does not exist. (Exception from HRESULT: 
0x800704DD)
At C:\script\PlexBackup.ps1:2488 char:17
+ ...             Start-BitsTransfer -Source $tempZipFilePath -Destination  ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Start-BitsTransfer], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException,Microsoft.BackgroundInte 
   lligentTransfer.Management.NewBitsTransferCommand
 
Archiving:
  C:\Users\Matthew <name>\AppData\Local\Plex Media Server\Default
to temp file:
  C:\Users\MATTHE~1\AppData\Local\Temp\PlexBackup-b44d69bb-dbfc-43b2-8acf-c4c50d56ad30.7z
at:
  2019/04/24 12:40:19.472
Completed at:
  2019/04/24 12:40:25.550
Copying temp file:
  C:\Users\MATTHE~1\AppData\Local\Temp\PlexBackup-b44d69bb-dbfc-43b2-8acf-c4c50d56ad30.7z
to:
  D:\Files\PlexBackups\20190424123905\1\Default.7z
at:
  2019/04/24 12:40:25.558
Start-BitsTransfer : The operation being requested was not performed because the user has not 
logged on to the network. The specified service does not exist. (Exception from HRESULT: 
0x800704DD)
At C:\script\PlexBackup.ps1:2488 char:17
+ ...             Start-BitsTransfer -Source $tempZipFilePath -Destination  ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Start-BitsTransfer], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException,Microsoft.BackgroundInte 
   lligentTransfer.Management.NewBitsTransferCommand
 
Archiving:
  C:\Users\Matthew <name>\AppData\Local\Plex Media Server\Media
to temp file:
  C:\Users\MATTHE~1\AppData\Local\Temp\PlexBackup-03af16c0-dcef-4919-a3c1-b992ffd423df.7z
at:
  2019/04/24 12:40:25.590
Completed at:
  2019/04/24 12:41:19.871
Copying temp file:
  C:\Users\MATTHE~1\AppData\Local\Temp\PlexBackup-03af16c0-dcef-4919-a3c1-b992ffd423df.7z
to:
  D:\Files\PlexBackups\20190424123905\1\Media.7z
at:
  2019/04/24 12:41:19.879
Start-BitsTransfer : The operation being requested was not performed because the user has not 
logged on to the network. The specified service does not exist. (Exception from HRESULT: 
0x800704DD)
At C:\script\PlexBackup.ps1:2488 char:17
+ ...             Start-BitsTransfer -Source $tempZipFilePath -Destination  ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Start-BitsTransfer], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException,Microsoft.BackgroundInte 
   lligentTransfer.Management.NewBitsTransferCommand
 
Archiving:
  C:\Users\Matthew <name>\AppData\Local\Plex Media Server\Metadata
to temp file:
  C:\Users\MATTHE~1\AppData\Local\Temp\PlexBackup-2b9d5e65-6c2e-483a-a8f3-29db47bd923f.7z
at:
  2019/04/24 12:41:19.928
Completed at:
  2019/04/24 12:42:04.735
Copying temp file:
  C:\Users\MATTHE~1\AppData\Local\Temp\PlexBackup-2b9d5e65-6c2e-483a-a8f3-29db47bd923f.7z
to:
  D:\Files\PlexBackups\20190424123905\1\Metadata.7z
at:
  2019/04/24 12:42:04.743
Start-BitsTransfer : The operation being requested was not performed because the user has not 
logged on to the network. The specified service does not exist. (Exception from HRESULT: 
0x800704DD)
At C:\script\PlexBackup.ps1:2488 char:17
+ ...             Start-BitsTransfer -Source $tempZipFilePath -Destination  ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Start-BitsTransfer], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException,Microsoft.BackgroundInte 
   lligentTransfer.Management.NewBitsTransferCommand
 
Archiving:
  C:\Users\Matthew <name>\AppData\Local\Plex Media Server\Plug-in Support
to temp file:
  C:\Users\MATTHE~1\AppData\Local\Temp\PlexBackup-320480c5-bc2f-4bd7-bc92-fdeb84aec93e.7z
at:
  2019/04/24 12:42:04.784
Completed at:
  2019/04/24 12:42:21.871
Copying temp file:
  C:\Users\MATTHE~1\AppData\Local\Temp\PlexBackup-320480c5-bc2f-4bd7-bc92-fdeb84aec93e.7z
to:
  D:\Files\PlexBackups\20190424123905\1\Plug-in Support.7z
at:
  2019/04/24 12:42:21.883
Start-BitsTransfer : The operation being requested was not performed because the user has not 
logged on to the network. The specified service does not exist. (Exception from HRESULT: 
0x800704DD)
At C:\script\PlexBackup.ps1:2488 char:17
+ ...             Start-BitsTransfer -Source $tempZipFilePath -Destination  ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Start-BitsTransfer], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException,Microsoft.BackgroundInte 
   lligentTransfer.Management.NewBitsTransferCommand
 
Archiving:
  C:\Users\Matthew <name>\AppData\Local\Plex Media Server\Plug-ins
to temp file:
  C:\Users\MATTHE~1\AppData\Local\Temp\PlexBackup-392a71cc-8736-42a1-a600-e35bb61c88be.7z
at:
  2019/04/24 12:42:21.924
Completed at:
  2019/04/24 12:42:22.753
Copying temp file:
  C:\Users\MATTHE~1\AppData\Local\Temp\PlexBackup-392a71cc-8736-42a1-a600-e35bb61c88be.7z
to:
  D:\Files\PlexBackups\20190424123905\1\Plug-ins.7z
at:
  2019/04/24 12:42:22.761
Start-BitsTransfer : The operation being requested was not performed because the user has not 
logged on to the network. The specified service does not exist. (Exception from HRESULT: 
0x800704DD)
At C:\script\PlexBackup.ps1:2488 char:17
+ ...             Start-BitsTransfer -Source $tempZipFilePath -Destination  ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Start-BitsTransfer], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException,Microsoft.BackgroundInte 
   lligentTransfer.Management.NewBitsTransferCommand
 
Archiving:
  C:\Users\Matthew <name>\AppData\Local\Plex Media Server\Scanners
to temp file:
  C:\Users\MATTHE~1\AppData\Local\Temp\PlexBackup-2aa5497e-90fc-447a-83aa-119a84c7c754.7z
at:
  2019/04/24 12:42:22.794
Completed at:
  2019/04/24 12:42:22.825
Copying temp file:
  C:\Users\MATTHE~1\AppData\Local\Temp\PlexBackup-2aa5497e-90fc-447a-83aa-119a84c7c754.7z
to:
  D:\Files\PlexBackups\20190424123905\1\Scanners.7z
at:
  2019/04/24 12:42:22.832
Start-BitsTransfer : The operation being requested was not performed because the user has not 
logged on to the network. The specified service does not exist. (Exception from HRESULT: 
0x800704DD)
At C:\script\PlexBackup.ps1:2488 char:17
+ ...             Start-BitsTransfer -Source $tempZipFilePath -Destination  ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Start-BitsTransfer], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException,Microsoft.BackgroundInte 
   lligentTransfer.Management.NewBitsTransferCommand
 
Backing up registry key:
  HKCU\Software\Plex, Inc.\Plex Media Server
to:
  D:\Files\PlexBackups\20190424123905\2\Plex.reg
Restoring special subfolders.
Copying folder:
  D:\Files\PlexBackups\20190424123905\3\Plug-in Support\Data\com.plexapp.system\DataItems\Deactivated
to:
  C:\Users\Matthew <name>\AppData\Local\Plex Media Server\Plug-in Support\Data\com.plexapp.system\DataItems\Deactivated
at:
  2019/04/24 12:42:22.909
Completed at:
  2019/04/24 12:42:22.927
Starting Plex service(s):
  PlexService
  Plex Update Service
Script ended at:
  2019/04/24 12:42:24.336
Script ran for (hr:min:sec.msec):
  00:03:19.241
Script returned:
  SUCCESS
Done.

Any ideas? Some sort of environment not working when it is a task maybe?

Seems like it may be this issue:
https://serverfault.com/questions/900689/how-to-properly-run-powershell-scripts-containing-bits-operations-from-task-sche

Update: I fixed my own issue. I’ll leave this here in case anyone else finds this. I had to enable "Run only when user is logged on" in the scheduled task as mentioned in the link above. I didn’t realize that made a difference, since the user is logged in anyway. With that option enabled, I actually saw the powershell script run on the console, and everything worked as expected.

2 Likes

Thanks for the update @mkitchinpl. I do not run Plex as a service, so I haven’t tested this path (did not realize that Start-BitsTransfer requires a logged in user). Also, I did not realize that Start-BitsTransfer exception does not get caught in the exception handler block. I will update the instructions and fix code to make sure the Start-BitsTransfer errors are properly handled (the script should not return success if the file transfer operation fails). Thanks again for the feedback… If you run into other problems, please submit an issue at Github.

1 Like

Thanks. I will post in Github next time. I honestly didn’t because I assumed it was user error not a real ā€œissueā€. I meant to remove the part about the service issue after I troubleshot some more. Something about running it over and over again got the service status out of whack and it couldn’t be stopped or started. A reboot fixed that. In the end, the core problem was the Start-BitsTransfer requires a logged in user
issue. Thanks for the response and an awesome script.

1 Like

Thank you so much !!!

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

I’ve had so many issues backing up plex over the years. Thanks a lot for doing this, it’s super helpful. I’m actually rebuilding my PC now, and doing a backup with your utility as we speak. Hopefully the restore works :slight_smile:

Thanks again man

1 Like

Thank you again tou are my hero !!!

1 Like

ā€œYou must launch PlexBackup as Administrator while being logged in under the same account your Plex Media Server runs.ā€

The account under which I run Plex is a normal user account. If I ā€œrun as administratorā€ it runs under the context of a different account. So that has a different AppData folder and different registry keys.

I can get around the AppData folder by specifying the value in config. The reg key fix required hard-coding the reg key in the backup script, but I couldn’t get the test-path check to work so just left it as is.

Am I missing an obvious fix (besides the obvious of making the Plex account an admin)?

No, you don’t, but I do not see how to implement it in any other way. The thing is: the default PMS app directory belongs to a user profile. I believe it was a bad design decision, but that’s what we got. And the same is true about the registry key. I wish, PMS were decoupled from the user profile (you can’t really run multiple PMS instances on the same computer, so what’s the point?). So a backup process needs access to the PMS data files/registry key and it also has to use admin privileges for such operations like stopping/starting Windows services. If you can suggest a workaround, please feel free to submit it in an issue, but at this point, I got nothing.

1 Like

I think the backup script can also run under the same user account as the Plex server itself, right?

The location of the Plex data folder can be changed, so it doesn’t have to be located inside the Users folder. Thus inhering the access permissions of that user.
You can set custom permissions for the custom folder, if you have to.

Right, but that’s not the issue. The problem is that his PMS account is not an administrator, so backup would fail to perform admin tasks like stopping services. Also, while you can use a custom folder, the registry keys are still under HKCU and HKU/.DEFAULT.

Thanks guys. I was curious about what admin access was needed for, so questions answered.

1 Like

I’m trying to run this script, and when I enter the command ā€œ.\PlexBackup.ps1ā€, nothing happens… no error message, nothing. I have no experience running Powershell scripts, which could definitely be coming into play here.

Any help would be much appreciated. Thanks!

Please post it at the issues section on the Github project side and include a screenshot (maybe you’re not running it from PowerShell?): Issues Ā· alekdavis/PlexBackup Ā· GitHub