Build plan of approach to structured point in time restores of databases ( e.g. from Production to QA ) using Powershell as an easy helper tool to ensure all steps are being performed.
3. Restoring Databases
using .
• Build plan of approach to structured point
in time restores of databases ( e.g. from
Production to QA ) using Powershell as an
easy helper tool to ensure all steps are
being performed.
Think
Know
4. Backup
• Backup:
• Minimise data loss in case of disaster
• RPO: Recovery Point Objective = Data loss
• RTO: Recovery Time Objective = Down time
• RSO: Recovery Service Objective = Disaster infrastructure needed
• SQL server database backup :
– Full: Copy of all active pages in your database into a file.
– Differential: Copy of all modified pages since the last Full backup
– Log: Copy of all database transactions since the last Log backup
– Checksum ( Exec sp_configure ‘backup checksum default’,1; Reconfigure )
• What good is a backup if you cannot restore it ?
5. The Setup
• # SQLServer instances have been set up to support the
different QA environments
• Every instance already has the databases pre-configured,
SQLUser accounts in place, windows domain groups, …
• Segregation of duties:
– different passwords
– Different windows domain groups
• Naming conventions help out to make it all work smooth.
• Keep It Simple & Stupid works !
6. The Setup
• Dev/QA teams:
– Have databases for which they are responsible
– Request ad-hoc data refresh for a given
database for which they have responsibility.
– point-in-time restore can be requested
– Approval by team leader
7. SQL Server and Powershell
• SQLPs:
– Powershell integration provided by Microsoft SQL
Server since 2008
– SQL 2012 provides module SQLPs
– SQL 2014 enhanced / extended SQLPs
– SQLAgent integration (job step)
- http://sqlpsx.codeplex.com/
8. SMO vs Powershell ?
• SQL Server Management Objects (SMO)
– SSMS
– object hierarchy and the relationships
– http://msdn.microsoft.com/en-
us/library/ms162209.aspx
– Namespaces for different areas of
functionality within SMO.
– Still needed in case SQLPs
• doesn’t cover the topic
• cannot handle it
9. SMO vs Powershell ?
• SMO namespaces:
– http://msdn.microsoft.com/en-us/library/ms162233.aspx
Class Function
Microsoft.SqlServer.Management.Smo Contains instance classes, utility classes, and enumerations
Microsoft.SqlServer.Management.Common Replication Management Objects (RMO) and SMO, such as connection classes.
Microsoft.SqlServer.Management.Smo.Agent SQL Server Agent.
Microsoft.SqlServer.Management.Smo.Wmi WMI Provider.
Microsoft.SqlServer.Management.Smo.RegisteredServers Registered Server. (SSMS)
Microsoft.SqlServer.Management.Smo.Mail Database Mail.
Microsoft.SqlServer.Management.Smo.Broker Service Broker.
10. SMO vs Powershell ?
• SQLPs:
– 46 Cmdlets that facilitate functionality
– Powershell programmers will be able to recognize parallels
– Discovery / get-help
get-command -module sqlps | sort noun, verb | ft -autosize
21. Process all collected backup files
• Force target dabase offline
– (Get-SqlDatabase -ServerInstance ‘si' -Name ‘db‘).SetOffline()
• will fail without notification when there are still connections
– $SMOdb.ExecuteNonQuery( $("if exists ( select 1 from sys.databases
where name ='{0}' and state = 0 ) Alter database [{0}] set offline
with rollback immediate ; " -f $TargetDb ) )
• -NoRecovery
• -Checksum
• -RelocateFile $rfl
• -FileNumber $DBBackupInfo.Position
• -ReplaceDatabase
22. Post restore operations
• Send-MailMessage
• Sp_changedbowner / ALTER AUTHORIZATION
• Resync Logins and SQLUsers
– Naming conventions
– Do not grant missing accounts !
• DBCC CheckDB
23. Things start coming together
• Put it in a .PS1 file to be used providing
the parameters
Clear-Host
Set-Location $ScriptPath ;
& '.ALZDBA Restore SQL Server database (SQLPS).ps1' -SourceServer ‘S1I1'
-SourceDb 'DB' -TargetServer ‘S2I2' -TargetDb 'DB'-LogRestore
-PointInTimeRestore -PointInTime '2014-05-21 12:00:00'
28. Thank you
“Education is not to fill a bucket, but lighting a fire."
William Butler Yeats
(Irish prose Writer, Dramatist and Poet. Nobel Prize for Literature in 1923. 1865-1939)