Advertisement
Guest User

Saved.ps1

a guest
Jan 26th, 2019
619
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # PowerShell Script to Backup and Delete older folder
  2.  
  3. ########
  4.  
  5. # Fonction to write on the Powershell prompt and in the file logs
  6. $PathLog="A:\Logs\Save\Log.txt"
  7.  
  8. function logMsg($msg)
  9. {
  10.     write-Host $msg
  11.     Add-Content $PathLog $msg
  12. }
  13.  
  14. ########
  15.  
  16. # Variable path
  17.  
  18. $PathBDD = "A:\AtlasServer\AtlasTools\RedisDatabase\redis_atlasdb.rdb"
  19. $pathSaved = "A:\AtlasServer\ShooterGame\Saved"
  20.  
  21. $PathDest = "S:"
  22. $PathDestFormat = "S:\Saved_$(Get-Date -format "yyyy-MM-dd_HH-mm-ss")"
  23.  
  24. ########
  25.  
  26. # Get the directory content and copy it
  27. Copy-Item -Path $pathSaved -Destination $PathDestFormat -Recurse
  28.  
  29. logMsg "------------------------------------------------------------------------------------------"
  30. logMsg "$(Get-Date -format "yyyy-MM-dd_HH-mm-ss") - Copy of Saved"
  31.  
  32.  
  33. # Copy the Redis database file
  34.  
  35. Copy-Item -Path $PathBDD -Destination $PathDestFormat
  36.  
  37. logMsg "$(Get-Date -format "yyyy-MM-dd_HH-mm-ss") - Copy of BDD"
  38. logMsg "$(Get-Date -format "yyyy-MM-dd_HH-mm-ss") - PATH - $($PathDestFormat)"
  39.  
  40.  
  41. ########
  42.  
  43. # Delete the old saves
  44.  
  45. # "$n" The number of save to keep "$n"
  46.  
  47. $n = 50
  48.  
  49. $items = Get-ChildItem $PathDest "Saved_*"
  50.  
  51. Set-Location  $PathDest
  52.  
  53. try {
  54.  
  55.     $items |
  56.         Sort-Object LastWriteTime -Descending |
  57.         Select-Object -Last ($items.count - $n) |
  58.         Foreach-Object {
  59.             logMsg "$((Get-Date -format "yyyy-MM-dd HH-mm-ss")) - Delete folder Named : $($_)";
  60.             Remove-Item $_ -Recurse
  61.         }
  62.     }
  63.     catch {Write-Warning "There is not enough backup to delete"}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement