Restore-LatestBackup.ps1

  • Comments posted to this topic are about the item Restore-LatestBackup.ps1

  • Thank you

  • I put together this script to copy a production database backup to a development server and restore it. The job will run at night, and keeps the development data parallel to production. We are using SQL Server 2012 SP2 (build 11.0.5058).

    [font="Courier New"]import-module SQLPS -DisableNameChecking

    #

    #knock off existing connections

    Invoke-Sqlcmd "ALTER DATABASE DB_VIEWS SET OFFLINE WITH ROLLBACK IMMEDIATE;"

    Invoke-Sqlcmd "ALTER DATABASE DB_VIEWS SET ONLINE;"

    #

    # find the latest full backup to be restored

    $a = Get-ChildItem Filesystem::\\RC-DBPROD\Backup\DB_VIEWS\DB_VIEWS*.bak | sort LastWriteTime | select -last 1

    #

    # production DB uses different disks then this development server

    $RelocateData = New-Object Microsoft.SqlServer.Management.Smo.RelocateFile("DB_VIEWS", "C:\MSSQL\DB_VIEWS.mdf")

    $RelocateLog = New-Object Microsoft.SqlServer.Management.Smo.RelocateFile("DB_VIEWS_log", "C:\MSSQL\DB_VIEWS_log.ldf")

    Restore-SqlDatabase -ServerInstance RC-DBUPD58 -Database DB_VIEWS -BackupFile $a -RelocateFile @($RelocateData,$RelocateLog) -replacedatabase

    [/font]

  • Thanks for the script.

  • alan.spillert (8/27/2014)


    I put together this script to copy a production database backup to a development server and restore it. The job will run at night, and keeps the development data parallel to production. We are using SQL Server 2012 SP2 (build 11.0.5058).

    [font="Courier New"]import-module SQLPS -DisableNameChecking

    #

    #knock off existing connections

    Invoke-Sqlcmd "ALTER DATABASE DB_VIEWS SET OFFLINE WITH ROLLBACK IMMEDIATE;"

    Invoke-Sqlcmd "ALTER DATABASE DB_VIEWS SET ONLINE;"

    #

    # find the latest full backup to be restored

    $a = Get-ChildItem Filesystem::\\RC-DBPROD\Backup\DB_VIEWS\DB_VIEWS*.bak | sort LastWriteTime | select -last 1

    #

    # production DB uses different disks then this development server

    $RelocateData = New-Object Microsoft.SqlServer.Management.Smo.RelocateFile("DB_VIEWS", "C:\MSSQL\DB_VIEWS.mdf")

    $RelocateLog = New-Object Microsoft.SqlServer.Management.Smo.RelocateFile("DB_VIEWS_log", "C:\MSSQL\DB_VIEWS_log.ldf")

    Restore-SqlDatabase -ServerInstance RC-DBUPD58 -Database DB_VIEWS -BackupFile $a -RelocateFile @($RelocateData,$RelocateLog) -replacedatabase

    [/font]

    Thanks for the extra insight.

  • Nice, thanks! Been meaning to update this guy to use the newer sqlps module instead.

  • At Luke's powershell script , at bottom lines of the code as below, if I want the source server and destination server has different data file physical path how can we rewrite below codes? For example the source server physical path is E:\MSSQL\Data\
    The destination is E:\MSSQL12\Data

    # Loop through backup file and configure data and log file names.

    $logicalFilename = $null;

    $physicalFilename = $null;

    foreach ($dbFile IN $backupFiles.Rows){

    $logicalFilename = $dbFile.LogicalName;

    $physicalFilename = $dbFile.PhysicalName.Replace($sourceInstance, $destInstance).Replace($dbName, $newDbName);

    $smoMoveFile = New-Object('Microsoft.SqlServer.Management.Smo.RelocateFile');

    $smoMoveFile.LogicalFileName = $logicalFilename;

    $smoMoveFile.PhysicalFileName = $physicalFilename;

    $smoNewDB.RelocateFiles.Add($smoMoveFile);

    }

    #Restore db

    $smoNewDB.SqlRestore($destServerConnection);

  • sqlfriends - Monday, September 25, 2017 4:00 PM

    At Luke's powershell script , at bottom lines of the code as below, if I want the source server and destination server has different data file physical path how can we rewrite below codes? For example the source server physical path is E:\MSSQL\Data\
    The destination is E:\MSSQL12\Data

    You could add another replace() to the line that sets the physical filename, like this:  
    $physicalFilename = $dbFile.PhysicalName.Replace($sourceInstance, $destInstance).Replace($dbName, $newDbName).Replace('MSSQL', 'MSSQL12');

  • Thank you, that works!

    Thanks for the script, this is the one of the best scripts used to store db to another sever.

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply