Guest User

Untitled

a guest
Oct 22nd, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. param ($bakpath, $dbname, $instance, $datapath, $logpath, [switch]$norecovery, [switch]$replace, [switch]$rollback, [switch]$standby, [switch]$test)
  2.  
  3. # Restore latest backup from wildcard path, optionally forcing out open connections
  4. # Used to restore backups copied to a standby or test server
  5. ""
  6.  
  7. # SQL 2008 PS module imports
  8. #[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
  9. #if ((get-pssnapin sqlserverprovidersnapin100 -ErrorAction "SilentlyContinue") -eq $NULL) { add-pssnapin sqlserverprovidersnapin110 }
  10. #if ((get-pssnapin sqlservercmdletsnapin100 -ErrorAction "SilentlyContinue") -eq $NULL) { add-pssnapin sqlservercmdletsnapin110 }
  11.  
  12. # SQL 2012 PS module imports
  13. Import-Module sqlps
  14.  
  15. $srv = new-object ('Microsoft.SqlServer.Management.Smo.Server') $instance
  16.  
  17. if (($bakpath -eq $null) -or ($dbname -eq $null)) {
  18. Write-Error "Usage: RestoreLastBak.ps1 -bakpath(file/wildcard) -dbname [-instance] [-datapath] [-logpath] [-norecovery] [-replace] [-rollback] [-test]"
  19. exit
  20. }
  21.  
  22. if ($datapath -eq $null) {
  23. $datapath = $srv.Information.MasterDBPath
  24. $logpath = $srv.Information.MasterDBLogPath
  25. }
  26. elseif ($logpath -eq $null) { $logpath = $datapath }
  27.  
  28. if (($datapath.Length -eq 0) -or ($logpath.Length -eq 0)) {
  29. Write-Error "Unable to determine restore paths. Must specify parameters -datapath, -logpath"
  30. exit
  31. }
  32.  
  33.  
  34. if ($instance -eq $null) { $instance = "." }
  35.  
  36. #$sqlver = Invoke-Sqlcmd -Query "SELECT @@VERSION" -ServerInstance $instance
  37. #$sqlver.Column1.ToString()
  38.  
  39. $bak = dir $bakpath | sort -prop LastWriteTime | select -last 1
  40. #"Last backup: $bak"
  41.  
  42. $go = [Environment]::NewLine + "GO" + [Environment]::NewLine
  43.  
  44. $sql = "RESTORE FILELISTONLY FROM DISK = '$bak'"
  45. $sql
  46.  
  47. $files = Invoke-Sqlcmd -Query $sql -ServerInstance $instance
  48.  
  49. $sql = ""
  50. if ($rollback) { $sql += "IF EXISTS(SELECT * FROM sys.databases WHERE [name] = '$dbname' AND [state_desc] = 'ONLINE') ALTER DATABASE [$dbname] SET SINGLE_USER WITH ROLLBACK IMMEDIATE" + $go }
  51. $sql += "RESTORE DATABASE [$dbname] FROM DISK = '$bak' WITH "
  52.  
  53. foreach ($file in $files) {
  54. $lname = $file.LogicalName
  55. $pname = $file.PhysicalName.Split("\")[-1]
  56. $ext = $pname.Split(".")[-1]
  57. $ftype = $file.Type
  58. " -> {0} ({1}: {2})" -f $lname, $ftype, $pname
  59.  
  60. if ($ftype -eq "D") { $sql += "MOVE N'$lname' TO N'$datapath\$dbname.$ext', " }
  61. elseif ($ftype -eq "L") { $sql += "MOVE N'$lname' TO N'$logpath\$dbname.$ext'" }
  62. }
  63. ""
  64. if ($norecovery) { $sql += ", NORECOVERY" }
  65. if ($replace) { $sql += ", REPLACE" }
  66. if ($standby) { $sql += ", STANDBY" }
  67. $sql += $go
  68.  
  69. if (($rollback) -and (!$norecovery)) { $sql += "ALTER DATABASE [$dbname] SET MULTI_USER" + $go }
  70. $sql
  71.  
  72. if (!$test) { Invoke-Sqlcmd -Query $sql -Verbose -ServerInstance $instance -QueryTimeout 65535 } # Timeout added to work around PS1 bug that does not respect unlimited query runtime
Add Comment
Please, Sign In to add comment