Advertisement
Guest User

Untitled

a guest
Feb 27th, 2018
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import-module PSFTP
  2.  
  3.  
  4. # Define time and date
  5. function DateTime([string]$argument)
  6. {
  7.     switch ($argument)
  8.     {
  9.         time {$output=Get-Date -uformat "%T"}
  10.         date {$output=Get-Date -uformat "%Y-%m-%d"}
  11.     }
  12.     return $output
  13. }
  14.  
  15. function GetDateFromName([string]$argument)
  16. {
  17.     $date = @(0,0,0,0)
  18.     $Regex='([0-9]{2})\/([0-9]{2})\/([0-9]{4})'
  19.     FOR ($a = 1; $a -lt 4; $a++)
  20.     {
  21.         $date[$a] = [regex]::match((Get-Childitem -path $argument).lastwritetime, $regex).Groups[$a].Value     
  22.     }
  23.     $result = $($date[3] + "-" + $date[1] + "-" + $date[2])
  24.     return $result
  25. }
  26.  
  27.  
  28. # Options
  29.     $1cFolderEnabled = 1
  30.     $SQLEnabled = 1
  31.     $SelectelEnabled = 0
  32.     $SelectelcFolderEnabled = 0
  33.     $1cConfEnabled = 1
  34.     $CleverFolderEnabled = 1
  35.     $CleverBaseEnabled = 1
  36.     $EcoEnabled = 1
  37.     $HypervEnabled = 0
  38.     $YandexSQLEnabled = 1
  39.     $YandexVeeamEnabled = 1
  40.    
  41.     $MailEnabled = 1
  42.    
  43.     # 1C Folder
  44.     $1cfDir = @(
  45.                 @{"Host" = "\\DC";"Path" = "D:\backup\1c";"Max" = 7},
  46.                 @{"Host" = "\\YUDIN";"Path" = "\\Yudin\D$\b\1c";"Max" = 7}
  47.                 )
  48.     # SQL
  49.     $SQLDir = @(
  50.                 @{"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")},
  51.                 @{"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")}
  52.                 )
  53.        
  54.     # Selectel
  55.     $Selectel = @(
  56.                   @{"Filter" = "1c_hr"; "Max" = 7},
  57.                   @{"Filter" = "1c_budget"; "Max" = 7},
  58.                   @{"Filter" = "1c_work"; "Max" = 7}
  59.                 )
  60.                
  61.     $SQL = @(
  62.             @{"SrcPath" = "D:\backup\sql"; "Name" = "1c_budget"; "Max" = 7 },
  63.             @{"SrcPath" = "D:\backup\sql"; "Name" = "1c_hr"; "Max" = 7 },
  64.             @{"SrcPath" = "D:\backup\sql"; "Name" = "1c_work"; "Max" = 7 }
  65.         )
  66.        
  67.     # 1C
  68.     $1cConfDir = @(
  69.                 @{"Host" = "\\DC";"Path" = "D:\backup\1c_conf";"Max" = 30},
  70.                 @{"Host" = "\\YUDIN";"Path" = "\\Yudin\D$\b\1c_conf";"Max" = 30}
  71.                 )
  72.     # Cleverence Folder
  73.     $CleverFolderDir = @(
  74.                 @{"Host" = "\\DC";"Path" = "D:\backup\CleverenceFolder";"Max" = 15},
  75.                 @{"Host" = "\\YUDIN";"Path" = "\\yudin\D$\b\CleverenceFolder";"Max" = 15}
  76.                 )
  77.     # Cleverence Folder
  78.     $CleverBaseDir = @(
  79.                 @{"Host" = "\\DC";"Path" = "D:\backup\CleverenceBase";"Max" = 15},
  80.                 @{"Host" = "\\YUDIN";"Path" = "\\yudin\D$\b\CleverenceBase";"Max" = 15}
  81.                 )
  82.     # Cleverence Folder
  83.     $EcoDir = @(
  84.                 @{"Host" = "\\DC";"Path" = "D:\backup\Eco";"Max" = 10},
  85.                 @{"Host" = "\\YUDIN";"Path" = "\\yudin\D$\b\Eco";"Max" = 10}
  86.                 )
  87.     # HyperV
  88.     $HypervDir = @(
  89.                 @{"Host" = "\\DC";"Path" = "D:\backup\HyperV";"Max" = 3; "Daily" = 3; "VMs" = @("1c","dc1n","dc2n")}
  90. #               @{"Host" = "\\YUDIN";"Path" = "\\yudin\D$\b\Eco";"Max" = 10}
  91.                 )
  92.  
  93.  #Yandex
  94.   $YandexSQLDir = @(
  95.                 @{"YaTPath" = "K:\Backup\SQL\1c_budget"; "Name"= "1c_budget";"Filter" = "1c_budget*" },
  96.                 @{"YaTPath" = "K:\Backup\SQL\1c_hr"; "Name"= "1c_hr";"Filter" = "1c_hr*"},
  97.                 @{"YaTPath" = "K:\Backup\SQL\1c_work";"Name"= "1c_work";"Filter" = "1c_work*"}
  98.                
  99.                 )
  100.  
  101. $YandexVeeamDir = @(
  102.                 @{"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*"},
  103.                 #@{"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*"},
  104.                 @{"YaTPath" = "K:\Backup\Veeam\DC1n";"Name"= "DC1n FULL";"FULL?" = "1";"YaSPath"="\\yudin\D$\b\hyperv\veeam\DC1n"; "Filter"="DC1n*.vbk";"Filter2"="DC1n*.vbk"},
  105.                 @{"YaTPath" = "K:\Backup\Veeam\DC2n";"Name"= "DC2n FULL";"FULL?" = "1";"YaSPath"="\\yudin\D$\b\hyperv\veeam\DC2n";"Filter"="DC2n*.vbk";"Filter2"="DC2nFULL*"},
  106.                 @{"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*"},
  107.                 @{"YaTPath" = "K:\Backup\Veeam\windowsxp";"Name"= "windowsxp FULL";"FULL?" = "1";"YaSPath"="\\yudin\D$\b\hyperv\veeam\windowsxp";"Filter"="windowsxp*.vbk";"Filter2"="windowsxpFULL*"},
  108.                 @{"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*"},
  109.                 #@{"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*"},
  110.                 @{"YaTPath" = "K:\Backup\Veeam\DC1n";"Name"= "DC1n INC";"FULL?" = "0";"YaSPath"="\\yudin\D$\b\hyperv\veeam\DC1n";"Filter"="DC1n*.vib";"Filter2"="DC1n*.vib"},
  111.                 @{"YaTPath" = "K:\Backup\Veeam\DC2n";"Name"= "DC2n INC";"FULL?" = "0";"YaSPath"="\\yudin\D$\b\hyperv\veeam\DC2n";"Filter"="DC2n*.vib";"Filter2"="DC2n*.vib"},
  112.                 @{"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"},
  113.                 @{"YaTPath" = "K:\Backup\Veeam\windowsxp";"Name"= "windowsxp INC";"FULL?" = "0";"YaSPath"="\\yudin\D$\b\hyperv\veeam\windowsxp";"Filter"="windowsxp*.vib";"Filter2"="windowsxp*.vib"}
  114.                 )
  115.  
  116.  
  117.  
  118.     # Logs
  119.     $LogDir = "D:\backup\Log"
  120.     $LogMax = 30
  121.     $BackupReport = "D:\backup\log\$(DateTime("date"))\$(DateTime("date"))_BackupReport.html"
  122.  
  123.     # Mail
  124.     $smtpServer = "smtp.yandex.ru"
  125.     $MailFrom = "robot@opttriabc.ru"
  126.     $mailto = "admin@opttriabc.ru"
  127.     $MailPass = 'Sa$ShoTyPhi#'
  128.  
  129.     #FTP
  130.     $ftp = "ftp://ftp.selcdn.ru"
  131.     $user = "16225_b"
  132.     $pass = ConvertTo-Securestring "M4QRUpKFXk" -AsPlainText -Force
  133.     $Credentials = New-Object System.Management.Automation.PSCredential($user,$pass)
  134.  
  135.    
  136.    
  137.    
  138. #   $Credentials = New-Object System.Net.NetworkCredential($user,$pass)  
  139.    
  140. # Executing
  141.     # Greetings
  142.     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
  143.     $BackupErrors = 0
  144.    
  145.     # Backup 1C Folder
  146.     Write-Output "<br>------------------------ Backup 1C server folder ------------------------<br><br>" >> $BackupReport
  147.     IF ($1cFolderEnabled -eq 1)
  148.     {
  149.         FOR ($a = 0; $a -lt $1cfDir.Count; $a++ )
  150.         {
  151.             $TCount = (Get-ChildItem -path $1cfDir[$a]["Path"] -filter "*.7z").Count
  152.             $TLast = (Get-ChildItem -path $1cfDir[$a]["Path"] -filter "*.7z" | Select-Object -last 1 -expand Name) -replace ".7z",""
  153.             $TSize = "{0:N2}" -f ((Get-ChildItem -path $1cfDir[$a]["Path"] -filter "*.7z" | Select-Object -last 1 | Measure-Object -property length -sum).sum / 1MB)
  154.             Write-Output $1cfDir[$a]['Host']
  155.             Write-Output $TCount
  156.             Write-Output $TLast
  157.             Write-Output $TSize
  158.             Write-Output " "
  159.            
  160.             Write-Output "Host:<a style=""font-weight:bold;"">"$1cfDir[$a]['Host']"</a><br>" >> $BackupReport
  161.             IF ($TLast -eq "$(DateTime("date"))")
  162.             {
  163.                 Write-Output "Last one:<a style=""color:green;""> $TLast</a><br>" >> $BackupReport
  164.             }
  165.             ELSEIF ($TLast -lt "$(DateTime("date"))")
  166.             {
  167.                 Write-Output "Last one:<a style=""color:green;""> $TLast</a><a style=""color:red;""> (OUTDATED!)</a><br>" >> $BackupReport
  168.                 $BackupErrors++
  169.             }
  170.             IF ($TSize -gt 1)
  171.             {
  172.                 Write-Output "Size: $TSize MB<br>" >>$BackupReport
  173.             }
  174.             ELSE
  175.             {
  176.                 Write-Output "Size: $TSize<a style=""font-family:arial;color:red;""> (Corrupted?)</a><br>" >> $BackupReport
  177.                 $BackupErrors++
  178.             }
  179.             FOR (;$TCount -gt $1cfDir[$a]['Max'];)
  180.             {
  181.                 $TFirst = Get-ChildItem -path $1cfDir[$a]["Path"] -filter "*.7z" | Select-Object -first 1 -expand Fullname
  182.                 Remove-Item $TFirst -force
  183.                 $TCount = (Get-ChildItem -path $1cfDir[$a]["Path"] -filter "*.7z").Count
  184.             }
  185.             Write-Output "Count: $TCount of "$1cfDir[$a]['Max']"<br><br>" >> $BackupReport
  186.            
  187.         }
  188.     }
  189.     ELSE
  190.     {
  191.         Write-Output "-------------------------------- <a style=""font-family:arial;color:red;""> Disabled </a>-------------------------------<br>" >> $BackupReport
  192.     }
  193.     # SQL
  194.     Write-Output "<br>------------------------------- Backup SQL ------------------------------<br><br>" >> $BackupReport
  195.  
  196.     IF ($SQLEnabled -eq 1)
  197.     {
  198.            
  199.            
  200.  
  201.              
  202.  
  203.                    
  204.                          
  205.         FOR ($a = 0; $a -lt $SQLDir.Count; $a++ )
  206.         {
  207.             Write-Output "Host:<a style=""font-weight:bold;"">"$SQLDir[$a]['Host']"</a><br>" >> $BackupReport
  208.             Write-Output $SQLDir[$a]['Host']
  209.            
  210.             FOR ($b = 0; $b -lt ($SQLDir[0]["Filter"]).Count; $b++ )
  211.             {
  212.  
  213.                 $TCount = (Get-ChildItem -path $SQLDir[$a]["Path"] -filter $SQLDir[$a]["Filter"][$b]).Count
  214.                 $TName = (Get-ChildItem -path $SQLDir[$a]["Path"] -filter $SQLDir[$a]["Filter"][$b] | Select-Object -last 1 -expand Name) -replace ".bak",""
  215.                 $TName -match "(?<date>[0-9]{4}_[0-9]{2}_[0-9]{2})"
  216.                 $TDate = $matches['date']
  217.                 $TDate = $TDate -replace "_","-"
  218.                 $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)
  219.                    
  220.  
  221.                
  222.                          
  223.  
  224.                  
  225.  
  226.  
  227.                 #Write-Output $TCount
  228.                 #Write-Output $TName
  229.                 #Write-Output $TDate
  230.                 #Write-Output $TSize
  231.                 #Write-Output " "
  232.  
  233.              
  234.                  
  235.          
  236.                
  237.                         IF ($TDate -eq "$(DateTime("date"))")
  238.                         {
  239.                         Write-Output "Last one:<a style=""color:green;""> $TName</a><br>" >> $BackupReport
  240.  
  241.                         }
  242.  
  243.                         ELSEIF ($TDate -lt "$(DateTime("date"))")
  244.                         {
  245.                         Write-Output "Last one:<a style=""color:green;""> $TName</a><a style=""color:red;""> (OUTDATED!)</a><br>" >> $BackupReport
  246.                         $BackupErrors++
  247.                         }
  248.  
  249.                        
  250.  
  251.                         IF ($TSize -gt 1)
  252.                         {
  253.                         Write-Output "Size: $TSize MB<br>" >>$BackupReport
  254.                         }
  255.                         ELSE
  256.                         {
  257.                         Write-Output "Size: $TSize<a style=""font-family:arial;color:red;""> (Corrupted?)</a><br>" >> $BackupReport
  258.                         }
  259.  
  260.  
  261.                        
  262. #                  
  263.                        
  264.                        
  265.                         FOR (;$TCount -gt $SQLDir[$a]['Max'];)
  266.                         {
  267.                         $TFirst = Get-ChildItem -path $SQLDir[$a]["Path"] -filter $SQLDir[$a]["Filter"][$b] | Select-Object -first 1 -expand Fullname
  268.                         Remove-Item $TFirst -force
  269.                         $TCount = (Get-ChildItem -path $SQLDir[$a]["Path"] -filter $SQLDir[$a]["Filter"][$b]).Count
  270.                         }
  271.  
  272.                 Write-Output "Count: $TCount of "$SQLDir[$a]['Max']"<br><br>" >> $BackupReport
  273.             }
  274.         }
  275.      
  276.     }
  277.    
  278.     ELSE
  279.     {
  280.         Write-Output "-------------------------------- <a style=""font-family:arial;color:red;""> Disabled </a>-------------------------------<br>" >> $BackupReport
  281.     }
  282.  
  283.  
  284.     # Selectel
  285. #   Write-Output "<br>--------------------------- Upload to Selectel --------------------------<br><br>" >> $BackupReport
  286.     IF ($SelectelEnabled -eq 1)
  287.     {
  288.          FOR ($a = 0; $a -lt $Selectel.Count; $a++)
  289.         {
  290.             Write-Output "File:<a style=""font-weight:bold;"">"$Selectel[$a]['Filter']"</a><br>" >> $BackupReport
  291.            
  292.             $TList = & "swift" -A https://auth.selcdn.ru -U "16225_b" -K "M4QRUpKFXk" list "triabc" 2>&1 | Out-String
  293.             IF ($TList -like "*Auth GET failed: https://auth.selcdn.ru 403 Forbidden*")
  294.             {
  295.                 Write-Output "<a style=""color:red;""> ERROR: Auth GET failed: https://auth.selcdn.ru 403 Forbidden</a><br>" >> $BackupReport
  296.                 $BackupErrors++
  297.             }
  298.             ELSE
  299.             {
  300.                 $TList = & "swift" -A https://auth.selcdn.ru -U "16225_b" -K "M4QRUpKFXk" list "triabc" | Select-String -pattern $Selectel[$a]["Filter"] | Sort-Object -Descending
  301.                 Write-Output $Tlist
  302.                 $TName = $TList[0]
  303.                 $TName -match "(?<date>[0-9]{4}_[0-9]{2}_[0-9]{2})"
  304.                 $TDate = $matches['date']
  305.                 $TDate = $TDate -replace "_","-"
  306.                 $TCount = $TList.Count
  307.    
  308.                 Write-Output $TList
  309.                 Write-Output $TDate
  310.                 Write-Output $TCount
  311.            
  312.                 IF ($TDate -eq "$(DateTime("date"))")
  313.                 {
  314.                     Write-Output "Last one:<a style=""color:green;""> $TDate</a><br>" >> $BackupReport
  315.                 }
  316.                 ELSEIF ($TDate -lt "$(DateTime("date"))")
  317.                 {
  318.                     Write-Output "Last one:<a style=""color:green;""> $TDate</a><a style=""color:red;""> (OUTDATED!)</a><br>" >> $BackupReport
  319.                     $BackupErrors++
  320.                 }
  321.                 Write-Output "Count: $TCount of "$Selectel[$a]['Max']"<br><br>" >> $BackupReport               
  322.             }
  323.  
  324.  
  325.         }  
  326.      
  327.     Write-Output "<br>------------------------ Upload to Selectel - FTP -----------------------<br><br>" >> $BackupReport
  328.      $TryNumber = 0
  329.  
  330.         while($TryNumber -lt 10)
  331.             {
  332.             try{
  333.                 write-host " "
  334.                 write-host "Connecting to"$ftp"..."
  335.  
  336.         #      Set-FTPConnection -Credentials $Credentials -Server $ftp -UseBinary -UsePassive
  337.                 Set-FTPConnection -Credentials $Credentials -Server $ftp -Session Session -UseBinary -UsePassive
  338.                 write-host "Connected!"
  339.                 $Connected = 1
  340.                 $TryNumber = 10
  341.                 }
  342.             catch [Exception] {
  343.                 write-host "Connection error"
  344.                 write-host $_.Exception.Message;
  345.                 write-host "Retry in 60 seconds..."
  346.                 $TryNumber = $TryNumber++
  347.                 Start-Sleep -sec 60
  348.                 }
  349.                
  350.             }  
  351. if ($Connected -eq 1)  
  352.     {
  353.     FOR ($a = 0; $a -lt $SQL.Count; $a++)
  354.         {
  355.  
  356.         $Filter = $SQL[$a]['Name']+"*.bak"
  357.         $SrcName = (Get-ChildItem -path $SQL[$a]['SrcPath'] -filter $Filter | Select -expand Name -last 1) -replace ".bak",""
  358.         $SrcFullName = $SQL[$a]['SrcPath']+"\"+$SrcName+".bak"
  359.  
  360.         $SrcCount = (Get-ChildItem -path $SQL[$a]['SrcPath'] -filter $Filter).count
  361.         $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)
  362.         $SrcDate = GetDateFromName($SrcFullName)
  363.  
  364.         $Ftpfilter = $SQL[$a]['Name']+"*"
  365.         $FtpPath = "ftp://ftp.selcdn.ru/triabc"
  366.         $FtpLastItem = Get-FTPChildItem -Path $FtpPath -filter $Ftpfilter | Select-Object -last 1
  367.         $FtpReplace = $SQL[$a]['Name']+"_"
  368.         $FtpDate = ($FtpLastItem.Name) -replace "$FtpReplace",""
  369.  
  370.         $FtpFullPath = $FtpPath+"/"+$FtpLastItem.Name
  371.        
  372.         $FtpFile = Get-FTPChildItem -Path $FtpFullPath -Recurse
  373.         $FtpSize = "{0:N2}" -f (($ftpFile | Measure-Object -property SizeInByte -sum).sum / 1GB)
  374.         $FtpCount = (Get-FTPChildItem -Path $FtpPath -filter $Ftpfilter).count
  375.        
  376.         $SrcSize = "{0:N2}" -f ((Get-ChildItem -path $SQL[$a]['SrcPath'] -force -filter $Filter | Select-Object -last 1 | Measure-Object -property length -sum).sum / 1GB)
  377.        
  378.         Write-Output "DB name: <a style=""font-weight:bold;""> "$SQL[$a]['Name']" </a><br>" >> $BackupReport
  379.         Write-Output "Number of backups: Local - $SrcCount; Cloud - $FtpCount <br>" >> $BackupReport
  380.        
  381.         if ($SrcSize -gt $SrcAvgSize)
  382.             {
  383.            
  384.             if ($SrcDate -eq $(DateTime("date")))
  385.                 {
  386.                 Write-Output "Last local item: <a style=""color:green;""> $SrcDate - $SrcSize"GB" </a> <br>" >> $BackupReport
  387.                 if ($FtpSize -eq $SrcSize -and $FtpDate -eq $SrcDate)
  388.                     {
  389.                     Write-Output "Last cloud item:<a style=""color:green;""> $FtpDate - $FtpSize"GB" </a> <br><br>" >> $BackupReport
  390.                     }
  391.                 else
  392.                     {
  393.                     Write-Output "Last cloud item:<a style=""color:red;""> $FtpDate - $FtpSize"GB" - (DON'T MATCHES!) </a> <br><br>" >> $BackupReport
  394.                     }
  395.                 }
  396.             else
  397.                 {
  398.                 Write-Output "Last local item:<a style=""color:red;""> $SrcDate - $SrcSize"GB" - (OUTDATED!) </a> <br>" >> $BackupReport
  399.                 Write-Output "Last cloud item:<a style=""color:red;""> $FtpDate - $FtpSize"GB" - (CHECK SOURCE!) </a> <br><br>" >> $BackupReport
  400.                 Write-Output "Debug FtpLastItem $FtpLastItem   FtpFullPath $FtpFullPath FtpCount $FtpCount   <br><br>" >> $BackupReport
  401.                 }
  402.             }
  403.         else
  404.             {
  405.             Write-Output "Last local item:<a style=""color:red;""> $SrcDate - $SrcSize"GB" - (CORRUPTED!) </a> <br>" >> $BackupReport
  406.             Write-Output "Last cloud item:<a style=""color:red;""> $FtpDate - $FtpSize"GB" - (CHECK SOURCE!) </a> <br><br>" >> $BackupReport
  407.             }
  408.  
  409.         }
  410.        
  411.     }
  412. else
  413.     {
  414.     Write-Output "FTP connection error!" >> $BackupReport
  415.     }
  416.    
  417.     }
  418.     ELSE
  419.     {
  420.         Write-Output "-------------------------------- <a style=""font-family:arial;color:red;""> Disabled </a>-------------------------------<br>" >> $BackupReport
  421.     }
  422.     Write-Output "<br>----------------------------- Backup 1C Conf ----------------------------<br><br>" >> $BackupReport
  423.     IF ($1cConfEnabled -eq 1)
  424.     {
  425.         FOR ($a = 0; $a -lt $1cConfDir.Count; $a++ )
  426.         {
  427.             $TCount = (Get-ChildItem -path $1cConfDir[$a]["Path"] -filter "*.cf").Count
  428.             $TLast = (Get-ChildItem -path $1cConfDir[$a]["Path"] -filter "*.cf" | Select-Object -last 1 -expand Name) -replace ".cf",""
  429.             $TSize = "{0:N2}" -f ((Get-ChildItem -path $1cConfDir[$a]["Path"] -filter "*.cf" | Select-Object -last 1 | Measure-Object -property length -sum).sum / 1MB)
  430.             Write-Output $1cConfDir[$a]['Host']
  431.             Write-Output $TCount
  432.             Write-Output $TLast
  433.             Write-Output $TSize
  434.             Write-Output " "
  435.            
  436.             Write-Output "Host:<a style=""font-weight:bold;"">"$1cConfDir[$a]['Host']"</a><br>" >> $BackupReport
  437.             IF ($TLast -eq "$(DateTime("date"))")
  438.             {
  439.                 Write-Output "Last one:<a style=""color:green;""> $TLast</a><br>" >> $BackupReport
  440.             }
  441.             ELSEIF ($TLast -lt "$(DateTime("date"))")
  442.             {
  443.                 Write-Output "Last one:<a style=""color:green;""> $TLast</a><a style=""color:red;""> (OUTDATED!)</a><br>" >> $BackupReport
  444.                 $BackupErrors++
  445.             }
  446.             IF ($TSize -gt 1)
  447.             {
  448.                 Write-Output "Size: $TSize MB<br>" >>$BackupReport
  449.             }
  450.             ELSE
  451.             {
  452.                 Write-Output "Size: $TSize<a style=""font-family:arial;color:red;""> (Corrupted?)</a><br>" >> $BackupReport
  453. #               $TCount--
  454.                 $BackupErrors++
  455.             }
  456.             FOR (;$TCount -gt $1cConfDir[$a]['Max'];)
  457.             {
  458.                 $TFirst = Get-ChildItem -path $1cConfDir[$a]["Path"] -filter "*.cf" | Select-Object -first 1 -expand Fullname
  459.                 Remove-Item $TFirst -force
  460.                 $TCount = (Get-ChildItem -path $1cConfDir[$a]["Path"] -filter "*.cf").Count
  461.             }
  462.             Write-Output "Count: $TCount of "$1cConfDir[$a]['Max']"<br><br>" >> $BackupReport
  463.            
  464.         }
  465.     }
  466.     ELSE
  467.     {
  468.         Write-Output "-------------------------------- <a style=""font-family:arial;color:red;""> Disabled </a>-------------------------------<br>" >> $BackupReport
  469.     }
  470. # Backup Cleverence Folder
  471.     Write-Output "<br>------------------------ Backup Cleverence folder -----------------------<br><br>" >> $BackupReport
  472.     IF ($CleverFolderEnabled -eq 1)
  473.     {
  474.         FOR ($a = 0; $a -lt $CleverFolderDir.Count; $a++ )
  475.         {
  476.             $TCount = (Get-ChildItem -path $CleverFolderDir[$a]["Path"] -filter "*.7z").Count
  477.             $TLast = (Get-ChildItem -path $CleverFolderDir[$a]["Path"] -filter "*.7z" | Select-Object -last 1 -expand Name) -replace ".7z",""
  478.             $TSize = "{0:N2}" -f ((Get-ChildItem -path $CleverFolderDir[$a]["Path"] -filter "*.7z" | Select-Object -last 1 | Measure-Object -property length -sum).sum / 1MB)
  479.             Write-Output $CleverFolderDir[$a]['Host']
  480.             Write-Output $TCount
  481.             Write-Output $TLast
  482.             Write-Output $TSize
  483.             Write-Output " "
  484.            
  485.             Write-Output "Host:<a style=""font-weight:bold;"">"$CleverFolderDir[$a]['Host']"</a><br>" >> $BackupReport
  486.             IF ($TLast -eq "$(DateTime("date"))")
  487.             {
  488.                 Write-Output "Last one:<a style=""color:green;""> $TLast</a><br>" >> $BackupReport
  489.             }
  490.             ELSEIF ($TLast -lt "$(DateTime("date"))")
  491.             {
  492.                 Write-Output "Last one:<a style=""color:green;""> $TLast</a><a style=""color:red;""> (OUTDATED!)</a><br>" >> $BackupReport
  493.                 $BackupErrors++
  494.             }
  495.             IF ($TSize -gt 1)
  496.             {
  497.                 Write-Output "Size: $TSize MB<br>" >>$BackupReport
  498.             }
  499.             ELSE
  500.             {
  501.                 Write-Output "Size: $TSize<a style=""font-family:arial;color:red;""> (Corrupted?)</a><br>" >> $BackupReport
  502. #               $TCount--
  503.                 $BackupErrors++
  504.             }
  505.             FOR (;$TCount -gt $CleverFolderDir[$a]['Max'];)
  506.             {
  507.                 $TFirst = Get-ChildItem -path $CleverFolderDir[$a]["Path"] -filter "*.7z" | Select-Object -first 1 -expand Fullname
  508.                 Remove-Item $TFirst -force
  509.                 $TCount = (Get-ChildItem -path $CleverFolderDir[$a]["Path"] -filter "*.7z").Count
  510.             }
  511.             Write-Output "Count: $TCount of "$CleverFolderDir[$a]['Max']"<br><br>" >> $BackupReport
  512.            
  513.         }
  514.     }
  515.     ELSE
  516.     {
  517.         Write-Output "-------------------------------- <a style=""font-family:arial;color:red;""> Disabled </a>-------------------------------<br>" >> $BackupReport
  518.     }
  519. # Backup Cleverence Base
  520.     Write-Output "<br>------------------------- Backup Cleverence base ------------------------<br><br>" >> $BackupReport
  521.     IF ($CleverBaseEnabled -eq 1)
  522.     {
  523.         FOR ($a = 0; $a -lt $CleverBaseDir.Count; $a++ )
  524.         {
  525.             $TCount = (Get-ChildItem -path $CleverBaseDir[$a]["Path"] -filter "*.7z").Count
  526.             $TLast = (Get-ChildItem -path $CleverBaseDir[$a]["Path"] -filter "*.7z" | Select-Object -last 1 -expand Name) -replace ".7z",""
  527.             $TSize = "{0:N2}" -f ((Get-ChildItem -path $CleverBaseDir[$a]["Path"] -filter "*.7z" | Select-Object -last 1 | Measure-Object -property length -sum).sum / 1MB)
  528.             Write-Output $CleverBaseDir[$a]['Host']
  529.             Write-Output $TCount
  530.             Write-Output $TLast
  531.             Write-Output $TSize
  532.             Write-Output " "
  533.            
  534.             Write-Output "Host:<a style=""font-weight:bold;"">"$CleverBaseDir[$a]['Host']"</a><br>" >> $BackupReport
  535.             IF ($TLast -eq "$(DateTime("date"))")
  536.             {
  537.                 Write-Output "Last one:<a style=""color:green;""> $TLast</a><br>" >> $BackupReport
  538.             }
  539.             ELSEIF ($TLast -lt "$(DateTime("date"))")
  540.             {
  541.                 Write-Output "Last one:<a style=""color:green;""> $TLast</a><a style=""color:red;""> (OUTDATED!)</a><br>" >> $BackupReport
  542.                 $BackupErrors++
  543.             }
  544.             IF ($TSize -gt 1)
  545.             {
  546.                 Write-Output "Size: $TSize MB<br>" >>$BackupReport
  547.             }
  548.             ELSE
  549.             {
  550.                 Write-Output "Size: $TSize<a style=""font-family:arial;color:red;""> (Corrupted?)</a><br>" >> $BackupReport
  551. #               $TCount--
  552.                 $BackupErrors++
  553.             }
  554.             FOR (;$TCount -gt $CleverBaseDir[$a]['Max'];)
  555.             {
  556.                 $TFirst = Get-ChildItem -path $CleverBaseDir[$a]["Path"] -filter "*.7z" | Select-Object -first 1 -expand Fullname
  557.                 Remove-Item $TFirst -force
  558.                 $TCount = (Get-ChildItem -path $CleverBaseDir[$a]["Path"] -filter "*.7z").Count
  559.             }
  560.             Write-Output "Count: $TCount of "$CleverBaseDir[$a]['Max']"<br><br>" >> $BackupReport
  561.            
  562.         }
  563.     }
  564.     ELSE
  565.     {
  566.         Write-Output "-------------------------------- <a style=""font-family:arial;color:red;""> Disabled </a>-------------------------------<br>" >> $BackupReport
  567.     }
  568. # Backup Cleverence Folder
  569.     Write-Output "<br>---------------------------- Backup Eco folder --------------------------<br><br>" >> $BackupReport
  570.     IF ($EcoEnabled -eq 1)
  571.     {
  572.         FOR ($a = 0; $a -lt $EcoDir.Count; $a++ )
  573.         {
  574.             $TCount = (Get-ChildItem -path $EcoDir[$a]["Path"] -filter "*.7z").Count
  575.             $TLast = (Get-ChildItem -path $EcoDir[$a]["Path"] -filter "*.7z" | Select-Object -last 1 -expand Name) -replace ".7z",""
  576.             $TSize = "{0:N2}" -f ((Get-ChildItem -path $EcoDir[$a]["Path"] -filter "*.7z" | Select-Object -last 1 | Measure-Object -property length -sum).sum / 1MB)
  577.             Write-Output $EcoDir[$a]['Host']
  578.             Write-Output $TCount
  579.             Write-Output $TLast
  580.             Write-Output $TSize
  581.             Write-Output " "
  582.            
  583.             Write-Output "Host:<a style=""font-weight:bold;"">"$EcoDir[$a]['Host']"</a><br>" >> $BackupReport
  584.             IF ($TLast -eq "$(DateTime("date"))")
  585.             {
  586.                 Write-Output "Last one:<a style=""color:green;""> $TLast</a><br>" >> $BackupReport
  587.             }
  588.             ELSEIF ($TLast -lt "$(DateTime("date"))")
  589.             {
  590.                 Write-Output "Last one:<a style=""color:green;""> $TLast</a><a style=""color:red;""> (OUTDATED!)</a><br>" >> $BackupReport
  591.                 $BackupErrors++
  592.             }
  593.             IF ($TSize -gt 1)
  594.             {
  595.                 Write-Output "Size: $TSize MB<br>" >>$BackupReport
  596.             }
  597.             ELSE
  598.             {
  599.                 Write-Output "Size: $TSize<a style=""font-family:arial;color:red;""> (Corrupted?)</a><br>" >> $BackupReport
  600. #               $TCount--
  601.                 $BackupErrors++
  602.             }
  603.             FOR (;$TCount -gt $EcoDir[$a]['Max'];)
  604.             {
  605.                 $TFirst = Get-ChildItem -path $EcoDir[$a]["Path"] -filter "*.7z" | Select-Object -first 1 -expand Fullname
  606.                 Remove-Item $TFirst -force
  607.                 $TCount = (Get-ChildItem -path $EcoDir[$a]["Path"] -filter "*.7z").Count
  608.             }
  609.             Write-Output "Count: $TCount of "$EcoDir[$a]['Max']"<br><br>" >> $BackupReport
  610.            
  611.         }
  612.     }
  613.     ELSE
  614.     {
  615.         Write-Output "-------------------------------- <a style=""font-family:arial;color:red;""> Disabled </a>-------------------------------<br>" >> $BackupReport
  616.     }
  617. # HyperV
  618.     Write-Output "<br>----------------------------- Backup Hyper-V ----------------------------<br><br>" >> $BackupReport
  619.     IF ($HypervEnabled -eq 1)
  620.     {
  621.  
  622.         FOR ($a = 0; $a -lt $HypervDir.Count; $a++ )
  623.         {
  624.             Write-Output "Host:<a style=""font-weight:bold;"">"$HypervDir[$a]['Host']"</a><br>" >> $BackupReport
  625.             Write-Output $HypervDir[$a]['Host']
  626.            
  627.             FOR ($b = 0; $b -lt ($HypervDir[0]["VMs"]).Count; $b++ )
  628.             {
  629.  
  630.                 $filter = $HypervDir[$a]["VMs"][$b]+"*"
  631.                 $list = Get-ChildItem $HypervDir[$a]["Path"] -Directory -Filter $filter | where {$_.LastWriteTime -lt (Get-Date).AddDays(-$HypervDir[$a]["Daily"])}
  632.  
  633.                 cd $HypervDir[$a]["Path"]
  634.  
  635.                 foreach ($item in $list)
  636.                 {
  637.                     $day = ($item.LastWriteTime).DayOfWeek
  638.                     if ($day -ne "Monday")
  639.                     {
  640.                     Remove-Item -path $item -recurse -force
  641.                     }
  642.                 }
  643.  
  644.                
  645.                 $TCount = (Get-ChildItem -directory -path $HypervDir[$a]["Path"] -filter $filter).Count
  646.  
  647.  
  648.  
  649.                 $TFullpath = Get-ChildItem -directory -path $HypervDir[$a]["Path"] -filter $filter | Select-Object -last 1 -expand Fullname
  650.                 $TName = (Get-ChildItem -directory -path $HypervDir[$a]["Path"] -filter $filter | Select-Object -last 1 -expand Name)
  651.                 $TName -match "_(?<date>[0-9]{4}[0-9]{2}[0-9]{2})"
  652.                 $TDate = GetDateFromName($TFullpath)
  653.                 $TSize = "{0:N2}" -f ((Get-ChildItem -path $TFullpath -force -Recurse | Measure-Object -property length -sum).sum / 1MB)
  654.                
  655.                 Write-host $TCount
  656.                 Write-host $TName
  657.                 Write-host $TDate
  658.                 Write-host $(DateTime("date"))
  659.                 Write-host $TSize
  660.                 Write-host $TFullpath
  661.                 Write-host "========================"
  662.  
  663.                
  664.                 IF ($TDate -eq "$(DateTime("date"))")
  665.                 {
  666.                     Write-Output "Last one:<a style=""color:green;""> $TName</a><br>" >> $BackupReport
  667.                 }
  668.                 ELSEIF ($TDate -lt "$(DateTime("date"))")
  669.                 {
  670.                     Write-Output "Last one:<a style=""color:green;""> $TName</a><a style=""color:red;""> (OUTDATED!)</a><br>" >> $BackupReport
  671.                     $BackupErrors++
  672.                 }
  673.                 IF ($TSize -gt 1)
  674.                 {
  675.                     Write-Output "Size: $TSize MB<br>" >>$BackupReport
  676.                 }
  677.                 ELSE
  678.                 {
  679.                     Write-Output "Size: $TSize<a style=""font-family:arial;color:red;""> (Corrupted?)</a><br>" >> $BackupReport
  680. #                   $TCount--
  681.                     $BackupErrors++
  682.                 }
  683.                 FOR (;$TCount -gt $HypervDir[$a]['Max'];)
  684.                 {
  685.                     $TFirst = Get-ChildItem -directory -path $HypervDir[$a]["Path"] -filter $filter | Select-Object -first 1 -expand Fullname
  686.                     Remove-Item $TFirst -force -recurse
  687.                     write-host $TCount
  688.                     $TCount = (Get-ChildItem -directory -path $HypervDir[$a]["Path"] -filter $filter).Count
  689.                 }
  690.                 Write-Output "Count: $TCount of "$HypervDir[$a]['Max']"<br><br>" >> $BackupReport
  691.             }
  692.         }
  693.     }
  694.     ELSE
  695.     {
  696.         Write-Output "-------------------------------- <a style=""font-family:arial;color:red;""> Disabled </a>-------------------------------<br>" >> $BackupReport
  697.     }
  698.    
  699.  
  700. <#
  701.     Write-Output "<br>-------------------------- Storage stats on \\DC ----------------------<br>" >> $BackupReport
  702.     Get-DiskFreeSpace >> $BackupReport
  703. #>
  704.    
  705.  
  706. # End of Report
  707.     Write-Output "<br>-------------------------- End of report at $(DateTime("time")). ----------------------<br>" >> $BackupReport
  708.     Write-Output "------------------------------- Have a nice day. ----------------------------<br>" >> $BackupReport
  709.  
  710. # E-Mail and stuff
  711.  
  712.     IF ($MailEnabled -eq 1)
  713.     {
  714.     [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$True}
  715.         IF ($BackupErrors -eq 0)
  716.         {
  717.             $msg = new-object Net.Mail.MailMessage  
  718.             $smtp = new-object Net.Mail.SmtpClient($smtpServer)  
  719.             $msg.From = $MailFrom
  720.             $msg.IsBodyHTML = $true
  721.             $msg.To.Add($Mailto)  
  722.             $msg.Subject = "Backup report (success)"
  723.             $MailTextT =  Get-Content  -Path $BackupReport
  724.             $msg.Body = $MailTextT
  725.             $smtp.EnableSsl = $true
  726.             $smtp.Credentials = New-Object System.Net.NetworkCredential($MailFrom, $MailPass);
  727.             $smtp.Send($msg)
  728.         }
  729.         ELSE
  730.         {
  731.             $msg = new-object Net.Mail.MailMessage  
  732.             $smtp = new-object Net.Mail.SmtpClient($smtpServer)  
  733.             $msg.From = $MailFrom
  734.             $msg.IsBodyHTML = $true
  735.             $msg.To.Add($Mailto)  
  736.             $msg.Subject = "BACKUP ERROR! ($BackupErrors error(s) total)"
  737.             $MailTextT =  Get-Content  -Path $BackupReport | Out-String
  738.             $msg.Body = $MailTextT
  739.             $smtp.EnableSsl = $true
  740.             $smtp.Credentials = New-Object System.Net.NetworkCredential($MailFrom, $MailPass);
  741.             $smtp.Send($msg)
  742.         }
  743.     }
  744.     # чтобы окно не закрылось
  745. #   $host.ui.RawUI.ReadKey(6)|out-null
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement