Home » Applied SQL »Beginner »sqlserverpedia-syndication »SSC »TSQL » Currently Reading:

Applied SQL: Demonstrate Simple Backup and Restore

October 26, 2011 Applied SQL, Beginner, sqlserverpedia-syndication, SSC, TSQL 2 Comments

Let’s apply a little backup and restore knowledge, eh wot?

I introduce this whole concept in Applied SQL: You Have Homework. Find all assignments on the Applied SQL page.

Prerequisites: basic T-SQL, create objects/data, basic understanding of recovery models and backup types.

Reading – SQL Server Books Online articles:

Setup: We’re going to build a script that demonstrates different backups, and the effect of their restores.  The setup and assignments should all contribute to this one script, so save your work!  First, script a new test database and table simply:

  • Name the database “testing”
  • Set it to FULL recovery mode.
  • Create a simple table (T1) in it with at least one varchar(100) column.
  • Insert a few rows into your table.
  • Add a “drop database; GO ” at the beginning of your script, so you’ll have a clean start each time.

Level 1 Assignments: Backups – after the Setup section in your script, add the following:

[1-1] Full backup 

  • Backup the database to disk, filename testing.bak
  • Insert a few more rows into your table.

[1-2] TLOG backup

  • Backup the LOG to disk, filename testingLOG1.bak
  • Insert a few more rows into your table.

[1-3] DIFF backup, one more TLOG backup

  • Take a DIFFERENTIAL backup of the database, filename testingDIFF2.bak
  • Insert a few more rows into your table.
  • Backup the LOG to disk, filename testingLOG3.bak
  • Insert a few more rows into your table.

For the purposes of this exercise, we’re naming the backup files a little oddly (full, log1, diff2, log3). We’re doing this to help us keep track in the next assignment section – restores.

Setup2: Speaking of the next section…we have the option of using the same backup files, or of rerunning then entire script. Rerunning the whole script is kind of fun, so go back to all of your BACKUP statements and add options to initialize and format your backup file.

Level 2 Assignment: Restores – after the Backups section in your script, add and run a “drop database; GO ” to make way for the restores, then add the following:

[2-1] Restore FULL

  • Restore the FULL backup of the database (from step 1-1 above).
  • Select all data from your table; note how data entered later (after the FULL backup) isn’t here. Exactly as we planned it…
  • Drop the database & comment out this section when you’re ready to move on.

[2-2] Restore FULL, DIFF

  • Restore the FULL and DIFFERENTIAL backups of the database (from steps 1-1 and 1-3 above). If you haev problems restoring the differential, pay attention to the error you get.
  • Select all data from your table; note how data entered later (after the DIFF backup) isn’t here. Get the tlog1/diff relationship straight in your head…do you know why we skipped right over the tlog1 backup?
  • Drop the database & comment out this section when you’re ready to move on.

[2-3] Restore FULL, DIFF, TLOG

  • Restore the FULL, DIFFERENTIAL, and TLOG3 backups of the database (from steps 1-1, 1-3, 1-4 above). Again, pay attention to any errors you get to figure out the problem.
  • Select all data from your table to see where you stand.

Bonus points: Post your scripts (or a link to them) in the comments below, just to show off that you did it.

Double bonus points: Do you think we should’ve done something more here? Post a related assignment of your own in the comments, and I’ll see how I do.

Recommended optional videos, by MidnightDBA:

 -Jen McCown
www.MidnightDBA.com/Jen

Share on Twitter
Share on LinkedInShare on TumblrSubmit to StumbleUponhttp://www.midnightdba.com/Jen/wp-content/uploads/2011/08/SSMS_practice-300x145.pngDigg ThisSubmit to reddit

Currently there are "2 comments" on this Article:

  1. John Halunen says:

    Great blog. You could do a second blog (or additional homework) on tail of the log and recovery/norecovery.

  2. [...] McCown’s (blog | twitter) latest assignment in the Applied SQL series deals with creating a simple database to be used to demonstrate backup [...]

Comment on this Article:







Blog Posts by Category


We're speaking at the PASS Summit 2013!

The Newsiest

Hear Sean and Jen on the PowerScripting Podcast! The MidnightDBAs were honored guests on episode 218. Check it out! Free SQL training, coming to a town near you A full day of SQL Server training is more than likely going to be at a town near you! Find out when and where at www.sqlsaturday.com/

The best database career advice you’ve never heard!

DBARoadmap.com

The DBA Roadmap Seminar is 7 MP3 tracks (over 5 hours!) of insider guidance on your database career. We'll teach you how and what to study as a DBA, weigh in on controversial resume debates, teach you to recognize a worthy recruiter, and discuss the new professionalism of interviews. Also some bonus materials, PDF companion guides, and really spiffy intro music!

Once your $99 PayPal payment is submitted, you'll get the download link in e-mail! (Download is a 370Mb ZIP file.)

Become a DBA. Become a BETTER DBA. Use the Roadmap.

Visit www.DBARoadmap.com for info, forums, and more!

Add to Cart View Cart