Advertisement
Ubidibity

dayz-bak.ps1

Jan 8th, 2025 (edited)
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Example: Define the folder and identify changed files
  2. # 20250124 revision: I added AnalyzeZip.ps1 to display a summary.  If this isn't helpful you can comment or delete line 37
  3. # AnalyzeZip available at https://pastebin.com/wDntDwJT
  4.  
  5. $folderPath = "c:\dayz-server\"
  6. $destFolder = "E:\DayZ_Backups\"
  7. $timestamp = Get-Date -format "yyMMddHHmm"
  8. $destinationZip = "$destFolder$timestamp-dayz-server-bak.zip"
  9.  
  10. # Enumerate changed items (example: files modified in the last day)
  11. $sourceItems = Get-ChildItem -Path $folderPath -File -Recurse | Where-Object { $_.LastWriteTime -gt (Get-Date).AddDays(-1) }
  12.  
  13. # Create an array of relative paths to preserve directory structure
  14. $filePaths = $sourceItems | ForEach-Object {
  15.     # Create relative paths by trimming the root folder path
  16.     $_.FullName.Substring($folderPath.Length).TrimStart('\')
  17. }
  18.  
  19. # Use LiteralPath to include the files with relative paths in the ZIP
  20. if ($filePaths.Count -gt 0) {
  21.     # Create a temporary folder to recreate the structure
  22.     $tempFolder = Join-Path -Path $env:TEMP -ChildPath "TempZip"
  23.     Remove-Item -Recurse -Force -Path $tempFolder -ErrorAction SilentlyContinue
  24.     New-Item -ItemType Directory -Path $tempFolder | Out-Null
  25.  
  26.     # Copy the files into the temporary folder while preserving the relative structure
  27.     foreach ($item in $sourceItems) {
  28.         $relativePath = $item.FullName.Substring($folderPath.Length).TrimStart('\')
  29.         $destination = Join-Path -Path $tempFolder -ChildPath $relativePath
  30.         New-Item -ItemType Directory -Path (Split-Path -Path $destination -Parent) -Force | Out-Null
  31.         Copy-Item -Path $item.FullName -Destination $destination
  32.     }
  33.  
  34.     # Compress the recreated structure
  35.     Compress-Archive -Path $tempFolder\* -DestinationPath $destinationZip
  36.  
  37.     # Clean up the temporary folder
  38.     Remove-Item -Recurse -Force -Path $tempFolder
  39.     # Display summary of number and total (uncompressed) size of files
  40.     C:\Users\Administrator\Documents\AnalyzeZip.ps1 -zipPath $destinationZip
  41. } else {
  42.     Write-Host "No files found to add to the zip archive."
  43. }
  44.  
Tags: DayZ
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement