Last active
December 2, 2021 04:25
-
-
Save techthoughts2/bef3ba382f14b7a24603462d575dbb1e to your computer and use it in GitHub Desktop.
This quick script will perform a backup of the plex registry settings as well as a complete backup of your Plex media server data.
This can be tied to a task scheduler for ongoing backups so you never have to worry about your Plex server data.
This d
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Requires –Modules PoshGram | |
#https://support.plex.tv/articles/201539237-backing-up-plex-media-server-data/ | |
#https://support.plex.tv/articles/202915258-where-is-the-plex-media-server-data-directory-located/ | |
# if you don't want to send yourself telegram messages the telegram portion can be removed | |
Import-Module -Name PoshGram | |
$token = '111111111:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' | |
$chat_id = '-111111111' | |
# specify backup destination | |
$backupDir = '\\backup\path\destiniation' | |
$backupDate = Get-Date -Format 'MM-dd-yyyy' | |
$TargetDir = "$backupDir\$backupDate" | |
$sendTelegramTextMessageSplat = @{ | |
BotToken = $token | |
ChatID = $chat_id | |
Message = 'Backing up Plex Media Server data' | |
} | |
Send-TelegramTextMessage @sendTelegramTextMessageSplat | |
$stopWatch = New-Object -TypeName System.Diagnostics.Stopwatch | |
$stopWatch.Start() | |
# create backup directory | |
if (-not(Test-Path -Path $TargetDir -ErrorAction Stop )) { | |
Write-Verbose -Message "Output directory not found. Creating..." | |
$newItemSplat = @{ | |
ItemType = 'Directory' | |
Path = $TargetDir | |
ErrorAction = 'Stop' | |
} | |
try { | |
New-Item @newItemSplat | |
Write-Verbose -Message "Created." | |
} | |
catch { | |
$stopWatch.Stop() | |
$sendTelegramTextMessageSplat = @{ | |
BotToken = $token | |
ChatID = $chat_id | |
Message = 'Output directory creation FAILED in {0} minutes with {1}' -f $stopWatch.Elapsed.TotalMinutes, $_ | |
} | |
Send-TelegramTextMessage @sendTelegramTextMessageSplat | |
return | |
} | |
} | |
else { | |
Write-Verbose -Message "Output directory verified." | |
} | |
# backup plex registry data | |
Invoke-Command { reg export 'HKEY_CURRENT_USER\Software\Plex, Inc.\Plex Media Server' "$TargetDir\PlexServer.reg" } | |
# backup plex media server data | |
$plexDataDir = "$env:LOCALAPPDATA\Plex Media Server\*" | |
$copyItemSplat = @{ | |
Path = $plexDataDir | |
Destination = $TargetDir | |
Recurse = $true | |
ErrorAction = 'Stop' | |
Force = $true | |
Exclude = 'Cache' | |
Container = $true | |
} | |
try { | |
Copy-Item @copyItemSplat | |
} | |
catch { | |
$stopWatch.Stop() | |
$sendTelegramTextMessageSplat = @{ | |
BotToken = $token | |
ChatID = $chat_id | |
Message = 'Backup FAILED in {0} minutes with {1}' -f $stopWatch.Elapsed.TotalMinutes, $_ | |
} | |
Send-TelegramTextMessage @sendTelegramTextMessageSplat | |
return | |
} | |
$stopWatch.Stop() | |
$sendTelegramTextMessageSplat = @{ | |
BotToken = $token | |
ChatID = $chat_id | |
Message = 'Backup completed in {0} minutes' -f $stopWatch.Elapsed.TotalMinutes | |
} | |
Send-TelegramTextMessage @sendTelegramTextMessageSplat |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment