sql backups, restores and verification with sql sever

14
SQL Server Backups with PowerShell Automating restores and verification to leave more time for the fun things in a DBA’s job

Upload: stuart-moore

Post on 10-Jun-2015

697 views

Category:

Technology


1 download

DESCRIPTION

A presentation from SQL Southwest User Group about using PowerShell to automate SQL Server backups, restores and verifications, so DBAs can get on with the more fun things in life. An accompanying set of scripts are available from my website http://stuart-moore.com

TRANSCRIPT

Page 1: SQL Backups, Restores and Verification with SQL Sever

SQL Server Backups with PowerShell

Automating restores and verification to leave more time for

the fun things in a DBA’s job

Page 2: SQL Backups, Restores and Verification with SQL Sever

Started with SQL Server 7 in 1998, 15 years later still working with it, but newer versions as well.

Worked as DBA and Developer in that period. Also work with Oracle, MySQL and Linux

In spare time I’m most likely to be found studying a Mathematics degree with the OU, or sat on a bike saddle somewhere remote.

Email: [email protected] Blog: http://stuart-moore.com Twitter: @napalmgram

Stuart Moore

Page 3: SQL Backups, Restores and Verification with SQL Sever

Started off as Monad back in 2005 Improved with each version, now on version

3 Lots of support for SQL Server:

◦ SMO – SQL Management Objects◦ SQLPS – Official SQL Server Module, offers

methods for the above ◦ SQLPSX – Unofficial SQL Server Module

PowerShell

Page 4: SQL Backups, Restores and Verification with SQL Sever

Essential part of the job Easy to set up simple backup routines with

maintenance plans, not so easy for complex setups

Automated Backups

Page 5: SQL Backups, Restores and Verification with SQL Sever

Should be as essential as backups:◦ Did the backup really complete properly?◦ How long does a full restore take (how long should

RTO be)◦ How long do the transaction backups take to

restore?◦ Do you have whole backup chain◦ If needed, can you prove it to Auditors?

Automated Restores

Page 6: SQL Backups, Restores and Verification with SQL Sever

SQL Server doesn’t offer an easy means to automate restores.

Tend to need to build a job for each database◦ Not easy with an instance with 100s of DB◦ Differing file layouts also a problem

So why aren’t they?

Page 7: SQL Backups, Restores and Verification with SQL Sever

Demos may not be the best examples of PowerShell.◦ I’ve taken some liberties to make them easier to

read◦ Best practice is to write reusable functions, not

lots of scripts.

Warning!!!!

Page 8: SQL Backups, Restores and Verification with SQL Sever

1. Basic Full backup of single db2. Backup all DB on an instance3. Catch new databases4. Split Database backups

Backup Demos

Page 9: SQL Backups, Restores and Verification with SQL Sever

1. Simple Restore2. Include transaction backups3. Check transaction log order4. Restore with file relocation5. Restore to Random point in Time

Restore Demos

Page 10: SQL Backups, Restores and Verification with SQL Sever

So now we’ve restored the database is that it?

No, we need to check that it’s actually usable and structurally sound.◦ Run DBCC checks against◦ Run our own scripts to check integrity or business

rules

Corrupted DBs courtesy of Paul Randal from here - http://bit.ly/1dEeklT

Verification

Page 11: SQL Backups, Restores and Verification with SQL Sever

Use Invoke-Sqlcmd◦ Problems with query timeouts fixed in SQL Server

2012◦ Versions prior to that, look at Invoke-Sqlcmd from

Chad Miller - http://bit.ly/16GOeL8

Page 12: SQL Backups, Restores and Verification with SQL Sever

Using DBCC

Verification Demo

Page 13: SQL Backups, Restores and Verification with SQL Sever

Nothing other than SQL Server backup and restores.◦ Not a faster method◦ Not a slower method◦ Can be monitored just like all other backups◦ Blocked by exactly the same things

Under the hood

Page 14: SQL Backups, Restores and Verification with SQL Sever

Not the world’s most informative error messages:

◦ Plenty of error checking in production scripts◦ Or get used to stepping through to find the

problems

Problems