Created
May 26, 2025 22:54
-
-
Save glektarssza/7723a8e4333e89fa59370f80e7f14b01 to your computer and use it in GitHub Desktop.
Prism Launcher Backup Creator
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
<# | |
.SYNOPSIS | |
Create a new backup of a Prism Launcher instance. | |
.DESCRIPTION | |
Create a new backup of a Prism launcher instance at the specified | |
destination. If the instance directory is not specified it will be looked | |
for in some common locations. These are, in order: | |
* `$env:APPDATA\PrismLauncher\instances` | |
* `$env:USERPROFILE\scoop\persist\prismlauncher\instances` | |
If the instance directory is not found, the command will fail. | |
.PARAMETER InstanceName | |
The name of the Prism Launcher instance to backup. | |
.PARAMETER Destination | |
The destination path where the backup will be created. This can be a full | |
file name or a directory. If a directory is specified, the backup will be | |
created in that directory with the name of the instance and a timestamp in | |
the format of `yyyy-MM-dd_HH-mm-ss.zip`. The directory and any required | |
parent directories will be created if they do not exist. | |
.PARAMETER InstanceDirectory | |
The directory where the Prism Launcher instance is located. If not specified, | |
the command will look for the instances directory in common locations. | |
.EXAMPLE | |
New-PrismInstanceBackup -InstanceName "MyInstance" -Destination "D:\PrismBackups\MyInstance.zip" | |
# Create a backup of the Prism Launcher instance "MyInstance" to a specified file. | |
.EXAMPLE | |
New-PrismInstanceBackup -InstanceName "MyInstance" -Destination "D:\PrismBackups" | |
# Create a backup of the Prism Launcher instance "MyInstance" to a specified directory. | |
# This will create a file named "MyInstance_<SOME_DATE_TIME>.zip" in the directory "D:\PrismBackups". | |
.EXAMPLE | |
New-PrismInstanceBackup -InstanceName "MyInstance" -Destination "D:\PrismBackups\MyInstance.zip" -InstanceDirectory "D:\PrismLauncher\instances" | |
# Create a backup of the Prism Launcher instance "MyInstance" to the specified | |
# file using the instance directory "D:\PrismLauncher\instances". | |
.COMPONENT | |
Backup | |
Prism Launcher | |
Instance Management | |
Minecraft | |
.FUNCTIONALITY | |
Backup Prism Launcher instances to a specified destination. | |
.LINK | |
https://prismlauncher.org | |
https://github.com/PrismLauncher/PrismLauncher | |
.NOTES | |
This function is completely untested and is used at your own risk. | |
HC SVNT DRACONES | |
#> | |
function New-PrismInstanceBackup { | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)] | |
[ValidateNotNullOrEmpty()] | |
[String] | |
$InstanceName, | |
[Parameter(Mandatory = $false, Position = 1)] | |
[String] | |
$Destination, | |
[Parameter(Mandatory = $false)] | |
[String] | |
$InstanceDirectory, | |
[Parameter(Mandatory = $false)] | |
[switch] | |
$PerInstanceDestinationSubdirectory | |
) | |
if ($null -eq $Destination -or $Destination -eq "") { | |
if ($env:OneDrive -ne "") { | |
$Destination = Join-Path -Path "$env:OneDrive" -ChildPath "PrismLauncherBackups"; | |
} | |
else { | |
Write-Error "Destination path is required. Please specify a valid destination path." ` | |
-RecommendedAction "Specify a valid destination path using the Destination parameter." ` | |
-Category InvalidArgument -ErrorId "PrismLauncherBackupDestinationRequired" ` | |
-CategoryTargetType "PrismLauncherBackupDestination" -CategoryTargetName "Destination" ` | |
-CategoryReason "Destination path cannot be null or empty." -CategoryActivity "Check Backup Destination"; | |
} | |
} | |
if ($null -eq $InstanceDirectory -or $InstanceDirectory -eq "") { | |
$local:prismLauncher = (Get-Command -Name prismlauncher -ErrorAction SilentlyContinue); | |
if ($null -eq $local:prismLauncher) { | |
Write-Error "Prism Launcher command not found. Please ensure Prism Launcher is installed and available in your PATH." ` | |
-RecommendedAction "Install Prism Launcher or ensure it is in your PATH." -Category ObjectNotFound ` | |
-CategoryActivity "Find Prism Launcher Command" -ErrorId "PrismLauncherNotFound" ` | |
-CategoryTargetType "PrismLauncherCommand" -CategoryTargetName "PrismLauncher" ` | |
-CategoryReason "Prism Launcher command not found in the current environment."; | |
return; | |
} | |
$local:instanceDirectory = Join-Path -Path "$env:APPDATA" -ChildPath "PrismLauncher\instances"; | |
if (-not (Test-Path -Path "$local:instanceDirectory" -PathType Container)) { | |
$local:instanceDirectory = Join-Path -Path "$env:USERPROFILE" -ChildPath "scoop\persist\prismlauncher\instances"; | |
if (-not (Test-Path -Path "$local:instanceDirectory" -PathType Container)) { | |
Write-Error "Prism Launcher instance directory not found. Please specify a valid instance directory." ` | |
-RecommendedAction "Specify a valid instance directory using the InstanceDirectory parameter." ` | |
-CategoryActivity "Find Prism Launcher Instance Directory" -Category ObjectNotFound ` | |
-ErrorId "PrismLauncherInstanceDirectoryNotFound" -CategoryTargetType "PrismLauncherInstanceDirectory" ` | |
-CategoryTargetName "PrismLauncherInstanceDirectory" -CategoryReason "Prism Launcher instance directory not found in common locations."; | |
return; | |
} | |
} | |
if (-not (Test-Path -Path "$local:instanceDirectory" -PathType Container)) { | |
Write-Debug "Prism Launcher instance directory not found in app data, looking for Scoop installation"; | |
$local:instanceDirectory = Join-Path -Path "$env:USERPROFILE" -ChildPath "scoop\persist\prismlauncher\instances"; | |
} | |
} | |
else { | |
Write-Debug "Using provided Prism Launcher instance directory of '$InstanceDirectory'."; | |
$local:instanceDirectory = $InstanceDirectory; | |
} | |
if (-not (Test-Path -Path "$local:instanceDirectory" -PathType Container)) { | |
Write-Error "Prism Launcher instance directory not found. Please specify a valid instance directory." ` | |
-RecommendedAction "Specify a valid instance directory using the InstanceDirectory parameter." ` | |
-CategoryActivity "Find Prism Launcher Instance Directory" -Category ObjectNotFound ` | |
-ErrorId "PrismLauncherInstanceDirectoryNotFound" -CategoryTargetType "PrismLauncherInstanceDirectory" ` | |
-CategoryTargetName "PrismLauncherInstanceDirectory" -CategoryReason "Prism Launcher instance directory not found in common locations."; | |
return; | |
} | |
Write-Debug "Prism Launcher instance directory found at '$local:instanceDirectory'."; | |
$local:instancePath = Join-Path -Path "$local:instanceDirectory" -ChildPath "$InstanceName"; | |
if (-not (Test-Path -Path "$local:instancePath" -PathType Container)) { | |
Write-Error "Prism Launcher instance '$InstanceName' not found in instance directory '$local:instanceDirectory'." ` | |
-RecommendedAction "Ensure the instance name is correct." -Category ObjectNotFound ` | |
-ErrorId "PrismLauncherInstanceNotFound" -CategoryTargetType "PrismLauncherInstance" ` | |
-CategoryTargetName $InstanceName -CategoryReason "Prism Launcher instance not found in the specified directory."; | |
return; | |
} | |
Write-Debug "Prism Launcher instance path found at '$local:instancePath'."; | |
if (Test-Path -Path "$Destination" -PathType Leaf) { | |
Write-Error "Destination '$Destination' already exists as a file. Please specify a different destination." ` | |
-RecommendedAction "Specify a different destination path." -Category InvalidArgument ` | |
-ErrorId "PrismLauncherBackupDestinationExists" -CategoryTargetType "PrismLauncherBackupDestination" ` | |
-CategoryTargetName $Destination -CategoryReason "Destination path already exists as a file." ` | |
-CategoryActivity "Check Backup Destination" -TargetObject $Destination; | |
return; | |
} | |
Write-Debug "Creating backup of Prism Launcher instance '$InstanceName' to destination '$Destination'."; | |
$local:tempBackupFile = Join-Path -Path ([System.IO.Path]::GetTempPath()) -ChildPath ([System.IO.Path]::GetRandomFileName()); | |
try { | |
Compress-Archive -Path "$local:instancePath" -DestinationPath "$local:tempBackupFile" -CompressionLevel NoCompression; | |
if ($Destination.EndsWith(".zip")) { | |
$local:backupDirectory = [System.IO.Path]::GetDirectoryName([System.IO.Path]::GetFullPath($Destination)); | |
$local:backupFileName = [System.IO.Path]::GetFileName([System.IO.Path]::GetFullPath($Destination)); | |
} | |
elseif ($PerInstanceDestinationSubdirectory) { | |
$local:backupDirectory = Join-Path -Path "$Destination" -ChildPath "$($InstanceName.Replace(" ", "_"))"; | |
$local:backupFileName = "$((Get-Date).ToString('yyyy-MM-dd_HH-mm-ss')).zip"; | |
} | |
else { | |
$local:backupDirectory = "$Destination"; | |
$local:backupFileName = "$($InstanceName.Replace(" ", "_"))_$((Get-Date).ToString('yyyy-MM-dd_HH-mm-ss')).zip"; | |
} | |
if (-not (Test-Path -Path "$local:backupDirectory" -PathType Container)) { | |
New-Item -Path "$local:backupDirectory" -ItemType Directory -Force | Out-Null; | |
} | |
$local:backupFilePath = Join-Path -Path "$local:backupDirectory" -ChildPath "$local:backupFileName"; | |
Move-Item -Path "$local:tempBackupFile" -Destination "$local:backupFilePath" -Force; | |
} | |
catch { | |
Write-Error "Failed to create backup archive. $_" -ErrorId "PrismLauncherBackupFailed" -TargetObject $_; | |
} | |
finally { | |
if (Test-Path -Path "$local:tempBackupFile") { | |
Remove-Item -Path "$local:tempBackupFile" -Force -ErrorAction SilentlyContinue; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment