Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 9.67 KB | None | 0 0
  1. Imports System.Data.SqlClient
  2. Imports Microsoft.VisualBasic.Devices.Network
  3. Imports System.IO
  4. Imports System.IO.Compression
  5. Imports System.Net
  6. Imports System.Net.Mail
  7.  
  8.  
  9. Module Module1
  10.     Const Line As String = "ControlChars.CrLf"
  11.     Sub Main()
  12.         Dim SQLConnection As SqlConnection
  13.         Dim SQLCommands As SqlCommand
  14.         Dim FTPConnection As New Microsoft.VisualBasic.Devices.Network
  15.         Dim ParameterCommands As String = Interaction.Command
  16.         Dim SQLDBhost, SQLDatabase, SQLUsername, SQLPassword As String
  17.         Dim FTPHost, FTPUsername, FTPPassword, Operations, TempFile As String
  18.         Dim SMTPHost, SMTPUser, SMTPPass, SMTPFROM, SMTPTo, SMTPLog As String
  19.         Dim SQLSuccess As Boolean = False
  20.         Dim Restore As Boolean = False
  21.         If ParameterCommands = "" Then
  22.             Console.WriteLine("You haven't specified a parameter" & vbCrLf & vbCrLf & "Usable Parameters are: -SQLDBhost-SQLDatabase Name-SQLUsername-SQLPassword-FTPServer-FTPUsername-FTPPassword-SMTPHost-SMTPUser-SMTPPass-SMTPFROM-SMTPTo")
  23.             Console.Read()
  24.         Else
  25.             SQLDBhost = Split(ParameterCommands, "-")(1).ToString
  26.             SQLDatabase = Split(ParameterCommands, "-")(2).ToString
  27.             SQLUsername = Split(ParameterCommands, "-")(3).ToString
  28.             SQLPassword = Split(ParameterCommands, "-")(4).ToString
  29.             FTPHost = Split(ParameterCommands, "-")(5).ToString
  30.             FTPUsername = Split(ParameterCommands, "-")(6).ToString
  31.             FTPPassword = Split(ParameterCommands, "-")(7).ToString
  32.             Operations = Split(ParameterCommands, "-")(8).ToString
  33.             SMTPHost = Split(ParameterCommands, "-")(9).ToString
  34.             SMTPUser = Split(ParameterCommands, "-")(10).ToString
  35.             SMTPPass = Split(ParameterCommands, "-")(11).ToString
  36.             SMTPFROM = Split(ParameterCommands, "-")(12).ToString
  37.             SMTPTo = Split(ParameterCommands, "-")(13).ToString
  38.             If SQLDBhost = "" Or SQLDatabase = "" Or SQLUsername = "" Or SQLPassword = "" Or FTPHost = "" Or FTPUsername = "" Or FTPPassword = "" Or Operations = "" Or SMTPHost = "" Or SMTPUser = "" Or SMTPPass = "" Or SMTPFROM = "" Or SMTPTo = "" Then
  39.                 Console.WriteLine("Some Parameters are missing!" & vbCrLf & "Usable Parameters are: -SQLDBhost-SQLDatabase Name-SQLUsername-SQLPassword-FTPServer-FTPUsername-FTPPassword-SMTPHost-SMTPUser-SMTPPass-SMTPFROM-SMTPTo")
  40.             Else
  41.                 If UCase(Operations) = "BACKUP" Then
  42.                     Console.WriteLine("------EXECUTING BACKUP OPERATIONS------")
  43.                     SQLConnection = New SqlConnection("DATA SOURCE=" & SQLDBhost & ";user id=" & SQLUsername & ";password=" & SQLPassword)
  44.                     Console.WriteLine(SQLConnection.ConnectionString)
  45.                     TempFile = Microsoft.VisualBasic.FileIO.FileSystem.GetTempFileName
  46.                     Console.WriteLine(TempFile)
  47.                     SQLConnection.Open()
  48.                     If SQLConnection.State = ConnectionState.Open Then
  49.                         SQLCommands = New SqlCommand("BACKUP DATABASE " & SQLDatabase & " TO DISK='" & TempFile & "'")
  50.                         SQLCommands.Connection = SQLConnection
  51.                         Console.WriteLine(SQLCommands.CommandText)
  52.                         SQLCommands.ExecuteNonQuery()
  53.                         Console.WriteLine(SQLCommands.CommandText & " Executed!")
  54.                         SQLConnection.Close()
  55.                         Console.WriteLine(CompressFile(TempFile, Microsoft.VisualBasic.FileIO.FileSystem.GetParentPath(TempFile)))
  56.                         SQLSuccess = True
  57.                         If SQLSuccess = True Then
  58.                             Console.WriteLine("Uploading Database...")
  59.                             FTPConnection.UploadFile(TempFile & ".zip", "ftp://" & FTPHost & "/backup.zip", FTPUsername, FTPPassword)
  60.                             Console.WriteLine("Database Upload Complete...")
  61.                             Console.WriteLine("Press any key to exit...")
  62.                         Else
  63.                             Console.WriteLine("Could not execute Database backup! Please check your parameters!")
  64.                         End If
  65.                     End If
  66.                 Else
  67.                     If UCase(Operations) = "RESTORE" Then
  68.                         Console.WriteLine("------EXECUTING RESTORE OPERATIONS------")
  69.                         SQLConnection = New SqlConnection("DATA SOURCE=" & SQLDBhost & ";user id=" & SQLUsername & ";password=" & SQLPassword)
  70.                         Console.WriteLine(SQLConnection.ConnectionString)
  71.                         TempFile = Microsoft.VisualBasic.FileIO.FileSystem.GetTempFileName
  72.                         Console.WriteLine(TempFile)
  73.                         Console.WriteLine("Downloading backup...")
  74.                         FTPConnection.DownloadFile("ftp://" & FTPHost & "/backup.zip", TempFile & ".zip", FTPUsername, FTPPassword)
  75.                         Console.WriteLine("Download Complete...")
  76.                         Console.Write("Decompressing Data...")
  77.                         DecompressFile(TempFile & ".zip", "backup.bak", Microsoft.VisualBasic.FileIO.FileSystem.GetParentPath(TempFile))
  78.                         Console.WriteLine("Done Decompressing Downloaded Backup...")
  79.                         SQLConnection.Open()
  80.                         If SQLConnection.State = ConnectionState.Open Then
  81.                             Console.WriteLine("Restoring Database...")
  82.                             SQLCommands = New SqlCommand("RESTORE DATABASE [" & SQLDatabase & "] FROM DISK=N'" & Microsoft.VisualBasic.FileIO.FileSystem.GetParentPath(TempFile) & "\backup.bak' WITH  FILE = 1,  NOUNLOAD,  REPLACE,  STATS = 10")
  83.                             SQLCommands.Connection = SQLConnection
  84.                             Console.WriteLine(SQLCommands.CommandText)
  85.                             SQLCommands.ExecuteNonQuery()
  86.                             Console.WriteLine(SQLCommands.CommandText & " Executed Successfuly!")
  87.                             SQLConnection.Close()
  88.                             Restore = True
  89.                             If Restore = True Then
  90.                                 SendMail(SMTPHost, SMTPUser, SMTPPass, SMTPFROM, SMTPTo, "BackupTool", "Database Restore was successful at : " & Date.Now)
  91.  
  92.                             Else
  93.                                 SendMail(SMTPHost, SMTPUser, SMTPPass, SMTPFROM, SMTPTo, "BackupTool", "Database Restore was not successful at : " & Date.Now)
  94.                             End If
  95.                         End If
  96.                     Else
  97.  
  98.                     End If
  99.  
  100.  
  101.                 End If
  102.             End If
  103.         End If
  104.     End Sub
  105.  
  106.     Private Function SendMail(ByVal Host As String, ByVal Username As String, ByVal Password As String, ByVal FromMail As String, ByVal ToMail As String, ByVal Subject As String, ByVal Body As String) As Boolean
  107.         Dim SMTPC As New SmtpClient(Host)
  108.         Dim Mailmessage As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage
  109.         SMTPC.Credentials = New Net.NetworkCredential(Username, Password)
  110.         Mailmessage.From = New MailAddress(FromMail)
  111.         Mailmessage.To.Add(ToMail)
  112.         Mailmessage.Subject = Subject
  113.         Mailmessage.IsBodyHtml = True
  114.         Mailmessage.Body = Body
  115.         Mailmessage.Attachments.Add("
  116.        SMTPC.Send(Mailmessage)
  117.        SendMail = True
  118.    End Function
  119.    Public Function DecompressFile(ByRef inputFileName As String, ByRef destFileName As String, ByRef destDirectory As String) As Boolean
  120.        Try
  121.            Dim stream As New MemoryStream(File.ReadAllBytes(inputFileName))
  122.            Dim gZip As New GZipStream(stream, CompressionMode.Decompress)
  123.            Dim buffer(3) As Byte
  124.            stream.Position = stream.Length - 5
  125.            stream.Read(buffer, 0, 4)
  126.            Dim size As Integer = BitConverter.ToInt32(buffer, 0)
  127.            stream.Position = 0
  128.            Dim decompressed(size - 1) As Byte
  129.            gZip.Read(decompressed, 0, size)
  130.            gZip.Dispose()
  131.            stream.Dispose()
  132.            File.WriteAllBytes(destDirectory & "\" & destFileName, decompressed)
  133.            Return True
  134.        Catch ex As Exception
  135.            Return False
  136.        End Try
  137.    End Function
  138.    Public Function CompressFile(ByRef file As String, ByRef destination As String) As String
  139.  
  140.        If IO.File.Exists(file) = False Then
  141.            Return "Please specify a valid file (and path) to compress"
  142.            Exit Function
  143.        Else
  144.            If IO.Directory.Exists(destination) = False Then
  145.                Return "Please provide a destination location"
  146.                Exit Function
  147.            End If
  148.        End If
  149.        Try
  150.            Dim name As String = Path.GetFileName(file)
  151.            Dim source() As Byte = System.IO.File.ReadAllBytes(file)
  152.            Dim compressed() As Byte = ConvertToByteArray(source)
  153.            System.IO.File.WriteAllBytes(destination & "\" & name & ".zip", compressed)
  154.            Return "Compression Successful!"
  155.        Catch ex As Exception
  156.            Return "Compression Error: " & ex.ToString()
  157.        End Try
  158.  
  159.    End Function
  160.  
  161.  
  162.    Public Function ConvertToByteArray(ByVal source() As Byte) As Byte()
  163.  
  164.        Dim memoryStream As New MemoryStream()
  165.        Dim gZipStream As New GZipStream(memoryStream, CompressionMode.Compress, True)
  166.        gZipStream.Write(source, 0, source.Length)
  167.        gZipStream.Dispose()
  168.        memoryStream.Position = 0
  169.        Dim buffer(memoryStream.Length) As Byte
  170.        memoryStream.Read(buffer, 0, buffer.Length)
  171.        memoryStream.Dispose()
  172.        Return buffer
  173.  
  174.    End Function
  175.  
  176. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement