Backup/Restore

This category page is part of a series of Professional PowerShell SMO Recipes. You can find an index of all recipes on the main SMO Recipes page, and if you want to learn more about each recipe and see some sample screenshots, click on its category page.

These scripts were created and tested on Windows 8.1 with PowerShell v4 and SQL Server 2014, though most recipes should work with earlier versions of SQL Server. PowerShell v3 and above is required for many of the recipes. If you have a recipe request, leave a comment and I'll see what I can do. This cookbook will be continuously built, as I work more with SMO.

Recipe Categories

Backup/Restore

To see some these scripts used within a formally written PowerShell script that includes parameterized functions, check out my SQL migration script, Start-SQLMigration.ps1, on Microsoft’s ScriptCenter.

Find databases that have never been backed up or have outdated backups

Make FULL copy-only backup with progress bar

This script will back up a database to a fileshare (or to the local SQL Server if you know and have access to a valid path). When performing backup and restores using SMO, it’s important to remember to set the $server.ConnectionContext.StatementTimeout to prevent your backup or restore from timing out.

In this script, I’ve set the CopyOnly property to true, since it’s likely you’re going to be performing one-off backups and don’t want to get in the way of your regularly scheduled backup chain.

backup

Get database file structure from a FULL backup file

This is useful for automated restores. A functioning server is required for this to work, as the SQL Server does the actual legwork of reading the backup file. The service account running the SQL Server service must have access to the backup location.

Restore FULL backup to server default data and log locations. Progress bar included.

This script will restore a full backup, and place its data and log files in the server’s default directories. Note that this will fail if you attempt to replace files that already exist. To avoid this, you can test the path and add some random characters, but that’s beyond the scope of this snippet.

restore

Restore FULL backup, reusing previous file structure. Progress bar included.

This script automatically determines the data and file locations based on the file information contained within the .bak file. If you need to restore to an alternative location, you can manually change the values or refer to the previous recipe to see how to place the restore files within the SQL Server’s default data and log directories.

Restore FULL/DIFF/LOGS from Hallengren-style default directory structure

I’m using this in a new script I’m writing which restores an entire offline server’s worth of databases to another server. This differs from a server-to-server live migration covered by Start-SQLMigration.ps1. Consider this scenario: you use Ola Hallengren’s SQL Maintenance Scripts to backup your databases to a structure similar to the following

  • \\fileserver\sqlbackups\SQLSERVER2014A\MyDatabase\FULL
  • \\fileserver\sqlbackups\SQLSERVER2014A\MyDatabase\DIFF
  • \\fileserver\sqlbackups\SQLSERVER2014A\MyDatabase\LOG

Your SQL Server blows up and goes offline, and you have to restore one or more databases to a new server, up to the most recent log backup (or FULL/DIFF if you’re using the SIMPLE recovery model). Note: This does not address restoring system databases. That’s a bit more complex.

Want to see more? You can find an index of all recipes on SMO Recipes Index Page page or click on any specific category at the top of this page.

Chrissy is a PowerShell MVP who has worked in IT for nearly 20 years, and currently serves as a Sr. Database Engineer in Belgium. Always an avid scripter, she attended the Monad session at Microsoft’s Professional Developers Conference in Los Angeles back in 2005 and has worked and played with PowerShell ever since. Chrissy is currently pursuing an MS in Systems Engineering at Regis University and helps maintain RealCajunRecipes.com in her spare time. She holds a number of certifications, including those relating to SQL Server, SuSE Linux, SharePoint and network security. She recently became co-lead of the SQL PASS PowerShell Virtual Chapter. You can follow her on Twitter at @cl.

One comment on “Backup/Restore
  1. Eric Moore says:

    Very helpful. Thanks for posting!

Leave a Reply

Your email address will not be published. Required fields are marked *

*