Since today, about 25 new “How Do I”-video’s were uploaded to youtube. This means.. video’s that were already available to partners, are now made available for everyone. As mentioned before, I have been participating in some video’s, including the one about Backup/Restore: How Do I: Backup and Restore in a Multitenant Environment in Microsoft Dynamics NAV 2013 R2 .
As you might know, the native backup/Restore tool has been removed from the stack .. plus .. multitenancy brings some new perspectives in this matter. Or should I say “challenges”. In this video, I try to tackle these challenges using Powershell. In this blog, I would like to share the powershell scripts that I used, for your convenience (I guess it’s somewhat easier to copy/paste from a blogpost
). For more info, I recommend you to watch the movie.
Backup
The backup script is actually just backing up the App-db and after that, looping through the tenants, and backup those databases as well.. :
$ServiceInstance = 'DynamicsNAV71'
#Backup the Application Database
$NavApplication = Get-NAVApplication $ServiceInstance
Backup-SqlDatabase -ServerInstance $NavApplication.'Database Server' -Database $NavApplication.'Database Name' -BackupAction Database
#Backup All the Tenant Databases
$NavTenants = Get-NAVTenant $ServiceInstance
Foreach ($NavTenant in $NavTenants) {
Backup-SqlDatabase -ServerInstance $NavTenant.DatabaseServer -Database $NavTenant.Databasename -BackupAction Database
}
Restore from a folder
The restore is quite simply done as well.. : loop through the folder where all backups were placed, and restore the backups one-by-one:
$Backups = dir 'c:\$Restore\*.bak'
Set-NAVServerInstance DynamicsNAV71 -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}
$Restore.SqlRestore($server)
}
After this script, you have to mount the app and tenants to a multitenant serverinstance (which is basically what I explained in another video (also these scripts will be shared on a later stage)).
What about Backing up and Restoring 1 company
Well, I am planning a blog (or movie) about it, because there are some things to think about: mainly the fact that when you’re restoring a company in a tenant with shared tables (DataPerCompany=No), then those tables are going to be overwritten, and you have the chance of ending up with an inconsistant database. Be careful with this.
This is a great feature, but you have to know what you’re doing… . (and let’s be honest: should there be shared tables in the first place?
). Stay tuned…

Like
Report
*This post is locked for comments