Public Class BackupManager
Dim srvSql As Server
Dim saveBackupDialog As SaveFileDialog = New SaveFileDialog()
Dim openBackupDialog As OpenFileDialog = New OpenFileDialog()
Dim dbCache As DatabaseCache = DatabaseCache.Instance
Private Function GetAppPath() As String
Return System.IO.Path.GetDirectoryName
(System.Windows.Forms.Application.ExecutablePath)
End Function
Private Sub Connect()
ServerConnection srvConn = New
ServerConnection(dbCache.SqlConnection)
srvSql = New Server(srvConn)
End Sub
Private Function GetDatabase(ByVal conn As SqlConnection) As String
Dim connArray() As String = conn.ConnectionString.Split(";"c)
Dim toMatch As String = "AttachDbFilename="
Dim match As String = Nothing
Dim item As String
For Each item In connArray
If item.StartsWith(toMatch) Then
match = item.Substring(toMatch.Length)
Exit For
End If
Next
If Not String.IsNullOrEmpty(match) Then
match = match.Replace("|DataDirectory|", GetAppPath())
Else
Throw New Exception(
"Could not extract database path from connection string")
End If
Return match
End Function
Public Function MakeBackup() As Boolean
' If there was a SQL connection created
If srvSql Is Nothing Then
Connect()
End If
If Not srvSql Is Nothing Then
Dim path As String = GetAppPath()
path = path += "\Backup"
If Not Directory.Exists(path) Then
Directory.CreateDirectory(path)
End If
saveBackupDialog.InitialDirectory = path
saveBackupDialog.DefaultExt = "bak"
' If the user has chosen a path
' where to save the backup file
If saveBackupDialog.ShowDialog() = DialogResult.OK Then
' Create a new backup operation
Dim bkpDatabase As Backup = New Backup()
' Set the backup type to a database backup
bkpDataMyBase.Action = BackupActionType.Database
' Set the database that we want to perform a backup on
bkpDataMyBase.Database = GetDatabase(dbCache.SqlConnection)
' Set the backup device to a file
Dim bkpDevice As BackupDeviceItem = New BackupDeviceItem(saveBackupDialog.FileName,DeviceType.File)
' Add the backup device to the backup
bkpDataMyBase.Devices.Add(bkpDevice)
' Perform the backup
bkpDataMyBase.SqlBackup(srvSql)
Else
Return False
End If
Else
Throw New Exception("Could not connect to dataMyBase.")
End If
Return True
End Function
Public Sub RestoreBackup()
'database must first be unloaded before calling this.
' If there was a SQL connection created
If srvSql Is Nothing Then
Connect()
End If
If Not srvSql Is Nothing Then
openBackupDialog.InitialDirectory = "./Backup"
openBackupDialog.DefaultExt = "bak"
' If the user has chosen the file from which he wants the database to be restored
If openBackupDialog.ShowDialog() = DialogResult.OK Then
' Create a new database restore operation
Dim rstDatabase As Restore = New Restore()
' Set the restore type to a database restore
rstDataMyBase.Action = RestoreActionType.Database
' Set the database that we want to perform the restore on
rstDataMyBase.Database = GetDatabase(dbCache.SqlConnection)
' Set the backup device from which we want to restore, to a file
Dim bkpDevice As BackupDeviceItem = New BackupDeviceItem(openBackupDialog.FileName,DeviceType.File)
' Add the backup device to the restore type
rstDataMyBase.Devices.Add(bkpDevice)
' If the database already exists, replace it
rstDataMyBase.ReplaceDatabase = True
' Perform the restore
Try
rstDataMyBase.SqlRestore(srvSql)
Catch ex As FailedOperationException
Log.WriteLine("")
Log.WriteLine("Error: (BackupManager.RestoreBackup)")
Log.Write(ex)
Throw New Exception("Error while restoring backup.", ex)
End Try
End If
Else
Throw New Exception("Could not connect to dataMyBase.")
End If
End Sub
End Class