Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Extract multiple gzip SQL BAK files and then restore them

$
0
0

I'm a complete Powershell newb here so apologies for that upfront. I'm trying to restore multiple SQL bak files and found this script: http://obligatorymoniker.wordpress.com/2013/02/26/restore-database-backup-files-in-a-folder-to-sql-server-using-powershell/#comment-54

Our BAK files are all compressed as GZ files and sent to a clone of the server. I'd like to use the script above in conjunction with this script I've pieced together to extract the GZ files containing the SQL backups. The other issue I have is I'm not sure how to pass the directory of the SQL backups to the script I found above on the ObligatoryMoniker script. I have left a message on the blog as well. Any help is greatly appreciated!

 

Script to extract GZ compressed BAK files:

$ZipFiles = Get-ChildItem D:\RestorefromDenver\Extracted -Recurse -Include *.gz

$ZipFiles.count | out-default

foreach ($ZipFile in $ZipFiles)

{

C:\7z.exe e -y -oD:\RestorefromDenver\Extracted $ZipFile.Name

}

 

Here is the whole SQL restore script from the ObligatoryMoniker blog:

 

function invoke-DatabaseRestore {
    param ([String]$SQLServer="(local)"$BackupPath, [String]$BackupFileFilter"")
    #load 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
     
    gci $BackupPath -Filter $BackupFileFilter select fullname | % {
        $backupFile = $_.FullName
 
        #we will query the database name from the backup header later
        $server New-Object "Microsoft.SqlServer.Management.Smo.Server" )$SQLServer
        $backupDevice New-Object("Microsoft.SqlServer.Management.Smo.BackupDeviceItem" ) ($backupFile"File")
        $smoRestore new-object"Microsoft.SqlServer.Management.Smo.Restore" )
        $backupDeviceFL *
 
        $DataPath = if ($server.Settings.DefaultFile.Length -gt 0 ) {$server.Settings.DefaultFile } else { $server.Information.MasterDBLogPath }
        $LogPath = if ($server.Settings.DefaultLog.Length -gt 0 ) {$server.Settings.DefaultLog } else { $server.Information.MasterDBLogPath }
 
        #restore settings
        $smoRestore.NoRecovery = $false;
        $smoRestore.ReplaceDatabase = $true;
        $smoRestore.Action = "Database"
        $smoRestore.PercentCompleteNotification = 10;
        $smoRestore.Devices.Add($backupDevice)
  
        #get database name from backup file
        $smoRestoreDetails $smoRestore.ReadBackupHeader($server)
  
        #display database name
        "Database Name from Backup Header : " +$smoRestoreDetails.Rows[0]["DatabaseName"]
  
        #give a new database name
        $smoRestore.Database = $smoRestoreDetails.Rows[0]["DatabaseName"]
 
        #Relocate each file in the restore to the default directory
        $smoRestoreFiles $smoRestore.ReadFileList($server)
 
        foreach ($File in $smoRestoreFiles) {
            #Create relocate file object so that we can restore the database to a different path
            $smoRestoreFile New-Object("Microsoft.SqlServer.Management.Smo.RelocateFile" )
  
            #the logical file names should be the logical filename stored in the backup media
            $smoRestoreFile.LogicalFileName = $File.LogicalName
 
            $smoRestoreFile.PhysicalFileName = $( if($File.Type -eq "L") {$LogPath} else {$DataPath} ) + "\" + [System.IO.Path]::GetFileName($File.PhysicalName)
            $smoRestore.RelocateFiles.Add($smoRestoreFile)
        }
        #restore database
        $smoRestore.SqlRestore($server)
  
    }
}

Viewing all articles
Browse latest Browse all 6937

Trending Articles