Wednesday, June 4, 2014

NAV 2013 R2 - Restore a Multi Tenant Environment.

Hi all,

We had seen how to create a Multi Tenant Environment & How to Backup the Complete Environment with NAV 2013 R2 in the respective articles.

But what if i want to do a restore backup of the Multi Tenant Environment in a Different Server. How can i achieve it as in my SQL now i have multiple databases.

So let's see how we can achieve it.

** References - Waldo Blog. Thanks to Waldo for sharing the Information, i have tried to append the steps listed in the Video.

Scenario -
I took backup of My Multi tenant Environment Created with Multiple Tenants (5) and Application Objects. I want to Restore the whole Environment and Restore it on New Server.



Steps - (Before Starting the process, lets see the environment creation steps)x

1. Created a Service MultiTenantRestore as shown Below.



2. Copy the databases backup on the new server where you want to restore as shown below.



3. For Restore all the Tenant and Application database from a Specified Location, i have used Below Powershell Command.
* Powershell should be Run as Administrator. How to Open Powershell.

--------------------------------------------------------------------------------------
# Powershell Command to Restore Backup of Tenant and Application Database.
# Author - Waldo
# Modified By - Saurav Dhyani
# Blog - saurav-nav.blogspot.com
# Parameters Need to Specify Before Running Command -
# Backups - Directory Path where backups have been placed with file extension *.bak. (Currently Set to D:\Tenants Backups\*.bak)
# fileloc - Directory Path for data file to get restored. (D:\Tenants\data)
# logloc -  Directory Path for log file to get restored. (D:\Tenants\log)

$Backups = dir 'D:\Tenants Backups\*.bak'
$fileloc = 'D:\Tenants\data'
$logloc  = 'D:\Tenants\log'

Set-NAVServerInstance MultiTenantRestore -stop

foreach ($BackupFile in $Backups) {
 'Restoring ' + $BackupFile.Directory + '\' + $BackupFile.Name

 $server = New-Object Microsoft.SqlServer.Management.Smo.Server localhost
 $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.ReplaceDatabase = $true;
 $Restore.Devices.Add($backupDevice)
 $RestoreDetails = $Restore.ReadBackupHeader($server)
 $Restore.Database = Get-ChildItem $BackupFile | % {$_.BaseName}

 $dbname = Get-ChildItem $BackupFile | % {$_.BaseName}
 $files = $restore.ReadFileList($server)

 foreach ($file in $files)
 {
  $rsfile = new-object Microsoft.SqlServer.Management.Smo.RelocateFile
  $rsfile.LogicalFileName = $file.LogicalName


  $dbfile = $fileloc + '\'+ $dbname + '_Data.mdf'
  $logfile = $logloc + '\'+ $dbname + '_Log.ldf'

  if ($file.Type -eq 'D') {
    $rsfile.PhysicalFileName = $dbfile
  }
  else {
    $rsfile.PhysicalFileName = $logfile
  }
 $restore.RelocateFiles.Add($rsfile)
 }

 $Restore.SqlRestore($server)
}
--------------------------------------------------------------------------------------

You can download the Powershell command from the Sky Drive Link. Link is available at end of the Post.

4. Let's Understand the Command.
At the Beginning of the Command we set two Parameters i.e -

1. $Backups = dir 'D:\Tenants Backups\*.bak' (Directory Path where backups have been placed with file extension *.bak.)
2. $fileloc = 'D:\Tenants\data' (Directory Path for data file to get restored.)
3. $logloc  = 'D:\Tenants\log' (Directory Path for log file to get restored.)

Next i stopped the Service Instance which i will be using to mount my Application.

Then For Every .bak file in the Backup Directory,  i used the SQLPS Restore Commandlet to Restore the backup into the Server with certain properties set while restoring.
At the End i Placed the Data and Log file as per the parameter specified at the top of the commandlet.

5. When i executed the command, lets see what i get as output in Powershell.



6. Let's open SQL and check the database are they restored or not.


7. Now the task left is to set the service instance properties, let's do it from power shell.
--------------------------------------------------------------------------
# Powershell Command to Set service Instance to MultiTenancy.
# Author - Saurav Dhyani
# Blog - saurav-nav.blogspot.com
# Replace Server Name with Actual Server Name

Set-NAVServerConfiguration -ServerInstance MultiTenantRestore -KeyName MultiTenant -KeyValue "true"
Set-NAVServerConfiguration -ServerInstance MultiTenantRestore -KeyName DatabaseName -KeyValue ""
Set-NAVServerConfiguration MultiTenantRestore -Start

Mount-NAVApplication -DatabaseServer "<<SERVER NAME>>" -DatabaseName "MultiTenant_Application_Database" -ServerInstance MultiTenantRestore
Mount-NAVTenant -ServerInstance MultiTenantRestore -Id mytenant1 -DatabaseName "MyTenant1" -OverWriteTenantIDInDatabase
Mount-NAVTenant -ServerInstance MultiTenantRestore -Id mytenant2 -DatabaseName "MyTenant2" -OverWriteTenantIDInDatabase
Mount-NAVTenant -ServerInstance MultiTenantRestore -Id mytenant3 -DatabaseName "MyTenant3" -OverWriteTenantIDInDatabase
Mount-NAVTenant -ServerInstance MultiTenantRestore -Id mytenant4 -DatabaseName "MyTenant4" -OverWriteTenantIDInDatabase
Mount-NAVTenant -ServerInstance MultiTenantRestore -Id mytenant5 -DatabaseName "MyTenant5" -OverWriteTenantIDInDatabase

Set-NAVServerConfiguration MultiTenantRestore -Stop
Set-NAVServerConfiugration MultiTenantRestore -Start
-----------------------------------------------------------------------------
You can download the Powershell command from the Sky Drive Link. Link is available at end of the Post.


8. Let's try to understand what i am doing in above commandlet.

1. I Set the Service Instance as MultiTenant and remove the database Name from Service Instance. Then i start the service instance.
2. After that i mounted the application database on the service instance and after that mounted all tenats in the service instance.
3. After that i stop and started the service.

9. Let's navigate to administrative panel and check the service instance.



So i guess we have completed what we started successfully.

You can download the Powershell command from my SkyDrive.

File Name - Command 8 - NAV 2013 R2 - Restore a Multi Tenant Environment.
File Name - Command 9 - NAV 2013 R2 - Setting up Service Instance for Multi Tenant Environment.

Hope you find the information useful.

Regards,
Saurav Dhyani 
saurav-nav.blogspot.com

No comments:

Post a Comment