Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ### Valheim data backup script
- ### Backs up the entire appdata folder including characters/world data
- ## Note: If you get errors running the script, run the following:
- ## Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope CurrentUser
- ## Configuration
- # How many backups to keep before pruning older copies
- # 0 keeps all backups
- $NumToKeep = 7
- # Send pruned backups to recycle bin (false means permanent delete)
- $UseRecycleBin = $true
- # Where to save backups (default: yourdesktop\Valheim_Backups)
- $BackupFolderPath = "$env:USERPROFILE\Desktop\Valheim_Backups"
- # Name of each unique backup folder (default yyyyMMdd-HHmm, e.g. 20210212-1238)
- $FolderName = Get-Date -Format "yyyyMMdd-HHmm"
- ## Main Script (modify at own risk)
- $DestinationPath = "$BackupFolderPath\$FolderName"
- # Create the backup directory
- if ((Test-Path $DestinationPath) -eq $false) {
- New-Item `
- -Path $BackupFolderPath `
- -Name $FolderName `
- -ItemType "directory" | Out-Null
- }
- # Copy valheim appdata
- Copy-Item `
- "$env:USERPROFILE\AppData\LocalLow\IronGate\Valheim\*" `
- -Destination $DestinationPath `
- -Force -Recurse
- # Prune old backups
- if ($NumToKeep -gt 0) {
- $ExistingBackups = Get-ChildItem -Path $BackupFolderPath -Directory | Sort-Object -Descending
- for ($i = 0; $i -lt $ExistingBackups.Count; $i++) {
- if ($i -gt ($NumToKeep - 1)) {
- if ($UseRecycleBin) {
- Add-Type -AssemblyName Microsoft.VisualBasic
- [Microsoft.VisualBasic.FileIO.FileSystem]::Deletedirectory(
- $ExistingBackups[$i],
- 'OnlyErrorDialogs',
- 'SendToRecycleBin'
- )
- } else {
- Remove-Item $ExistingBackups[$i] -Recurse -Force
- }
- }
- }
- }
RAW Paste Data