Library tutorials & articles

Using SQL-DMO To Backup and Verify a Database

Backing Up

SQLDMO.BackupDevice

The SQLDMO.BackupDevice object will provide a list of backup devices currently installed on the server. I recommend using backup devices to backup your information, they will allow you to use the verify functionality of SQL-DMO to check the validity of the backup. The code snippet below uses this method to get a list of devices from the server.

  <%
  
    Dim
   srv
  Dim objDevice
  Set srv = Server.CreateObject("SQLDMO.SQLServer")
  srv.LoginTimeout = 15 
  srv.Connect "servername", "username", "password"
  Set objDevice = Server.CreateObject("SQLDMO.BackupDevice")
 
  For Each objDevice In srv.BackupDevices
    Response.Write objDevice.Name + "<BR>"
  Next%>

SQLDMO.Backup

This is basically the core object we will use to backup the database. It has a vast variety of properties that can be used to provide the same level of backup functionality as the SQL enterprise manager. Lets take a moment to discuss the properties used in the sample application.

  1. BackupSetName - A name for the backup.
  2. Database - The database you want to backup.
  3. Action - Either full or incremental. There are more options; however, the sample application uses only two.
  4. BackupSetDescription - A description of the backup.
  5. Files - Use the file option if you do not want to backup to a device. It is nothing more than a file path and name for the backup that will reside on the server. For example C:\pubs.bak. If you use a file, the device option must be blank.
  6. Devices - A list of backup devices created on the server. If you use a device the files option must be blank.
  7. TruncateLog - Options used to backup or truncate transactions logs. The following options are given:
    • NoLog - Records referencing committed transactions are removed. Transaction log is not backed up.
    • NoTruncate - Transaction log is backed up. Records referencing committed transactions are not removed, providing a point-in-time image of the log.
    • Truncate - Transaction log is backed up. Records referencing committed transactions are removed.
  8. Initialize - If set to true then this backup becomes the first of the device overwriting any other backup media.

Lets take a look at the backup.asp within the sample application.

  <%@ Language=VBScript %>
  <
  HTML
  >
  <
  BODY
  >
<!--contains all the login information -->
<!--#include file=login.asp -->
<%Dim objBackup 
  'creating the backup objectset objBackup      = Server.CreateObject("SQLDMO.Backup")
  'setting the properties
  objBackup.BackupSetName  = Request("fname")
  objBackup.Database       = Request("fdatabase")
  objBackup.Action         = Request("fAction")
  objBackup.BackupSetDescription = Request("fdescription")
  objBackup.Files        = Request("fbackupfile")
  objBackup.Devices      = Request("fdevice")
  objBackup.TruncateLog  = Request("flog")
  objBackup.Initialize   = Request("finit")
  'backing up the database
  objBackup.SQLBackup srv
  'disconnecting from the server
  srv.disconnect
  'clean upset srv = nothingset objBackup = Nothing%><P>
The backup was started, use the <AHREF="devices.asp">verify</A>
option to see if it completed successfully.
<AHREF="default.asp">Click here</A> to return.
</P></BODY></HTML>

Comments

  1. 06 Jun 2005 at 11:11

    here  the solution for Backup the database in VC++:




    int backup::OnButton1()


    {
       HRESULT hr;
       if FAILED(hr = CoInitialize (NULL))
       {
           //_tprintf(TEXT("CoInitialize Failed\n"));
           return (0);
       }


       LPSQLDMOSERVER pSQLServer = NULL;


       if FAILED(hr = CoCreateInstance(
           CLSIDSQLDMOServer,
           NULL,
           CLSCTX
    INPROCSERVER,
           IID
    ISQLDMOServer,
           (LPVOID*)&pSQLServer))
       {
           //_tprintf(TEXT("CoCreateInstance Failed\n"));
           return (0);
       }


       pSQLServer->SetLoginTimeout(10);
       pSQLServer->SetLoginSecure(TRUE);
       
       if FAILED(hr = pSQLServer->Connect((L"ws-45"),(L"sa"),(L"")))
       {
           AfxMessageBox("Backup connection Failed");
           return DisplayError();
       }


       else
       {
           LPSQLDMODATABASES    pDatabases = NULL;
           LPSQLDMODATABASE    pDatabase = NULL;
           LPSQLDMOFILEGROUPS  pFileGroups = NULL;
           LPSQLDMOFILEGROUP    pFileGroup = NULL;
           LONG                lCount = 0;
           LPSQLDMODBFILE        ppDBFile = NULL;
           LPLONG                pRetVal = NULL;
           long    mdatasize;
           CString temp;
           
           float  m
    datasizeMB;
           CString datasize;


           SQLDMOBSTR        strDB;
       //    long *strDB;
       
    //        char* m
    str = new char[100];


           //pSQLServer->GetDatabaseCount(&lCount);


           hr = pSQLServer->GetDatabases(&pDatabases);


           for (LONG i = 0; i < 1; i++)
           {
               pDatabases->GetItemByOrd(i, &pDatabase);


               pDatabase->GetName(&strDB);
               
               pDatabase->Release();
               pDatabase->GetSpaceAvailableInMB(&m_datasizeMB);
               pDatabase->GetFileGroups(&pFileGroups);


           
               pDatabase->GetSize(&m_datasize);


           //    pDatabase->GetFileGroupByName(_T("DOTS"),&pFileGroup);
           //    pFileGroup->GetDBFileByOrd(0,&ppDBFile);


           //    temp = mdatasize;
               datasize.Format("%d",m
    datasize);
               temp.Format("%d",mdatasizeMB);
               SetDlgItemText(IDC
    EDIT1,datasize);
               //AfxMessageBox(datasize);
               
               //AfxMessageBox(temp);
               
               //    sprintf(mstr,"%lf",msize);


           
           
           //    wprintf(T("database %s"), strDB);
           //    wprintf(
    T(" Total size  %i \n"), msize);
           //    wprintf(
    T("Free space %lf \n"), m_sizeMB);
           }
           
                       
    }


       LPSQLDMOBACKUP pSQLBackup = NULL;


       if FAILED(hr = CoCreateInstance(
           CLSIDSQLDMOBackup,
           NULL,
           CLSCTX
    INPROCSERVER,
           IID
    ISQLDMOBackup,
           (LPVOID*)&pSQLBackup))
       {
       //    _tprintf(TEXT("CoCreateInstance Failed\n"));
           return (0);
       }


       LPCONNECTIONPOINTCONTAINER pMyConnectionPointContainer;
       LPCONNECTIONPOINT pMyConnectionPoint;


       CMyBackupSink* pMyBackupSink = new CMyBackupSink();
       pMyBackupSink->AddRef();
       
       if (!pMyBackupSink)
       {
           return(0);
       }


       DWORD dwCookie;


       // ask the connectable object (pSQLBackup) about its outgoing interface,
       // by using QI on IIDIConnectionPointContainer. If fails, this object
       // does not support outgoing interface
       //
       if FAILED(pSQLBackup->QueryInterface(
           IID
    IConnectionPointContainer,
           (LPVOID FAR*) &pMyConnectionPointContainer))
       {
           return DisplayError();
       }


       // find the specific outgoing interface IID_ISQLDMOBackupSink and retrieve
       // a pointer to the connectionpoint object. If fails, the object does not
       // support this outgoing interface


       if FAILED(pMyConnectionPointContainer->FindConnectionPoint(
           IID_ISQLDMOBackupSink,
           (LPCONNECTIONPOINT FAR*)&pMyConnectionPoint))
       {
           return DisplayError();
       }


       // establish the connection between the Sink and the ConnectionPoint object.
       // Retrieve a key (cookie) value for terminating the connection later. If
       // this fails, the Sink and ConnectionPoint object do not support the same interface
       
       if (S_OK != (hr = pMyConnectionPoint->Advise((LPUNKNOWN)pMyBackupSink, &dwCookie)))
       {
           return DisplayError();
       }


       if FAILED(hr = pSQLBackup->SetFiles((L"e:\test123.dmp") ))
       {
           return DisplayError();
       }


       if FAILED(hr = pSQLBackup->SetDatabase((L"dots")))
       {
           return DisplayError();
       }


    //    hr = pSQLBackup->EXEC sp_spaceused ;


       if FAILED(hr = pSQLBackup->SetTruncateLog(SQLDMOBackupLogTruncateOnly))
       {
           return DisplayError();
       }



     

  2. 29 Jan 2005 at 14:28

    Hi


    I tried your code, seems it is not working for remote database backup.
    do u have any update version for taking backups.


    thanks

  3. 01 Jan 1999 at 00:00

    This thread is for discussions of Using SQL-DMO To Backup and Verify a Database.

Leave a comment

Sign in or Join us (it's free).

AddThis

Related discussion

Related podcasts

  • Stack Overflow: Podcast #28

    This is the twenty-eighth episode of the StackOverflow podcast, where&#10;Joel and Jeff discuss Windows Azure, SQL Server 2008 full text search, Bayesian filtering, porn detection, and project management &mdash; among other things. &#10;&#10;Jeff met the inestimable Joey DeVilla aka Accordion Guy...

Related jobs

Events coming up

  • Jun 16

    Code Generation 2009

    Cambridge, United Kingdom

    A developer event with a practical focus on helping people get to grips with code generation tools and technologies.