Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import-module PSFTP
- # Define time and date
- function DateTime([string]$argument)
- {
- switch ($argument)
- {
- time {$output=Get-Date -uformat "%T"}
- date {$output=Get-Date -uformat "%Y-%m-%d"}
- }
- return $output
- }
- function GetDateFromName([string]$argument)
- {
- $date = @(0,0,0,0)
- $Regex='([0-9]{2})\/([0-9]{2})\/([0-9]{4})'
- FOR ($a = 1; $a -lt 4; $a++)
- {
- $date[$a] = [regex]::match((Get-Childitem -path $argument).lastwritetime, $regex).Groups[$a].Value
- }
- $result = $($date[3] + "-" + $date[1] + "-" + $date[2])
- return $result
- }
- # Options
- $1cFolderEnabled = 1
- $SQLEnabled = 1
- $SelectelEnabled = 0
- $SelectelcFolderEnabled = 0
- $1cConfEnabled = 1
- $CleverFolderEnabled = 1
- $CleverBaseEnabled = 1
- $EcoEnabled = 1
- $HypervEnabled = 0
- $YandexSQLEnabled = 1
- $YandexVeeamEnabled = 1
- $MailEnabled = 1
- # 1C Folder
- $1cfDir = @(
- @{"Host" = "\\DC";"Path" = "D:\backup\1c";"Max" = 7},
- @{"Host" = "\\YUDIN";"Path" = "\\Yudin\D$\b\1c";"Max" = 7}
- )
- # SQL
- $SQLDir = @(
- @{"Host" = "\\DC";"Path" = "D:\backup\sql"; "YaTPath" = @("K:\Backup\SQL\1c_budget","K:\Backup\SQL\1c_hr","K:\Backup\SQL\1c_work"); "Max" = 20; "Filter" = @("1c_budget*.bak","1c_hr*.bak","1c_work*.bak")},
- @{"Host" = "\\YUDIN";"Path" = "\\Yudin\D$\b\sql"; "YaTPath" = @("K:\Backup\SQL\1c_budget","K:\Backup\SQL\1c_hr","K:\Backup\SQL\1c_work"); "Max" = 20; "Filter" = @("1c_budget*.bak","1c_hr*.bak","1c_work*.bak")}
- )
- # Selectel
- $Selectel = @(
- @{"Filter" = "1c_hr"; "Max" = 7},
- @{"Filter" = "1c_budget"; "Max" = 7},
- @{"Filter" = "1c_work"; "Max" = 7}
- )
- $SQL = @(
- @{"SrcPath" = "D:\backup\sql"; "Name" = "1c_budget"; "Max" = 7 },
- @{"SrcPath" = "D:\backup\sql"; "Name" = "1c_hr"; "Max" = 7 },
- @{"SrcPath" = "D:\backup\sql"; "Name" = "1c_work"; "Max" = 7 }
- )
- # 1C
- $1cConfDir = @(
- @{"Host" = "\\DC";"Path" = "D:\backup\1c_conf";"Max" = 30},
- @{"Host" = "\\YUDIN";"Path" = "\\Yudin\D$\b\1c_conf";"Max" = 30}
- )
- # Cleverence Folder
- $CleverFolderDir = @(
- @{"Host" = "\\DC";"Path" = "D:\backup\CleverenceFolder";"Max" = 15},
- @{"Host" = "\\YUDIN";"Path" = "\\yudin\D$\b\CleverenceFolder";"Max" = 15}
- )
- # Cleverence Folder
- $CleverBaseDir = @(
- @{"Host" = "\\DC";"Path" = "D:\backup\CleverenceBase";"Max" = 15},
- @{"Host" = "\\YUDIN";"Path" = "\\yudin\D$\b\CleverenceBase";"Max" = 15}
- )
- # Cleverence Folder
- $EcoDir = @(
- @{"Host" = "\\DC";"Path" = "D:\backup\Eco";"Max" = 10},
- @{"Host" = "\\YUDIN";"Path" = "\\yudin\D$\b\Eco";"Max" = 10}
- )
- # HyperV
- $HypervDir = @(
- @{"Host" = "\\DC";"Path" = "D:\backup\HyperV";"Max" = 3; "Daily" = 3; "VMs" = @("1c","dc1n","dc2n")}
- # @{"Host" = "\\YUDIN";"Path" = "\\yudin\D$\b\Eco";"Max" = 10}
- )
- #Yandex
- $YandexSQLDir = @(
- @{"YaTPath" = "K:\Backup\SQL\1c_budget"; "Name"= "1c_budget";"Filter" = "1c_budget*" },
- @{"YaTPath" = "K:\Backup\SQL\1c_hr"; "Name"= "1c_hr";"Filter" = "1c_hr*"},
- @{"YaTPath" = "K:\Backup\SQL\1c_work";"Name"= "1c_work";"Filter" = "1c_work*"}
- )
- $YandexVeeamDir = @(
- @{"YaTPath" = "K:\Backup\Veeam\1c_main"; "Name"= "1c_main FULL";"FULL?" = "1";"YaSPath"="\\yudin\D$\b\hyperv\veeam\1c";"Filter" = "1c_main*.vbk";"Filter2" = "1c_mainFULL*"},
- #@{"YaTPath" = "K:\Backup\Veeam\1c_main_DC"; "Name"= "1c_main FULL";"FULL?" = "1";"YaSPath"="\\yudin\D$\b\hyperv\veeam\1c_main_DC";"Filter" = "1c_main*.vbk";"Filter2" = "1c_mainFULL*"},
- @{"YaTPath" = "K:\Backup\Veeam\DC1n";"Name"= "DC1n FULL";"FULL?" = "1";"YaSPath"="\\yudin\D$\b\hyperv\veeam\DC1n"; "Filter"="DC1n*.vbk";"Filter2"="DC1n*.vbk"},
- @{"YaTPath" = "K:\Backup\Veeam\DC2n";"Name"= "DC2n FULL";"FULL?" = "1";"YaSPath"="\\yudin\D$\b\hyperv\veeam\DC2n";"Filter"="DC2n*.vbk";"Filter2"="DC2nFULL*"},
- @{"YaTPath" = "K:\Backup\Veeam\srv-aux";"Name"= "srv-aux FULL";"FULL?" = "1";"YaSPath"="\\yudin\D$\b\hyperv\veeam\srv-aux";"Filter"="srv-aux*.vbk";"Filter2"="srv-auxFULL*"},
- @{"YaTPath" = "K:\Backup\Veeam\windowsxp";"Name"= "windowsxp FULL";"FULL?" = "1";"YaSPath"="\\yudin\D$\b\hyperv\veeam\windowsxp";"Filter"="windowsxp*.vbk";"Filter2"="windowsxpFULL*"},
- @{"YaTPath" = "K:\Backup\Veeam\1c_main"; "Name"= "1c_main INC";"FULL?" = "0";"YaSPath"="\\yudin\D$\b\hyperv\veeam\1c";"Filter"="1c_main*.vib";"Filter2"="1c_maininc*"},
- #@{"YaTPath" = "K:\Backup\Veeam\1c_main_DC"; "Name"= "1c_main INC";"FULL?" = "0";"YaSPath"="\\yudin\D$\b\hyperv\veeam\1c_main_DC";"Filter"="1c_main*.vib";"Filter2"="1c_maininc*"},
- @{"YaTPath" = "K:\Backup\Veeam\DC1n";"Name"= "DC1n INC";"FULL?" = "0";"YaSPath"="\\yudin\D$\b\hyperv\veeam\DC1n";"Filter"="DC1n*.vib";"Filter2"="DC1n*.vib"},
- @{"YaTPath" = "K:\Backup\Veeam\DC2n";"Name"= "DC2n INC";"FULL?" = "0";"YaSPath"="\\yudin\D$\b\hyperv\veeam\DC2n";"Filter"="DC2n*.vib";"Filter2"="DC2n*.vib"},
- @{"YaTPath" = "K:\Backup\Veeam\srv-aux";"Name"= "srv-aux INC";"FULL?" = "0";"YaSPath"="\\yudin\D$\b\hyperv\veeam\srv-aux";"Filter"="srv-aux*.vib";"Filter2"="srv-aux*.vib"},
- @{"YaTPath" = "K:\Backup\Veeam\windowsxp";"Name"= "windowsxp INC";"FULL?" = "0";"YaSPath"="\\yudin\D$\b\hyperv\veeam\windowsxp";"Filter"="windowsxp*.vib";"Filter2"="windowsxp*.vib"}
- )
- # Logs
- $LogDir = "D:\backup\Log"
- $LogMax = 30
- $BackupReport = "D:\backup\log\$(DateTime("date"))\$(DateTime("date"))_BackupReport.html"
- # Mail
- $smtpServer = "smtp.yandex.ru"
- $MailFrom = "robot@opttriabc.ru"
- $mailto = "admin@opttriabc.ru"
- $MailPass = 'Sa$ShoTyPhi#'
- #FTP
- $ftp = "ftp://ftp.selcdn.ru"
- $user = "16225_b"
- $pass = ConvertTo-Securestring "M4QRUpKFXk" -AsPlainText -Force
- $Credentials = New-Object System.Management.Automation.PSCredential($user,$pass)
- # $Credentials = New-Object System.Net.NetworkCredential($user,$pass)
- # Executing
- # Greetings
- Write-Output "<head><meta http-equiv=""Content-Type"" content=""text/html; charset=OEM-866""/></head>Report all the evidence 3.0!<br>Today is $(DateTime("date")), $(DateTime("time")). <br> " > $BackupReport
- $BackupErrors = 0
- # Backup 1C Folder
- Write-Output "<br>------------------------ Backup 1C server folder ------------------------<br><br>" >> $BackupReport
- IF ($1cFolderEnabled -eq 1)
- {
- FOR ($a = 0; $a -lt $1cfDir.Count; $a++ )
- {
- $TCount = (Get-ChildItem -path $1cfDir[$a]["Path"] -filter "*.7z").Count
- $TLast = (Get-ChildItem -path $1cfDir[$a]["Path"] -filter "*.7z" | Select-Object -last 1 -expand Name) -replace ".7z",""
- $TSize = "{0:N2}" -f ((Get-ChildItem -path $1cfDir[$a]["Path"] -filter "*.7z" | Select-Object -last 1 | Measure-Object -property length -sum).sum / 1MB)
- Write-Output $1cfDir[$a]['Host']
- Write-Output $TCount
- Write-Output $TLast
- Write-Output $TSize
- Write-Output " "
- Write-Output "Host:<a style=""font-weight:bold;"">"$1cfDir[$a]['Host']"</a><br>" >> $BackupReport
- IF ($TLast -eq "$(DateTime("date"))")
- {
- Write-Output "Last one:<a style=""color:green;""> $TLast</a><br>" >> $BackupReport
- }
- ELSEIF ($TLast -lt "$(DateTime("date"))")
- {
- Write-Output "Last one:<a style=""color:green;""> $TLast</a><a style=""color:red;""> (OUTDATED!)</a><br>" >> $BackupReport
- $BackupErrors++
- }
- IF ($TSize -gt 1)
- {
- Write-Output "Size: $TSize MB<br>" >>$BackupReport
- }
- ELSE
- {
- Write-Output "Size: $TSize<a style=""font-family:arial;color:red;""> (Corrupted?)</a><br>" >> $BackupReport
- $BackupErrors++
- }
- FOR (;$TCount -gt $1cfDir[$a]['Max'];)
- {
- $TFirst = Get-ChildItem -path $1cfDir[$a]["Path"] -filter "*.7z" | Select-Object -first 1 -expand Fullname
- Remove-Item $TFirst -force
- $TCount = (Get-ChildItem -path $1cfDir[$a]["Path"] -filter "*.7z").Count
- }
- Write-Output "Count: $TCount of "$1cfDir[$a]['Max']"<br><br>" >> $BackupReport
- }
- }
- ELSE
- {
- Write-Output "-------------------------------- <a style=""font-family:arial;color:red;""> Disabled </a>-------------------------------<br>" >> $BackupReport
- }
- # SQL
- Write-Output "<br>------------------------------- Backup SQL ------------------------------<br><br>" >> $BackupReport
- IF ($SQLEnabled -eq 1)
- {
- FOR ($a = 0; $a -lt $SQLDir.Count; $a++ )
- {
- Write-Output "Host:<a style=""font-weight:bold;"">"$SQLDir[$a]['Host']"</a><br>" >> $BackupReport
- Write-Output $SQLDir[$a]['Host']
- FOR ($b = 0; $b -lt ($SQLDir[0]["Filter"]).Count; $b++ )
- {
- $TCount = (Get-ChildItem -path $SQLDir[$a]["Path"] -filter $SQLDir[$a]["Filter"][$b]).Count
- $TName = (Get-ChildItem -path $SQLDir[$a]["Path"] -filter $SQLDir[$a]["Filter"][$b] | Select-Object -last 1 -expand Name) -replace ".bak",""
- $TName -match "(?<date>[0-9]{4}_[0-9]{2}_[0-9]{2})"
- $TDate = $matches['date']
- $TDate = $TDate -replace "_","-"
- $TSize = "{0:N2}" -f ((Get-ChildItem -path $SQLDir[$a]["Path"] -filter $SQLDir[$a]["Filter"][$b] | Select-Object -last 1 | Measure-Object -property length -sum).sum / 1MB)
- #Write-Output $TCount
- #Write-Output $TName
- #Write-Output $TDate
- #Write-Output $TSize
- #Write-Output " "
- IF ($TDate -eq "$(DateTime("date"))")
- {
- Write-Output "Last one:<a style=""color:green;""> $TName</a><br>" >> $BackupReport
- }
- ELSEIF ($TDate -lt "$(DateTime("date"))")
- {
- Write-Output "Last one:<a style=""color:green;""> $TName</a><a style=""color:red;""> (OUTDATED!)</a><br>" >> $BackupReport
- $BackupErrors++
- }
- IF ($TSize -gt 1)
- {
- Write-Output "Size: $TSize MB<br>" >>$BackupReport
- }
- ELSE
- {
- Write-Output "Size: $TSize<a style=""font-family:arial;color:red;""> (Corrupted?)</a><br>" >> $BackupReport
- }
- #
- FOR (;$TCount -gt $SQLDir[$a]['Max'];)
- {
- $TFirst = Get-ChildItem -path $SQLDir[$a]["Path"] -filter $SQLDir[$a]["Filter"][$b] | Select-Object -first 1 -expand Fullname
- Remove-Item $TFirst -force
- $TCount = (Get-ChildItem -path $SQLDir[$a]["Path"] -filter $SQLDir[$a]["Filter"][$b]).Count
- }
- Write-Output "Count: $TCount of "$SQLDir[$a]['Max']"<br><br>" >> $BackupReport
- }
- }
- }
- ELSE
- {
- Write-Output "-------------------------------- <a style=""font-family:arial;color:red;""> Disabled </a>-------------------------------<br>" >> $BackupReport
- }
- # Selectel
- # Write-Output "<br>--------------------------- Upload to Selectel --------------------------<br><br>" >> $BackupReport
- IF ($SelectelEnabled -eq 1)
- {
- FOR ($a = 0; $a -lt $Selectel.Count; $a++)
- {
- Write-Output "File:<a style=""font-weight:bold;"">"$Selectel[$a]['Filter']"</a><br>" >> $BackupReport
- $TList = & "swift" -A https://auth.selcdn.ru -U "16225_b" -K "M4QRUpKFXk" list "triabc" 2>&1 | Out-String
- IF ($TList -like "*Auth GET failed: https://auth.selcdn.ru 403 Forbidden*")
- {
- Write-Output "<a style=""color:red;""> ERROR: Auth GET failed: https://auth.selcdn.ru 403 Forbidden</a><br>" >> $BackupReport
- $BackupErrors++
- }
- ELSE
- {
- $TList = & "swift" -A https://auth.selcdn.ru -U "16225_b" -K "M4QRUpKFXk" list "triabc" | Select-String -pattern $Selectel[$a]["Filter"] | Sort-Object -Descending
- Write-Output $Tlist
- $TName = $TList[0]
- $TName -match "(?<date>[0-9]{4}_[0-9]{2}_[0-9]{2})"
- $TDate = $matches['date']
- $TDate = $TDate -replace "_","-"
- $TCount = $TList.Count
- Write-Output $TList
- Write-Output $TDate
- Write-Output $TCount
- IF ($TDate -eq "$(DateTime("date"))")
- {
- Write-Output "Last one:<a style=""color:green;""> $TDate</a><br>" >> $BackupReport
- }
- ELSEIF ($TDate -lt "$(DateTime("date"))")
- {
- Write-Output "Last one:<a style=""color:green;""> $TDate</a><a style=""color:red;""> (OUTDATED!)</a><br>" >> $BackupReport
- $BackupErrors++
- }
- Write-Output "Count: $TCount of "$Selectel[$a]['Max']"<br><br>" >> $BackupReport
- }
- }
- Write-Output "<br>------------------------ Upload to Selectel - FTP -----------------------<br><br>" >> $BackupReport
- $TryNumber = 0
- while($TryNumber -lt 10)
- {
- try{
- write-host " "
- write-host "Connecting to"$ftp"..."
- # Set-FTPConnection -Credentials $Credentials -Server $ftp -UseBinary -UsePassive
- Set-FTPConnection -Credentials $Credentials -Server $ftp -Session Session -UseBinary -UsePassive
- write-host "Connected!"
- $Connected = 1
- $TryNumber = 10
- }
- catch [Exception] {
- write-host "Connection error"
- write-host $_.Exception.Message;
- write-host "Retry in 60 seconds..."
- $TryNumber = $TryNumber++
- Start-Sleep -sec 60
- }
- }
- if ($Connected -eq 1)
- {
- FOR ($a = 0; $a -lt $SQL.Count; $a++)
- {
- $Filter = $SQL[$a]['Name']+"*.bak"
- $SrcName = (Get-ChildItem -path $SQL[$a]['SrcPath'] -filter $Filter | Select -expand Name -last 1) -replace ".bak",""
- $SrcFullName = $SQL[$a]['SrcPath']+"\"+$SrcName+".bak"
- $SrcCount = (Get-ChildItem -path $SQL[$a]['SrcPath'] -filter $Filter).count
- $SrcAvgSize = "{0:N2}" -f ((Get-ChildItem -path $SQL[$a]['SrcPath'] -force -filter $Filter | Select-Object -skip 1 | Measure-Object -property length -sum).sum / ($SrcCount - 1) / 1GB * 0.95)
- $SrcDate = GetDateFromName($SrcFullName)
- $Ftpfilter = $SQL[$a]['Name']+"*"
- $FtpPath = "ftp://ftp.selcdn.ru/triabc"
- $FtpLastItem = Get-FTPChildItem -Path $FtpPath -filter $Ftpfilter | Select-Object -last 1
- $FtpReplace = $SQL[$a]['Name']+"_"
- $FtpDate = ($FtpLastItem.Name) -replace "$FtpReplace",""
- $FtpFullPath = $FtpPath+"/"+$FtpLastItem.Name
- $FtpFile = Get-FTPChildItem -Path $FtpFullPath -Recurse
- $FtpSize = "{0:N2}" -f (($ftpFile | Measure-Object -property SizeInByte -sum).sum / 1GB)
- $FtpCount = (Get-FTPChildItem -Path $FtpPath -filter $Ftpfilter).count
- $SrcSize = "{0:N2}" -f ((Get-ChildItem -path $SQL[$a]['SrcPath'] -force -filter $Filter | Select-Object -last 1 | Measure-Object -property length -sum).sum / 1GB)
- Write-Output "DB name: <a style=""font-weight:bold;""> "$SQL[$a]['Name']" </a><br>" >> $BackupReport
- Write-Output "Number of backups: Local - $SrcCount; Cloud - $FtpCount <br>" >> $BackupReport
- if ($SrcSize -gt $SrcAvgSize)
- {
- if ($SrcDate -eq $(DateTime("date")))
- {
- Write-Output "Last local item: <a style=""color:green;""> $SrcDate - $SrcSize"GB" </a> <br>" >> $BackupReport
- if ($FtpSize -eq $SrcSize -and $FtpDate -eq $SrcDate)
- {
- Write-Output "Last cloud item:<a style=""color:green;""> $FtpDate - $FtpSize"GB" </a> <br><br>" >> $BackupReport
- }
- else
- {
- Write-Output "Last cloud item:<a style=""color:red;""> $FtpDate - $FtpSize"GB" - (DON'T MATCHES!) </a> <br><br>" >> $BackupReport
- }
- }
- else
- {
- Write-Output "Last local item:<a style=""color:red;""> $SrcDate - $SrcSize"GB" - (OUTDATED!) </a> <br>" >> $BackupReport
- Write-Output "Last cloud item:<a style=""color:red;""> $FtpDate - $FtpSize"GB" - (CHECK SOURCE!) </a> <br><br>" >> $BackupReport
- Write-Output "Debug FtpLastItem $FtpLastItem FtpFullPath $FtpFullPath FtpCount $FtpCount <br><br>" >> $BackupReport
- }
- }
- else
- {
- Write-Output "Last local item:<a style=""color:red;""> $SrcDate - $SrcSize"GB" - (CORRUPTED!) </a> <br>" >> $BackupReport
- Write-Output "Last cloud item:<a style=""color:red;""> $FtpDate - $FtpSize"GB" - (CHECK SOURCE!) </a> <br><br>" >> $BackupReport
- }
- }
- }
- else
- {
- Write-Output "FTP connection error!" >> $BackupReport
- }
- }
- ELSE
- {
- Write-Output "-------------------------------- <a style=""font-family:arial;color:red;""> Disabled </a>-------------------------------<br>" >> $BackupReport
- }
- Write-Output "<br>----------------------------- Backup 1C Conf ----------------------------<br><br>" >> $BackupReport
- IF ($1cConfEnabled -eq 1)
- {
- FOR ($a = 0; $a -lt $1cConfDir.Count; $a++ )
- {
- $TCount = (Get-ChildItem -path $1cConfDir[$a]["Path"] -filter "*.cf").Count
- $TLast = (Get-ChildItem -path $1cConfDir[$a]["Path"] -filter "*.cf" | Select-Object -last 1 -expand Name) -replace ".cf",""
- $TSize = "{0:N2}" -f ((Get-ChildItem -path $1cConfDir[$a]["Path"] -filter "*.cf" | Select-Object -last 1 | Measure-Object -property length -sum).sum / 1MB)
- Write-Output $1cConfDir[$a]['Host']
- Write-Output $TCount
- Write-Output $TLast
- Write-Output $TSize
- Write-Output " "
- Write-Output "Host:<a style=""font-weight:bold;"">"$1cConfDir[$a]['Host']"</a><br>" >> $BackupReport
- IF ($TLast -eq "$(DateTime("date"))")
- {
- Write-Output "Last one:<a style=""color:green;""> $TLast</a><br>" >> $BackupReport
- }
- ELSEIF ($TLast -lt "$(DateTime("date"))")
- {
- Write-Output "Last one:<a style=""color:green;""> $TLast</a><a style=""color:red;""> (OUTDATED!)</a><br>" >> $BackupReport
- $BackupErrors++
- }
- IF ($TSize -gt 1)
- {
- Write-Output "Size: $TSize MB<br>" >>$BackupReport
- }
- ELSE
- {
- Write-Output "Size: $TSize<a style=""font-family:arial;color:red;""> (Corrupted?)</a><br>" >> $BackupReport
- # $TCount--
- $BackupErrors++
- }
- FOR (;$TCount -gt $1cConfDir[$a]['Max'];)
- {
- $TFirst = Get-ChildItem -path $1cConfDir[$a]["Path"] -filter "*.cf" | Select-Object -first 1 -expand Fullname
- Remove-Item $TFirst -force
- $TCount = (Get-ChildItem -path $1cConfDir[$a]["Path"] -filter "*.cf").Count
- }
- Write-Output "Count: $TCount of "$1cConfDir[$a]['Max']"<br><br>" >> $BackupReport
- }
- }
- ELSE
- {
- Write-Output "-------------------------------- <a style=""font-family:arial;color:red;""> Disabled </a>-------------------------------<br>" >> $BackupReport
- }
- # Backup Cleverence Folder
- Write-Output "<br>------------------------ Backup Cleverence folder -----------------------<br><br>" >> $BackupReport
- IF ($CleverFolderEnabled -eq 1)
- {
- FOR ($a = 0; $a -lt $CleverFolderDir.Count; $a++ )
- {
- $TCount = (Get-ChildItem -path $CleverFolderDir[$a]["Path"] -filter "*.7z").Count
- $TLast = (Get-ChildItem -path $CleverFolderDir[$a]["Path"] -filter "*.7z" | Select-Object -last 1 -expand Name) -replace ".7z",""
- $TSize = "{0:N2}" -f ((Get-ChildItem -path $CleverFolderDir[$a]["Path"] -filter "*.7z" | Select-Object -last 1 | Measure-Object -property length -sum).sum / 1MB)
- Write-Output $CleverFolderDir[$a]['Host']
- Write-Output $TCount
- Write-Output $TLast
- Write-Output $TSize
- Write-Output " "
- Write-Output "Host:<a style=""font-weight:bold;"">"$CleverFolderDir[$a]['Host']"</a><br>" >> $BackupReport
- IF ($TLast -eq "$(DateTime("date"))")
- {
- Write-Output "Last one:<a style=""color:green;""> $TLast</a><br>" >> $BackupReport
- }
- ELSEIF ($TLast -lt "$(DateTime("date"))")
- {
- Write-Output "Last one:<a style=""color:green;""> $TLast</a><a style=""color:red;""> (OUTDATED!)</a><br>" >> $BackupReport
- $BackupErrors++
- }
- IF ($TSize -gt 1)
- {
- Write-Output "Size: $TSize MB<br>" >>$BackupReport
- }
- ELSE
- {
- Write-Output "Size: $TSize<a style=""font-family:arial;color:red;""> (Corrupted?)</a><br>" >> $BackupReport
- # $TCount--
- $BackupErrors++
- }
- FOR (;$TCount -gt $CleverFolderDir[$a]['Max'];)
- {
- $TFirst = Get-ChildItem -path $CleverFolderDir[$a]["Path"] -filter "*.7z" | Select-Object -first 1 -expand Fullname
- Remove-Item $TFirst -force
- $TCount = (Get-ChildItem -path $CleverFolderDir[$a]["Path"] -filter "*.7z").Count
- }
- Write-Output "Count: $TCount of "$CleverFolderDir[$a]['Max']"<br><br>" >> $BackupReport
- }
- }
- ELSE
- {
- Write-Output "-------------------------------- <a style=""font-family:arial;color:red;""> Disabled </a>-------------------------------<br>" >> $BackupReport
- }
- # Backup Cleverence Base
- Write-Output "<br>------------------------- Backup Cleverence base ------------------------<br><br>" >> $BackupReport
- IF ($CleverBaseEnabled -eq 1)
- {
- FOR ($a = 0; $a -lt $CleverBaseDir.Count; $a++ )
- {
- $TCount = (Get-ChildItem -path $CleverBaseDir[$a]["Path"] -filter "*.7z").Count
- $TLast = (Get-ChildItem -path $CleverBaseDir[$a]["Path"] -filter "*.7z" | Select-Object -last 1 -expand Name) -replace ".7z",""
- $TSize = "{0:N2}" -f ((Get-ChildItem -path $CleverBaseDir[$a]["Path"] -filter "*.7z" | Select-Object -last 1 | Measure-Object -property length -sum).sum / 1MB)
- Write-Output $CleverBaseDir[$a]['Host']
- Write-Output $TCount
- Write-Output $TLast
- Write-Output $TSize
- Write-Output " "
- Write-Output "Host:<a style=""font-weight:bold;"">"$CleverBaseDir[$a]['Host']"</a><br>" >> $BackupReport
- IF ($TLast -eq "$(DateTime("date"))")
- {
- Write-Output "Last one:<a style=""color:green;""> $TLast</a><br>" >> $BackupReport
- }
- ELSEIF ($TLast -lt "$(DateTime("date"))")
- {
- Write-Output "Last one:<a style=""color:green;""> $TLast</a><a style=""color:red;""> (OUTDATED!)</a><br>" >> $BackupReport
- $BackupErrors++
- }
- IF ($TSize -gt 1)
- {
- Write-Output "Size: $TSize MB<br>" >>$BackupReport
- }
- ELSE
- {
- Write-Output "Size: $TSize<a style=""font-family:arial;color:red;""> (Corrupted?)</a><br>" >> $BackupReport
- # $TCount--
- $BackupErrors++
- }
- FOR (;$TCount -gt $CleverBaseDir[$a]['Max'];)
- {
- $TFirst = Get-ChildItem -path $CleverBaseDir[$a]["Path"] -filter "*.7z" | Select-Object -first 1 -expand Fullname
- Remove-Item $TFirst -force
- $TCount = (Get-ChildItem -path $CleverBaseDir[$a]["Path"] -filter "*.7z").Count
- }
- Write-Output "Count: $TCount of "$CleverBaseDir[$a]['Max']"<br><br>" >> $BackupReport
- }
- }
- ELSE
- {
- Write-Output "-------------------------------- <a style=""font-family:arial;color:red;""> Disabled </a>-------------------------------<br>" >> $BackupReport
- }
- # Backup Cleverence Folder
- Write-Output "<br>---------------------------- Backup Eco folder --------------------------<br><br>" >> $BackupReport
- IF ($EcoEnabled -eq 1)
- {
- FOR ($a = 0; $a -lt $EcoDir.Count; $a++ )
- {
- $TCount = (Get-ChildItem -path $EcoDir[$a]["Path"] -filter "*.7z").Count
- $TLast = (Get-ChildItem -path $EcoDir[$a]["Path"] -filter "*.7z" | Select-Object -last 1 -expand Name) -replace ".7z",""
- $TSize = "{0:N2}" -f ((Get-ChildItem -path $EcoDir[$a]["Path"] -filter "*.7z" | Select-Object -last 1 | Measure-Object -property length -sum).sum / 1MB)
- Write-Output $EcoDir[$a]['Host']
- Write-Output $TCount
- Write-Output $TLast
- Write-Output $TSize
- Write-Output " "
- Write-Output "Host:<a style=""font-weight:bold;"">"$EcoDir[$a]['Host']"</a><br>" >> $BackupReport
- IF ($TLast -eq "$(DateTime("date"))")
- {
- Write-Output "Last one:<a style=""color:green;""> $TLast</a><br>" >> $BackupReport
- }
- ELSEIF ($TLast -lt "$(DateTime("date"))")
- {
- Write-Output "Last one:<a style=""color:green;""> $TLast</a><a style=""color:red;""> (OUTDATED!)</a><br>" >> $BackupReport
- $BackupErrors++
- }
- IF ($TSize -gt 1)
- {
- Write-Output "Size: $TSize MB<br>" >>$BackupReport
- }
- ELSE
- {
- Write-Output "Size: $TSize<a style=""font-family:arial;color:red;""> (Corrupted?)</a><br>" >> $BackupReport
- # $TCount--
- $BackupErrors++
- }
- FOR (;$TCount -gt $EcoDir[$a]['Max'];)
- {
- $TFirst = Get-ChildItem -path $EcoDir[$a]["Path"] -filter "*.7z" | Select-Object -first 1 -expand Fullname
- Remove-Item $TFirst -force
- $TCount = (Get-ChildItem -path $EcoDir[$a]["Path"] -filter "*.7z").Count
- }
- Write-Output "Count: $TCount of "$EcoDir[$a]['Max']"<br><br>" >> $BackupReport
- }
- }
- ELSE
- {
- Write-Output "-------------------------------- <a style=""font-family:arial;color:red;""> Disabled </a>-------------------------------<br>" >> $BackupReport
- }
- # HyperV
- Write-Output "<br>----------------------------- Backup Hyper-V ----------------------------<br><br>" >> $BackupReport
- IF ($HypervEnabled -eq 1)
- {
- FOR ($a = 0; $a -lt $HypervDir.Count; $a++ )
- {
- Write-Output "Host:<a style=""font-weight:bold;"">"$HypervDir[$a]['Host']"</a><br>" >> $BackupReport
- Write-Output $HypervDir[$a]['Host']
- FOR ($b = 0; $b -lt ($HypervDir[0]["VMs"]).Count; $b++ )
- {
- $filter = $HypervDir[$a]["VMs"][$b]+"*"
- $list = Get-ChildItem $HypervDir[$a]["Path"] -Directory -Filter $filter | where {$_.LastWriteTime -lt (Get-Date).AddDays(-$HypervDir[$a]["Daily"])}
- cd $HypervDir[$a]["Path"]
- foreach ($item in $list)
- {
- $day = ($item.LastWriteTime).DayOfWeek
- if ($day -ne "Monday")
- {
- Remove-Item -path $item -recurse -force
- }
- }
- $TCount = (Get-ChildItem -directory -path $HypervDir[$a]["Path"] -filter $filter).Count
- $TFullpath = Get-ChildItem -directory -path $HypervDir[$a]["Path"] -filter $filter | Select-Object -last 1 -expand Fullname
- $TName = (Get-ChildItem -directory -path $HypervDir[$a]["Path"] -filter $filter | Select-Object -last 1 -expand Name)
- $TName -match "_(?<date>[0-9]{4}[0-9]{2}[0-9]{2})"
- $TDate = GetDateFromName($TFullpath)
- $TSize = "{0:N2}" -f ((Get-ChildItem -path $TFullpath -force -Recurse | Measure-Object -property length -sum).sum / 1MB)
- Write-host $TCount
- Write-host $TName
- Write-host $TDate
- Write-host $(DateTime("date"))
- Write-host $TSize
- Write-host $TFullpath
- Write-host "========================"
- IF ($TDate -eq "$(DateTime("date"))")
- {
- Write-Output "Last one:<a style=""color:green;""> $TName</a><br>" >> $BackupReport
- }
- ELSEIF ($TDate -lt "$(DateTime("date"))")
- {
- Write-Output "Last one:<a style=""color:green;""> $TName</a><a style=""color:red;""> (OUTDATED!)</a><br>" >> $BackupReport
- $BackupErrors++
- }
- IF ($TSize -gt 1)
- {
- Write-Output "Size: $TSize MB<br>" >>$BackupReport
- }
- ELSE
- {
- Write-Output "Size: $TSize<a style=""font-family:arial;color:red;""> (Corrupted?)</a><br>" >> $BackupReport
- # $TCount--
- $BackupErrors++
- }
- FOR (;$TCount -gt $HypervDir[$a]['Max'];)
- {
- $TFirst = Get-ChildItem -directory -path $HypervDir[$a]["Path"] -filter $filter | Select-Object -first 1 -expand Fullname
- Remove-Item $TFirst -force -recurse
- write-host $TCount
- $TCount = (Get-ChildItem -directory -path $HypervDir[$a]["Path"] -filter $filter).Count
- }
- Write-Output "Count: $TCount of "$HypervDir[$a]['Max']"<br><br>" >> $BackupReport
- }
- }
- }
- ELSE
- {
- Write-Output "-------------------------------- <a style=""font-family:arial;color:red;""> Disabled </a>-------------------------------<br>" >> $BackupReport
- }
- <#
- Write-Output "<br>-------------------------- Storage stats on \\DC ----------------------<br>" >> $BackupReport
- Get-DiskFreeSpace >> $BackupReport
- #>
- # End of Report
- Write-Output "<br>-------------------------- End of report at $(DateTime("time")). ----------------------<br>" >> $BackupReport
- Write-Output "------------------------------- Have a nice day. ----------------------------<br>" >> $BackupReport
- # E-Mail and stuff
- IF ($MailEnabled -eq 1)
- {
- [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$True}
- IF ($BackupErrors -eq 0)
- {
- $msg = new-object Net.Mail.MailMessage
- $smtp = new-object Net.Mail.SmtpClient($smtpServer)
- $msg.From = $MailFrom
- $msg.IsBodyHTML = $true
- $msg.To.Add($Mailto)
- $msg.Subject = "Backup report (success)"
- $MailTextT = Get-Content -Path $BackupReport
- $msg.Body = $MailTextT
- $smtp.EnableSsl = $true
- $smtp.Credentials = New-Object System.Net.NetworkCredential($MailFrom, $MailPass);
- $smtp.Send($msg)
- }
- ELSE
- {
- $msg = new-object Net.Mail.MailMessage
- $smtp = new-object Net.Mail.SmtpClient($smtpServer)
- $msg.From = $MailFrom
- $msg.IsBodyHTML = $true
- $msg.To.Add($Mailto)
- $msg.Subject = "BACKUP ERROR! ($BackupErrors error(s) total)"
- $MailTextT = Get-Content -Path $BackupReport | Out-String
- $msg.Body = $MailTextT
- $smtp.EnableSsl = $true
- $smtp.Credentials = New-Object System.Net.NetworkCredential($MailFrom, $MailPass);
- $smtp.Send($msg)
- }
- }
- # чтобы окно не закрылось
- # $host.ui.RawUI.ReadKey(6)|out-null
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement