Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. ###########################################################################
  2. # NAME: Restore Backup With Dynamic Filename
  3. #
  4. # COMMENT: Using Ola's backup script creates dynamic file names of the .bak file
  5. # extension. This script gets the file name and uses it in the sqlcmd restore
  6. ###########################################################################
  7.  
  8. #Variables
  9. $folder = "\\server\networkShare\"
  10. $sqlInstance = "SQLInstance"
  11. $backupFile = Get-ChildItem $folder | Where {$_.Extension -eq ".bak"} | Sort-Object CreationTime | select -last 1
  12. $backupFilePath = $folder + $backupFile
  13.  
  14. #Build restore string
  15. [string] $dbCommand = 'sqlcmd /Q "RESTORE DATABASE [DB] ' +
  16. "FROM DISK = N'$backupFilePath' " +
  17. "WITH FILE = 1, " +
  18. "MOVE N'DB_Data' TO N'C:\Data\DB_Data.MDF', " +
  19. "MOVE N'DB_Log' TO N'C:\Data\DB_Log.LDF'," +
  20. 'NOUNLOAD, REPLACE"'
  21.  
  22. #Run SQLCMD to restore backup
  23. Invoke-Expression $dbCommand
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement