Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Data.SqlClient
- Imports Microsoft.VisualBasic.Devices.Network
- Imports System.IO
- Imports System.IO.Compression
- Imports System.Net
- Imports System.Net.Mail
- Module Module1
- Const Line As String = "ControlChars.CrLf"
- Sub Main()
- Dim SQLConnection As SqlConnection
- Dim SQLCommands As SqlCommand
- Dim FTPConnection As New Microsoft.VisualBasic.Devices.Network
- Dim ParameterCommands As String = Interaction.Command
- Dim SQLDBhost, SQLDatabase, SQLUsername, SQLPassword As String
- Dim FTPHost, FTPUsername, FTPPassword, Operations, TempFile As String
- Dim SMTPHost, SMTPUser, SMTPPass, SMTPFROM, SMTPTo, SMTPLog As String
- Dim SQLSuccess As Boolean = False
- Dim Restore As Boolean = False
- If ParameterCommands = "" Then
- 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")
- Console.Read()
- Else
- SQLDBhost = Split(ParameterCommands, "-")(1).ToString
- SQLDatabase = Split(ParameterCommands, "-")(2).ToString
- SQLUsername = Split(ParameterCommands, "-")(3).ToString
- SQLPassword = Split(ParameterCommands, "-")(4).ToString
- FTPHost = Split(ParameterCommands, "-")(5).ToString
- FTPUsername = Split(ParameterCommands, "-")(6).ToString
- FTPPassword = Split(ParameterCommands, "-")(7).ToString
- Operations = Split(ParameterCommands, "-")(8).ToString
- SMTPHost = Split(ParameterCommands, "-")(9).ToString
- SMTPUser = Split(ParameterCommands, "-")(10).ToString
- SMTPPass = Split(ParameterCommands, "-")(11).ToString
- SMTPFROM = Split(ParameterCommands, "-")(12).ToString
- SMTPTo = Split(ParameterCommands, "-")(13).ToString
- 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
- Console.WriteLine("Some Parameters are missing!" & vbCrLf & "Usable Parameters are: -SQLDBhost-SQLDatabase Name-SQLUsername-SQLPassword-FTPServer-FTPUsername-FTPPassword-SMTPHost-SMTPUser-SMTPPass-SMTPFROM-SMTPTo")
- Else
- If UCase(Operations) = "BACKUP" Then
- Console.WriteLine("------EXECUTING BACKUP OPERATIONS------")
- SQLConnection = New SqlConnection("DATA SOURCE=" & SQLDBhost & ";user id=" & SQLUsername & ";password=" & SQLPassword)
- Console.WriteLine(SQLConnection.ConnectionString)
- TempFile = Microsoft.VisualBasic.FileIO.FileSystem.GetTempFileName
- Console.WriteLine(TempFile)
- SQLConnection.Open()
- If SQLConnection.State = ConnectionState.Open Then
- SQLCommands = New SqlCommand("BACKUP DATABASE " & SQLDatabase & " TO DISK='" & TempFile & "'")
- SQLCommands.Connection = SQLConnection
- Console.WriteLine(SQLCommands.CommandText)
- SQLCommands.ExecuteNonQuery()
- Console.WriteLine(SQLCommands.CommandText & " Executed!")
- SQLConnection.Close()
- Console.WriteLine(CompressFile(TempFile, Microsoft.VisualBasic.FileIO.FileSystem.GetParentPath(TempFile)))
- SQLSuccess = True
- If SQLSuccess = True Then
- Console.WriteLine("Uploading Database...")
- FTPConnection.UploadFile(TempFile & ".zip", "ftp://" & FTPHost & "/backup.zip", FTPUsername, FTPPassword)
- Console.WriteLine("Database Upload Complete...")
- Console.WriteLine("Press any key to exit...")
- Else
- Console.WriteLine("Could not execute Database backup! Please check your parameters!")
- End If
- End If
- Else
- If UCase(Operations) = "RESTORE" Then
- Console.WriteLine("------EXECUTING RESTORE OPERATIONS------")
- SQLConnection = New SqlConnection("DATA SOURCE=" & SQLDBhost & ";user id=" & SQLUsername & ";password=" & SQLPassword)
- Console.WriteLine(SQLConnection.ConnectionString)
- TempFile = Microsoft.VisualBasic.FileIO.FileSystem.GetTempFileName
- Console.WriteLine(TempFile)
- Console.WriteLine("Downloading backup...")
- FTPConnection.DownloadFile("ftp://" & FTPHost & "/backup.zip", TempFile & ".zip", FTPUsername, FTPPassword)
- Console.WriteLine("Download Complete...")
- Console.Write("Decompressing Data...")
- DecompressFile(TempFile & ".zip", "backup.bak", Microsoft.VisualBasic.FileIO.FileSystem.GetParentPath(TempFile))
- Console.WriteLine("Done Decompressing Downloaded Backup...")
- SQLConnection.Open()
- If SQLConnection.State = ConnectionState.Open Then
- Console.WriteLine("Restoring Database...")
- SQLCommands = New SqlCommand("RESTORE DATABASE [" & SQLDatabase & "] FROM DISK=N'" & Microsoft.VisualBasic.FileIO.FileSystem.GetParentPath(TempFile) & "\backup.bak' WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 10")
- SQLCommands.Connection = SQLConnection
- Console.WriteLine(SQLCommands.CommandText)
- SQLCommands.ExecuteNonQuery()
- Console.WriteLine(SQLCommands.CommandText & " Executed Successfuly!")
- SQLConnection.Close()
- Restore = True
- If Restore = True Then
- SendMail(SMTPHost, SMTPUser, SMTPPass, SMTPFROM, SMTPTo, "BackupTool", "Database Restore was successful at : " & Date.Now)
- Else
- SendMail(SMTPHost, SMTPUser, SMTPPass, SMTPFROM, SMTPTo, "BackupTool", "Database Restore was not successful at : " & Date.Now)
- End If
- End If
- Else
- End If
- End If
- End If
- End If
- End Sub
- 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
- Dim SMTPC As New SmtpClient(Host)
- Dim Mailmessage As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage
- SMTPC.Credentials = New Net.NetworkCredential(Username, Password)
- Mailmessage.From = New MailAddress(FromMail)
- Mailmessage.To.Add(ToMail)
- Mailmessage.Subject = Subject
- Mailmessage.IsBodyHtml = True
- Mailmessage.Body = Body
- Mailmessage.Attachments.Add("
- SMTPC.Send(Mailmessage)
- SendMail = True
- End Function
- Public Function DecompressFile(ByRef inputFileName As String, ByRef destFileName As String, ByRef destDirectory As String) As Boolean
- Try
- Dim stream As New MemoryStream(File.ReadAllBytes(inputFileName))
- Dim gZip As New GZipStream(stream, CompressionMode.Decompress)
- Dim buffer(3) As Byte
- stream.Position = stream.Length - 5
- stream.Read(buffer, 0, 4)
- Dim size As Integer = BitConverter.ToInt32(buffer, 0)
- stream.Position = 0
- Dim decompressed(size - 1) As Byte
- gZip.Read(decompressed, 0, size)
- gZip.Dispose()
- stream.Dispose()
- File.WriteAllBytes(destDirectory & "\" & destFileName, decompressed)
- Return True
- Catch ex As Exception
- Return False
- End Try
- End Function
- Public Function CompressFile(ByRef file As String, ByRef destination As String) As String
- If IO.File.Exists(file) = False Then
- Return "Please specify a valid file (and path) to compress"
- Exit Function
- Else
- If IO.Directory.Exists(destination) = False Then
- Return "Please provide a destination location"
- Exit Function
- End If
- End If
- Try
- Dim name As String = Path.GetFileName(file)
- Dim source() As Byte = System.IO.File.ReadAllBytes(file)
- Dim compressed() As Byte = ConvertToByteArray(source)
- System.IO.File.WriteAllBytes(destination & "\" & name & ".zip", compressed)
- Return "Compression Successful!"
- Catch ex As Exception
- Return "Compression Error: " & ex.ToString()
- End Try
- End Function
- Public Function ConvertToByteArray(ByVal source() As Byte) As Byte()
- Dim memoryStream As New MemoryStream()
- Dim gZipStream As New GZipStream(memoryStream, CompressionMode.Compress, True)
- gZipStream.Write(source, 0, source.Length)
- gZipStream.Dispose()
- memoryStream.Position = 0
- Dim buffer(memoryStream.Length) As Byte
- memoryStream.Read(buffer, 0, buffer.Length)
- memoryStream.Dispose()
- Return buffer
- End Function
- End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement