Last active
June 22, 2016 16:01
-
-
Save chrismrgn/3b1a9c5d39e49edac90aa6a59b0c488b to your computer and use it in GitHub Desktop.
Creates a specified number of copies of a Database. Defaulted to clone the Tridion Content Manager Database 5 times
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Param( | |
[Parameter(Mandatory=$False,Position=1)] | |
[string]$DatabaseName="Tridion_cm", | |
[Parameter(Mandatory=$False,Position=2)] | |
[string]$MachineName = $env:COMPUTERNAME, | |
[Parameter(Mandatory=$False,Position=3)] | |
[string]$InstanceName = "Default", | |
[Parameter(Mandatory=$False,Position=3)] | |
[int]$DataBaseCopies = 5 | |
) | |
Set-Location SQLSERVER:\SQL\$MachineName\$InstanceName | |
#Global Variables | |
$DateTime = Get-Date -Format yyyyMMddHHmmss | |
#Get Server and Backup Path | |
$Server = Get-Item . | |
$backupPath = $Server.BackupDirectory | |
#Get Database | |
$Database = Get-SqlDatabase -Name $DatabaseName | |
$defaultDataPath = $Database.PrimaryFilePath | |
#Backup Database | |
$BackupFileName = $backupPath + "\" + $DatabaseName + "_$DateTime.bak" | |
Backup-SqlDatabase -Database $DatabaseName -BackupFile $BackupFileName | |
#Create Database Copies | |
for($i=1; $i -le $DataBaseCopies; $i++) | |
{ | |
$copyDatabaseName = "$defaultDataPath\$DatabaseName"+"_$i.mdf" | |
$copyLogName = "$defaultDataPath\$DatabaseName"+"_log_$i.ldf" | |
$databaseLogicalName = $DatabaseName | |
$logLogicalName = $DatabaseName + "_log" | |
#Setup Relocate Files | |
$MoveFiles = @() | |
$MoveFiles += New-Object Microsoft.SqlServer.Management.Smo.RelocateFile ("$databaseLogicalName",$copyDatabaseName) | |
$MoveFiles += New-Object Microsoft.SqlServer.Management.Smo.RelocateFile ("$logLogicalName",$copyLogName) | |
$copyDatabaseName = $DatabaseName+"_$i" | |
Restore-SqlDatabase -Database $copyDatabaseName ` | |
-RelocateFile $MoveFiles ` | |
-BackupFile $BackupFileName ` | |
-RestoreAction Database ` | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment