Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sync Minecraft saves via cloud syncing solution like OneDrive
$syncScript = $PSScriptRoot + "\sync-minecraft-saves.ps1"
$minuteRepeat = 10
schtasks /create /sc minute /mo $minuteRepeat /ru System /tn "Minecraft Save Sync" /tr "powershell.exe -NoLogo -WindowStyle hidden -file $syncScript"
$minecraftSaveFolder = "minecraftWorlds"
$minecraftLocalPath = $env:LOCALAPPDATA + "\Packages\Microsoft.MinecraftUWP_8wekyb3d8bbwe\LocalState\games\com.mojang"
$minecraftLocalSave = $minecraftLocalPath + "\" + $minecraftSaveFolder
$minecraftCloudSave = $PSScriptRoot + "\MinecraftBackup"
$cloudWorlds = Get-ChildItem $minecraftCloudSave
$localWorlds = Get-ChildItem $minecraftLocalSave
$saves = Compare-Object $cloudWorlds $localWorlds -IncludeEqual
switch ($saves) {
( {$PSItem.SideIndicator -eq "<="}) {
Write-Output "Copying from cloud: $($PSItem.InputObject.Name)"
Copy-Item -Path $PSItem.InputObject.FullName -Destination $minecraftLocalSave -Container -Recurse
}
( {$PSItem.SideIndicator -eq "=>"}) {
Write-Output "Copying to cloud: $($PSItem.InputObject.Name)"
Copy-Item -Path $PSItem.InputObject.FullName -Destination $minecraftCloudSave -Container -Recurse
}
( {$PSItem.SideIndicator -eq "=="}) {
$cloudLatest = (( Get-ChildItem ( $minecraftCloudSave + "\" + $PSItem.InputObject) -Recurse -File ).LastWriteTime | Measure-Object -Maximum)
$localLatest = (( Get-ChildItem ( $minecraftLocalSave + "\" + $PSItem.InputObject) -Recurse -File ).LastWriteTime | Measure-Object -Maximum)
if ($cloudLatest.Maximum -eq $localLatest.Maximum) {
if ($cloudLatest.Count -gt $localLatest.Count) {
Write-Output "Adding files from cloud: $($PSItem.InputObject.Name)"
xcopy ($minecraftCloudSave + "\" + $PSItem.InputObject) $minecraftLocalSave /d /e /c /i /h /y
}
else {
Write-Output "Already synchronized: $($PSItem.InputObject.Name)"
}
}
elseif ($cloudLatest.Maximum -gt $localLatest.Maximum) {
$world = $PSItem.InputObject.Name
Write-Output "Over-writing local with $world"
$old = Rename-Item -Path "$minecraftLocalSave\$world" -NewName ($world + "_old") -PassThru
Copy-Item -Path "$minecraftCloudSave\$world" -Destination $minecraftLocalSave -Container -Recurse
Remove-Item -Recurse -Force -Path $old
}
elseif ($cloudLatest.Maximum -lt $localLatest.Maximum) {
$world = $PSItem.InputObject.Name
Write-Output "Updating cloud with $world"
$old = Rename-Item -Path "$minecraftCloudSave\$world" -NewName ($world + "_old") -PassThru
Copy-Item -Path "$minecraftLocalSave\$world" -Destination $minecraftCloudSave -Container -Recurse
Remove-Item -Recurse -Force -Path $old
}
else {
Write-Error "Something went wrong with the folder comparison."
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.