sql backups, restores and verification with sql sever
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.comTRANSCRIPT
![Page 1: SQL Backups, Restores and Verification with SQL Sever](https://reader036.vdocuments.mx/reader036/viewer/2022083000/5577b03fd8b42af34a8b529c/html5/thumbnails/1.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022083000/5577b03fd8b42af34a8b529c/html5/thumbnails/2.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022083000/5577b03fd8b42af34a8b529c/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022083000/5577b03fd8b42af34a8b529c/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022083000/5577b03fd8b42af34a8b529c/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022083000/5577b03fd8b42af34a8b529c/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022083000/5577b03fd8b42af34a8b529c/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022083000/5577b03fd8b42af34a8b529c/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022083000/5577b03fd8b42af34a8b529c/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022083000/5577b03fd8b42af34a8b529c/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022083000/5577b03fd8b42af34a8b529c/html5/thumbnails/11.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022083000/5577b03fd8b42af34a8b529c/html5/thumbnails/12.jpg)
Using DBCC
Verification Demo
![Page 13: SQL Backups, Restores and Verification with SQL Sever](https://reader036.vdocuments.mx/reader036/viewer/2022083000/5577b03fd8b42af34a8b529c/html5/thumbnails/13.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022083000/5577b03fd8b42af34a8b529c/html5/thumbnails/14.jpg)
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