Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

This blog incorporates articles about ASP.Net 3.5 and 4.0. Also good amount of concentration is laid on .net projects,book reviews,C# articles,jobs in present industry and some general topics.
Browse by Tags · View All
ARTICLE 61
SQL Server 31
TSQL 30
C# 16
ASP.Net 11
JumpStart 8
GENERAL 8
ASP.Net MVC 7
C# 4.0 6
ASP.Net 4.5 5

Archive · View All
December 2012 22
July 2012 12
May 2012 8
January 2013 7
April 2013 4
March 2013 4
February 2013 4
January 2012 4
August 2012 2
May 2013 1

Rami Vemula's Blog

Restore a Database using PowerShell

Jan 7 2012 12:00AM by rami   

In the last post I narrated a PowerShell script which can backup a database – Backup a Database using PowerShell. In this post I want to show how to restore a Database from a backup file using PowerShell.

The following code performs a restore operation –

 

#Loading required assemblies
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended") | Out-Null
[Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo") | Out-Null
[Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum") | Out-Null

#Setting up arguments to variables
$Server = $args[0];
$BackupFile = $args[1];

#Handle Errors (if in case)
Trap
{
  $err = $_.Exception;
  while ( $err.InnerException )
  {
    $err = $err.InnerException;
    write-output ("Oops!!! There is a Problem -> " + $err.Message);
  }
  return;
}

#If arguments are insufficient
if($args.count -ne 2)
{
	write-output "Usage:"
	write-output 'Syntax : "ServerName" "Backup file path to use"'
	write-output 'Example : "Rami-Server" "C:\BackUp\SampleDB_full_2011-10-06-04-04-37.bak"'
    return;
};

if(!(Test-Path $BackupFile))
{
    Write-Output ("InValid Backup File Path");
    return;
}

#Create SMO Objects
$server = New-Object Microsoft.SqlServer.Management.Smo.Server $Server
$backupDevice = New-Object Microsoft.SqlServer.Management.Smo.BackupDeviceItem $BackupFile, "File"
$Restore = New-object Microsoft.SqlServer.Management.Smo.Restore

#Set properties for Restore
$Restore.NoRecovery = $false;
$Restore.Devices.Add($backupDevice)
$RestoreDetails = $Restore.ReadBackupHeader($server) 

#Set Database Name
$Restore.Database = Get-ChildItem $BackupFile | % {$_.BaseName}

#Set Relocation Files
$RestoreDBFile = New-Object Microsoft.SqlServer.Management.Smo.RelocateFile
$RestoreLog = New-Object Microsoft.SqlServer.Management.Smo.RelocateFile

#Set Logical and Physical File Names
$RestoreDBFile.LogicalFileName = $RestoreDetails.Rows[0]["DatabaseName"]
$RestoreDBFile.PhysicalFileName = $server.Information.MasterDBPath + "\" + $Restore.Database + ".mdf"

$RestoreLog.LogicalFileName = $RestoreDetails.Rows[0]["DatabaseName"] + "_Log"
$RestoreLog.PhysicalFileName = $server.Information.MasterDBLogPath  + "\" + $Restore.Database + "_Log.ldf"

Write-Output ("Restore Started at: " + (Get-Date -format yyyy-MM-dd-HH:mm:ss)); 

$Restore.RelocateFiles.Add($RestoreDBFile)
$Restore.RelocateFiles.Add($RestoreLog)

#Restore database
$Restore.SqlRestore($server)

Write-Output ("Restore Ended at: " + (Get-Date -format yyyy-MM-dd-HH:mm:ss));
Write-Output ("Restore completed with Database Name " + $Restore.Database);

 

Code was self explanatory, but I would like to narrate the common steps which I took in constructing this code –

  1. Above presented script takes 2 parameters as inputs – "ServerName" and "Backup File path".
  2. Then we perform some sanity checks for the inputs – File Existence etc.
  3. Finally we perform Database Restore using Microsoft.SqlServer.Management.Smo.Restore and Microsoft.SqlServer.Management.Smo.RelocateFile (SQL Server Management Object – collection of objects that are designed for programming all aspects of managing Microsoft SQL Server), and then corresponding Transaction log restore.
  4. We create a Restore object, then we initialize some properties for it. We also set logical and physical file names for the DB and Log files which are to be restored. Finally we use RelocateFile Object to relocate a file and then restore it.
  5. In case of any error in the complete flow, there is Trap block which catches the exception and notifies PowerShell Console.

Following Screenshots shows you the execution results and backup files –

Execution in Console

powershell_restore_execution

Now you can go and check in SSMS, newly restored database will be listed under databases node.


Republished from Rami Vemula [15 clicks].  Read the original version here [1 clicks].

rami
534 · 0% · 70
1
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

Your Comment


Sign Up or Login to post a comment.

"Restore a Database using PowerShell" rated 5 out of 5 by 1 readers
Restore a Database using PowerShell , 5.0 out of 5 based on 1 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]