Guest User

Untitled

a guest
Jul 18th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.57 KB | None | 0 0
  1. Get-Date
  2. $start=Get-Date
  3. $passwd = 'YOURPASSWORD'
  4. net use * /delete /y
  5. net use W: \\BACKUPSERVERIP\c$\winhv1 /user:administrator $passwd /Persistent:Yes /Y
  6. $failcount = 0
  7. $NetworkSharePath = "\\BACKUPSERVERIP\c$\winhv1"
  8. $ExportPath = "D:\backup"
  9. $limit = (Get-Date).AddDays(-15) # backups older than 15 days will be deleted
  10. $BackupServerName = "BACKUPSERVERNAME"
  11. . C:\scripts\email-rep.ps1
  12.  
  13. $tblname=get-date -Format M
  14. $tblname = $tblname -replace '\s',''
  15. $tblname +="hv1"
  16.  
  17. #Folder name creation in backup server
  18. New-Item -Path W:\$tblname -ItemType directory
  19.  
  20. #database creation
  21. $sqlConnection = new-object System.Data.SqlClient.SqlConnection “server=DBSERVERIP;database=dbname;Integrated Security=false;User ID = dbuser; Password = DBPASSWORD”
  22. $sqlConnection.Open()
  23. $sqlCommand = $sqlConnection.CreateCommand()
  24. $sqlCommand.CommandText = “create table dbo.$tblname(VMName varchar(100),starttm varchar(70),endtm varchar(70),Exporttm varchar(30),exportsz varchar(30),rexportsz varchar(30),compare varchar(20),totaltm varchar(30));”
  25. $sqlReader = $sqlCommand.ExecuteReader()
  26. $sqlConnection.Close()
  27.  
  28. #getting the list of VMs that are running
  29. $list = (Get-VM |where {$_.state -eq 'Running'} | Select Name -ExpandProperty Name)
  30.  
  31.  
  32. foreach ($i in $list)
  33. {
  34. Write-Host " Exporting $i "
  35. $sqlConnection.Open()
  36. $vmts = Get-Date
  37. $path = "$ExportPath\$i"
  38. $networkpath = "$NetworkSharePath\$tblname\$i"
  39. if(!(Test-Path $path))
  40. {
  41. Export-VM -Name $i -Path $ExportPath
  42. $vmte = Get-Date
  43. $vmexe = New-TimeSpan -Start $vmts -End $vmte
  44. Write-Output "Export time of VM $i is $($vmexe.Days) Days: $($vmexe.Hours) Hrs: $($vmexe.Minutes) Min: $($vmexe.Seconds) Sec"
  45. $exporttm = "$($vmexe.Days) Days $($vmexe.Hours) Hrs: $($vmexe.Minutes) Min: $($vmexe.Seconds) Sec"
  46. Get-ChildItem $ExportPath -Directory <#norecurse#> | foreach {
  47. $directoryName = $_.Name
  48. $lfoldersz = (Get-ChildItem "$ExportPath\$directoryName" -recurse | Measure-Object -property length -sum)
  49. $localsz = $lfoldersz.sum
  50. $lfoldersz = "{0:N2}" -f ($lfoldersz.sum / 1GB)
  51. $lfoldersz = [math]::Round($lfoldersz)
  52. Write-Host " Local backup size is $lfoldersz GB"
  53. $RemoteDriveSize = Get-PSDrive W
  54. $RemoteDriveSize = $RemoteDriveSize.Free
  55. $RemoteDriveSize = "{0:N2}" -f ($RemoteDriveSize / 1GB)
  56. $RemoteDriveSizer = [math]::Round($RemoteDriveSize)
  57. Write-Host " remote disk space is $RemoteDriveSize gb"
  58. if($RemoteDriveSizer -le $lfoldersz)
  59. {
  60. $Neededspace = $lfoldersz - $RemoteDriveSize
  61. Write-Host " needed space $Neededspace "
  62. $msg.Subject = "Backup Warning: $env:COMPUTERNAME Backup drive Full" #getting Computer name
  63. $msg.Body = "Hello, `nPlease free-up $Neededspace GB in $BackupServerName to continue the backup process of $directoryName VM"
  64. $smtp.Send($msg)
  65. #Read-Host "Press Enter"
  66. Start-Sleep -Seconds 1800
  67. Copy-Item $ExportPath\$directoryName $NetworkSharePath\$tblname
  68.  
  69. }
  70. Elseif(!(Test-Path $networkpath))
  71. {
  72. Write-Host "Begining file transfer of $i VM to backup server"
  73. Get-Date
  74. Copy-Item $ExportPath\$i $NetworkSharePath\$tblname -Recurse
  75. }
  76. else { Write-Host " VM $i already exist in $networkpath" -ForegroundColor Green }
  77. Write-Host "$i VM copied successfully"
  78. $rfoldersz = (Get-ChildItem $NetworkSharePath\$tblname\$directoryName -recurse | Measure-Object -property length -sum)
  79. $remotesz = $rfoldersz.sum
  80. $rfoldersz = "{0:N2}" -f ($rfoldersz.sum / 1GB) + " GB"
  81. $lfoldersz = "{0:N2}" -f ($localsz / 1GB) + " GB"
  82. #Write-Host "Remote backup size $rfoldersz "
  83. }
  84. if($localsz -eq $remotesz)
  85. {
  86. $res = "TRUE"
  87. Write-Output " The VM $directoryName is equal in source and destination."
  88. Write-Output " Removing export backup... "
  89. Remove-Item $ExportPath\$directoryName -Recurse
  90.  
  91. }
  92. else
  93. {
  94. $res = "FALSE"
  95. $failcount++
  96. $msg.Subject = "Backup Error: $env:COMPUTERNAME" #getting Computer name
  97. $msg.Body = "Hello, `nThe VM $directoryName is not equal in source and destination."
  98. $smtp.Send($msg)
  99. Write-Host " The VM $directoryName is not equal in source and destination." -BackgroundColor Red
  100. Write-Output " Removing export backup... "
  101. Remove-Item $ExportPath\$directoryName -Recurse
  102.  
  103. }
  104. $sqlCommand.CommandText = "INSERT INTO dbo.$tblname(VMName,starttm,endtm,Exporttm,exportsz,rexportsz,compare) VALUES ('$directoryName','$vmts','$vmte','$exporttm','$lfoldersz','$rfoldersz','$res')"
  105. $sqlReader = $sqlCommand.ExecuteReader()
  106.  
  107. }
  108. else
  109. {
  110. Write-host " VM $i already exist in local backup folder" -foregroundcolor "Yellow"
  111. if(!(Test-Path $networkpath))
  112. {
  113. Copy-Item $ExportPath\$directoryName $NetworkSharePath\$tblname -Recurse
  114. }
  115. Remove-Item $ExportPath\$directoryName -Recurse
  116. }
  117. $sqlConnection.Close()
  118. }
  119.  
  120. Get-Date
  121.  
  122. $end = Get-Date
  123. $exectime = New-TimeSpan -Start $start -End $end
  124. Write-Output "Script execution time is: $($exectime.Days) Days: $($exectime.Hours) Hrs: $($exectime.Minutes) Min: $($exectime.Seconds) Sec"
  125. $totaltm = "$($exectime.Days) Days: $($exectime.Hours) Hrs: $($exectime.Minutes) Min: $($exectime.Seconds) Sec"
  126. $sqlConnection.Open()
  127. $sqlCommand = $sqlConnection.CreateCommand()
  128. $sqlCommand.CommandText += "INSERT INTO dbo.$tblname(totaltm) VALUES ('$totaltm')"
  129. $sqlReader = $sqlCommand.ExecuteReader()
  130. $sqlConnection.Close()
  131. if($failcount -eq 0)
  132. {
  133. $msg.Subject = "Backup Notification: $env:COMPUTERNAME" #getting Computer name
  134. $msg.Body = "Hello, `n$env:COMPUTERNAME server backup completed Successfully."
  135. $smtp.Send($msg)
  136. get-childitem -Path $NetworkSharePath -Recurse -Force |? {$_.psiscontainer -and $_.lastwritetime -le $limit } | Remove-Item -Force -Recurse
  137. }
  138. else
  139. {
  140. $msg.Subject = "Backup Error: $env:COMPUTERNAME" #getting Computer name
  141. $msg.Body = "Hello, `n$env:COMPUTERNAME server backup completed with error."
  142. $smtp.Send($msg)
  143. Write-Host "Backup completed with error" -ForegroundColor Black -BackgroundColor Red
  144. }
  145. $failcount = 0
  146. Remove-PSDrive W
  147. Write-Output "-----------------------------------------------------------------------"
Add Comment
Please, Sign In to add comment