Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# Parametry wejsciowe
$SqlInstancePrimary = 'db-test02'
$SqlInstanceSecondary = 'db-test01'
$Database = 'NewTestAg'
$AvailabilityGroup = 'TestAG'
# 1. Usunięcie bazy danych z Availability Group na instancji Primary
$parameterRemoveSqlAvailabilityDatabase = @{
Path = "SQLSERVER:\Sql\$SqlInstancePrimary\DEFAULT\AvailabilityGroups\$AvailabilityGroup\AvailabilityDatabases\$Database"
}
$parameterRemoveSqlAvailabilityDatabase
Remove-SqlAvailabilityDatabase @parameterRemoveSqlAvailabilityDatabase
# 2. Przywrócenie kopii pełnej, następnie w zależności o polityki kopii zapasowych kopie różnicową i na końcu logi transakcyjne
$parameterPrimaryRestoreSqlDatabase = @{
ServerInstance = $SqlInstancePrimary
BackupFile = '\\db-test02\SQLBackup\f_bck_NewTestAG_200220192056.bak'
Database = $Database
Checksum = $true
ReplaceDatabase = $true
}
$parameterPrimaryRestoreSqlDatabase
Restore-SqlDatabase @parameterPrimaryRestoreSqlDatabase -Verbose
# 3. Dodanie z powrotem przywróconą baze danych do Availability Group
$parameterAddSqlAvailabilityDatabase = @{
Database = $Database
Path = "SQLSERVER:\Sql\$SqlInstancePrimary\DEFAULT\AvailabilityGroups\$AvailabilityGroup"
}
$parameterAddSqlAvailabilityDatabase
Add-SqlAvailabilityDatabase @parameterAddSqlAvailabilityDatabase -Verbose
# 4. Wykonanie pełnej kopii zapasowej na instancji Primary
$parameterBackupSqlDatabase = @{
ServerInstance = $SqlInstancePrimary
BackupFile = '\\db-test02\SQLBackup\f_bck_NewTestAG_220220192056.bak'
Checksum = $true
CompressionOption = 'On'
Database = $Database
}
$parameterBackupSqlDatabase
Backup-SqlDatabase @parameterBackupSqlDatabase
# 5. Przywrócenie bazy (z punktu 4) na instancji Secondary z opcją WITH NORECOVERY (i REPLACE)
$parameterSecondaryRestoreSqlDatabase = @{
ServerInstance = $SqlInstanceSecondary
BackupFile = '\\db-test02\SQLBackup\f_bck_NewTestAG_220220192056.bak'
Database = $Database
Checksum = $true
ReplaceDatabase = $true
NoRecovery = $true
}
$parameterSecondaryRestoreSqlDatabase
Restore-SqlDatabase @parameterSecondaryRestoreSqlDatabase
# 6. Dodajemy bazę na instancji Secondary do Availability Group
Sleep -Seconds 30
$parameterAddSqlAvailabilityDatabase = @{
Database = $Database
Path = "SQLSERVER:\Sql\$SqlInstanceSecondary\DEFAULT\AvailabilityGroups\$AvailabilityGroup"
}
$parameterAddSqlAvailabilityDatabase
Add-SqlAvailabilityDatabase @parameterAddSqlAvailabilityDatabase
# Sprawdzenie statusu
$parameterTestSqlAvailabilityGroup = @{
Path = "SQLSERVER:\Sql\$SqlInstancePrimary\DEFAULT\AvailabilityGroups\$AvailabilityGroup"
}
$parameterTestSqlAvailabilityGroup
(Test-SqlAvailabilityGroup @parameterTestSqlAvailabilityGroup).HealthState
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.