Windows Vista Forums

Backup all databases on a server
  1. #1


    dba Guest

    Backup all databases on a server

    Hello,

    I'm having a problem putting together a script to backup all
    databases on a server. I tried something like this, it will work for
    the first pass. But then fail on the second. I realize that it has to
    do with the creation of another
    Microsoft.SqlServer.Management.Smo.BackupDeviceItem but I'm not sure
    how to address this?



    [System.Reflection.Assembly]::LoadFile("C:\Program Files\Microsoft SQL
    Server\90\SDK\Assemblies\Microsoft.SqlServer.Smo.dll") | out-null
    [System.IO.Directory]::CreateDirectory("C:\temp") | out-null
    $srv=New-Object "Microsoft.SqlServer.Management.Smo.Server"
    "575ma3123581"
    $bck=new-object "Microsoft.SqlServer.Management.Smo.Backup"
    $bck.Action = 'Database'

    foreach([Microsoft.SqlServer.Management.Smo.Server.Database]$db in
    $srv.Databases )
    {

    $fil=new-object "Microsoft.SqlServer.Management.Smo.BackupDeviceItem"
    $fil.DeviceType='File'
    $fil.Name=[System.IO.Path]::Combine("\\AFileServer\sql
    \DatabaseServerHostName\DataBackup", $db.Name +".bak")

    $bck.Devices.Add($fil)
    $bck.Database= $db.Name
    $bck.SqlBackup($srv)

    }

    write-host "Backup of MyDatabase done"

      My System SpecsSystem Spec

  2. #2


    Marco Shaw [MVP] Guest

    Re: Backup all databases on a server

    dba wrote:

    > Hello,
    >
    > I'm having a problem putting together a script to backup all
    > databases on a server. I tried something like this, it will work for
    > the first pass. But then fail on the second. I realize that it has to
    > do with the creation of another
    > Microsoft.SqlServer.Management.Smo.BackupDeviceItem but I'm not sure
    > how to address this?
    Actually, your code looks good. I can test it tomorrow. You use
    new-object in each run, which should create a new instance just like you
    need.

    Marco

      My System SpecsSystem Spec

  3. #3


    RichS [MVP] Guest

    Re: Backup all databases on a server

    I think the problem is that on the second pass you are trying to create a
    backup device that already exists. This is how I've done backups using SMO

    ## load SMO assemblies
    ## use $null to prevent display of assembly load information
    $null =
    [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo")
    $null =
    [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum")
    $null = [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")

    clear-host

    ## set SMO variable
    $Smo = "Microsoft.SqlServer.Management.Smo."

    $server = new-object ($Smo + "Server") "SQL1"

    ## databse to back up
    $bkup = new-object ($smo + "Backup")
    $bkup.Database = "AdventureWorks"

    ## set backup device and type

    $date = get-date

    $date = $date -replace "/", "-" # replace / symbols in date part
    $date = $date -replace ":", "-" # replace : symbols in time part
    $date = $date -replace " ", "--Time-" # show time part
    $file = "C:\Backups\AdventureWorks" + "--" + $date + ".bak"

    $bkup.Devices.AddDevice($file,
    [Microsoft.SqlServer.Management.Smo.DeviceType]::File)
    $bkup.Action = [Microsoft.SqlServer.Management.Smo.BackupActionType]:atabase

    ## invoke backup
    $bkup.SqlBackup($server)


    --
    Richard Siddaway
    All scripts are supplied "as is" and with no warranty
    PowerShell MVP
    Blog: http://richardsiddaway.spaces.live.com/
    PowerShell User Group: http://www.get-psuguk.org.uk


    "Marco Shaw [MVP]" wrote:

    > dba wrote:

    > > Hello,
    > >
    > > I'm having a problem putting together a script to backup all
    > > databases on a server. I tried something like this, it will work for
    > > the first pass. But then fail on the second. I realize that it has to
    > > do with the creation of another
    > > Microsoft.SqlServer.Management.Smo.BackupDeviceItem but I'm not sure
    > > how to address this?
    >
    > Actually, your code looks good. I can test it tomorrow. You use
    > new-object in each run, which should create a new instance just like you
    > need.
    >
    > Marco
    >

      My System SpecsSystem Spec

Backup all databases on a server problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Exchange databases sam SBS Server 3 22 Feb 2010
Small Business Server backup hangs server... Ashley Draper SBS Server 4 29 Dec 2009
windows server backup - where to put / keep backup files NVVN Server General 1 19 Dec 2009
Learning C# & databases Patrick Ryan .NET General 2 26 Sep 2008
Iis,Asp and Access databases Dennis Milns Vista General 2 08 Jan 2008