Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # PowerShell Script to Backup and Delete older folder
- ########
- # Fonction to write on the Powershell prompt and in the file logs
- $PathLog="A:\Logs\Save\Log.txt"
- function logMsg($msg)
- {
- write-Host $msg
- Add-Content $PathLog $msg
- }
- ########
- # Variable path
- $PathBDD = "A:\AtlasServer\AtlasTools\RedisDatabase\redis_atlasdb.rdb"
- $pathSaved = "A:\AtlasServer\ShooterGame\Saved"
- $PathDest = "S:"
- $PathDestFormat = "S:\Saved_$(Get-Date -format "yyyy-MM-dd_HH-mm-ss")"
- ########
- # Get the directory content and copy it
- Copy-Item -Path $pathSaved -Destination $PathDestFormat -Recurse
- logMsg "------------------------------------------------------------------------------------------"
- logMsg "$(Get-Date -format "yyyy-MM-dd_HH-mm-ss") - Copy of Saved"
- # Copy the Redis database file
- Copy-Item -Path $PathBDD -Destination $PathDestFormat
- logMsg "$(Get-Date -format "yyyy-MM-dd_HH-mm-ss") - Copy of BDD"
- logMsg "$(Get-Date -format "yyyy-MM-dd_HH-mm-ss") - PATH - $($PathDestFormat)"
- ########
- # Delete the old saves
- # "$n" The number of save to keep "$n"
- $n = 50
- $items = Get-ChildItem $PathDest "Saved_*"
- Set-Location $PathDest
- try {
- $items |
- Sort-Object LastWriteTime -Descending |
- Select-Object -Last ($items.count - $n) |
- Foreach-Object {
- logMsg "$((Get-Date -format "yyyy-MM-dd HH-mm-ss")) - Delete folder Named : $($_)";
- Remove-Item $_ -Recurse
- }
- }
- catch {Write-Warning "There is not enough backup to delete"}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement