Answer the question
In order to leave comments, you need to log in
Need help with Powershell?
Good afternoon!
Please help me write the script.
We make backups of 2 databases every day, throw them into directories with the structure <year>\<month>\<day>
We need to clean up the backups for the previous month, leave the first from the beginning of the month and the next +15 days.
At the same time, you need to check that there are both backups in the directory (everything happens) I
wrote a Powershell script, but there is a problem:
$year = (Get-Date).Year
#$month = get-date ((get-date).AddMonths(-1)) -format MM
$month = "07"
$temp= "$env:temp" + "\backup\"
$dir="\\files\backup$\8.1\$year\$month"
#
# Создаём временный каталог
#
If (!(Test-Path $temp)){
New-Item $temp -type directory
}
#
# Сбрасываем счётчики
#
$trg=0
$ceh=0
Get-ChildItem -Path $dir| Sort-Object LastAccessTime | Select-Object -First 15 | ForEach-Object {
write-host $_
if ((Test-Path $dir\$_\trg*.dt) -And ($trg -eq 0)){
Copy-Item -recurse -Force $dir\$_\trg* $temp
$trg=1
}
if ((Test-Path $dir\$_\ceh*.dt) -And ($ceh -eq 0)){
#Write-Host $ceh
Copy-Item -recurse -Force $dir\$_\ceh* $temp
$ceh=1
}
if (($trg -eq 1) -And ($ceh -eq 1)){
write-host "All backups are found"
continue
}
}
# Сбрасываем счётчики и ищем второй бэкап
#
$trg=0
$ceh=0
Get-ChildItem -Path $dir| Sort-Object LastAccessTime | Select-Object -Last 12 | ForEach-Object {
if ((Test-Path $dir\$_\trg*.dt) -And ($trg -eq 0)){
Copy-Item -recurse -Force $dir\$_\trg* $temp
$trg=1
}
if ((Test-Path $dir\$_\ceh*.dt) -And ($ceh -eq 0)){
#Write-Host $ceh
Copy-Item -recurse -Force $dir\$_\ceh* $temp
$ceh=1
}
if (($trg -eq 1) -And ($ceh -eq 1)){
write-host "All backups are found"
continue
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question