Para crear el backup necesitamos los siguientes espacios de nombres, ademas del servidor de datos, la base de datos, el nombre del backup.

Imports System.Text
Imports System.Data.SqlClient
El boton examinar nos permite seleccionar una carpeta donde crearemos el backup.

Private Sub btnExaminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExaminar.Click
Dim Directorio As New FolderBrowserDialog
If Directorio.ShowDialog = Windows.Forms.DialogResult.OK Then
Me.txtDirPathBackup.Text = Directorio.SelectedPath
End If
End Sub
Una vez ingresamos el nombre del servidor de datos, la base de datos, la ruta donde se guardar el archivo .bak del backup que haremos y la descripcion del backup damos click en el boton crear backup.
En el evento click del mismo evaluaremos la informacion requerida, para que se vea mejor pueden agregar un errorprovider.

Private Sub btnBackup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBackup.Click
If cmbServidor.Text <> “” Then
If Me.cboBaseDatos.Text <> “” Then
If txtDirPathBackup.Text <> “” Then
If txtNom_Backup.Text <> “” Then
If txtDescrip_Backup.Text <> “” Then
If crear_backup() = True Then
MessageBox.Show(”Backup creado satisfactoriamnete”, “OK”, MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show(”Error al crear el Backup”, “Error”, MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Else
MessageBox.Show(”Ingrese una descripcion del Backup”, “Aviso”, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
Else
MessageBox.Show(”Ingrese el nombre del Backup”, “Aviso”, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
Else
MessageBox.Show(”Seleccione la ruta donde se creara el Backup”, “Aviso”, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
Else
MessageBox.Show(”Seleccione la Base de Datos”, “Aviso”, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
Else
MessageBox.Show(”Ingrese el Nombre del Servidor de Datos SQL”, “Aviso”, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
End Sub
Una vez ingresamos los datos requeridos llamaremos la funcion backup(), la cual nos permite configurar un SqlConnectionStringBuilder para configurar la conexion, asi mismo como un StringBuilder para ingresar la sentencia del backup que ejecutaremos para hacer el backup.

Private Function crear_backup() As Boolean
Dim conecsb As New SqlConnectionStringBuilder
conecsb.DataSource = Me.cmbServidor.Text
conecsb.InitialCatalog = “master”
conecsb.IntegratedSecurity = True
If txtDirPathBackup.Text.Length <> 3 Then
txtDirPathBackup.Text = txtDirPathBackup.Text + “\” + txtNom_Backup.Text + “.bak”
Else
txtDirPathBackup.Text = txtDirPathBackup.Text + txtNom_Backup.Text + “.bak”
End If
Using con As New SqlConnection(conecsb.ConnectionString)
Try
con.Open()
Dim sCmd As New StringBuilder
sCmd.Append(”BACKUP DATABASE [" + cboBaseDatos.Text + "] TO DISK = N’” + txtDirPathBackup.Text + “‘ “)
sCmd.Append(”WITH DESCRIPTION = N’” + txtDescrip_Backup.Text + “‘, NOFORMAT, NOINIT, “)
sCmd.Append(”NAME = N’” + txtNom_Backup.Text + “‘, SKIP, NOREWIND, NOUNLOAD, STATS = 10″)
Dim cmd As New SqlCommand(sCmd.ToString, con)
cmd.ExecuteNonQuery()
crear_backup = True
Catch ex As Exception
crear_backup = False
MessageBox.Show(ex.Message)
Finally
con.Close()
End Try
End Using
End Function
Una vez se ejecuta satisfactoriamente el backup vemos que, en la ubicacion donde decidimos guardar el backup, aparece nuestro archivo .bak con el nombre que le dimos.
