Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Delay Validation with SSIS TasksInternet Facing Reporting Services Instance »
    comments

    There was a need to create a job that would take the last backup from one server and restore it on another server every day at 5 AM. There are a couple of things you need to know before you do that; some of these things are: permissions, what is the file name of the last backup, are there any jobs that need to be stopped and disabled before you restore etc etc.

    This post will focus on getting the physical name for the last backup for a database. Let's look at some code.

    Create a test database

    1. CREATE DATABASE test
    2. GO

    Create some sample data

    1. USE test
    2. GO
    3.  
    4. SELECT * INTO TestTable
    5. FROM master..spt_values
    6. GO

    Now it is time to do the backups, I have a C:\Junkdraw\ folder on my machine and a job that runs every night that will delete everything older than 2 weeks (I got this idea from the lifehacks book and it really helps with getting rid of not needed files). If you want to run the backup commands on your PC, just make sure to change C:\Junkdraw\ to something that does exist.

    1. BACKUP DATABASE [Test] TO  DISK = N'C:\Junkdraw\Test.bak' WITH NOFORMAT, NOINIT
    2. ,SKIP, NOREWIND, NOUNLOAD,  STATS = 10
    3. GO
    4.  
    5. BACKUP DATABASE [Test] TO  DISK = N'C:\Junkdraw\Test2.bak' WITH NOFORMAT, NOINIT
    6. ,SKIP, NOREWIND, NOUNLOAD,  STATS = 10
    7. GO

    In order to get the information about the backups we need to join the backupset and backupmediafamily tables. Here is the query that will give me the physical device name, backup start date, backup finish date and the size of the backup.

    1. SELECT          physical_device_name,
    2.                 backup_start_date,
    3.                 backup_finish_date,
    4.                 backup_size/1024.0 AS BackupSizeKB
    5. FROM msdb.dbo.backupset b
    6. JOIN msdb.dbo.backupmediafamily m ON b.media_set_id = m.media_set_id
    7. WHERE database_name = 'Test'
    8. ORDER BY backup_finish_date DESC

    Output

    physical_device_name	backup_start_date	backup_finish_date	BackupSizeKB
    C:\Junkdraw\Test2.bak	2010-03-24 12:43:43.000	2010-03-24 12:43:43.000	1483.000000
    C:\Junkdraw\Test.bak	2010-03-24 12:43:33.000	2010-03-24 12:43:33.000	1482.000000

    What if you want to know the time the last backup finished for all the databases? You can use the following query to get that info

    1. WITH LastBackupTaken AS (
    2. SELECT database_name,
    3. backup_finish_date,
    4. RowNumber = ROW_NUMBER() OVER (PARTITION BY database_name
    5.                             ORDER BY backup_finish_date DESC)
    6. FROM msdb.dbo.backupset
    7.  
    8. )
    9.  
    10. SELECT database_name,backup_finish_date
    11. FROM LastBackupTaken
    12. WHERE RowNumber = 1

    Here are the results for that query

    master	2010-03-24 12:58:03.000
    model	2010-03-24 12:58:24.000
    msdb	2010-03-24 12:58:15.000
    Policy	2010-03-24 09:59:32.000
    Test	2010-03-24 12:43:43.000

    This last part is also on our wiki, you can find it here Get last backup dates for all databases ‎




    *** Remember, if you have a SQL related question, try our Microsoft SQL Server Programming forum or our Microsoft SQL Server Admin forum

    About the Author

    User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
    Social SitingsTwitterFacebookLinkedInHomePageFlickrLTD RSS Feed
    3283 views
    InstapaperVote on HN

    4 comments

    Comment from: Eli Weinstock-Herman (tarwn) [Member]
    Eli Weinstock-Herman (tarwn) Interesting, I used something similar to help me monitor backups at my previous employer. I queried for any databases that did not have backups of a specific type (full for instance) within a specific period. I plugged that query into our network monitoring software for each server and had a handy mechanism that would alert me if a database was created without a backup plan, if a database backup had not occurred, etc. This helped on several occasions with new databases, once when the backup software didn't report failures (brightstore?), and once when we turned the backup jobs off to perform maintenance and forgot to turn them back on (oops).

    I'm curious how you wrote the rest of the restore as well. I could see where being able to schedule a restore on a schedule (for instance, from our production down to beta and dev servers) would be extremely helpful.
    04/03/10 @ 16:06
    Comment from: SQLDenis [Member] Email
    SQLDenis it is pretty simple, it is I believe a 9 step process

    on orig server
    step 1 (SSIS package) get filename and copy backup to other server (script task)

    on destination server
    step 2 Stop 5 jobs and disable them that hit this DB

    Step 3 make DB single user and do restore

    Step 4 recreate users and grant permissions from orig server and apply permissions here

    Step 5 make DB multi user

    Step 6 update some tables with current time stamp

    step 7 enable jobs that were disabled before

    Step 8 alter 2 tables

    there is another step where I do something but don't quite remember what I do, will check when I VPN in or when I get to the office
    04/03/10 @ 16:22
    Comment from: SQLDenis [Member] Email
    SQLDenis >>if a database was created without a backup plan.......

    you know that you can setup maintenance plans that will backup every user database, this way if someone creates a new database it will be backed up
    04/03/10 @ 16:23
    Comment from: Eli Weinstock-Herman (tarwn) [Member]
    Eli Weinstock-Herman (tarwn) Yep, and I can use SQLMail to send messages as part of those tasks. This environment was using an external backup solution that incorporated system backups as well as database backups and managed the tapes, etc. It didn't pick up new databases automatically and for a while we were transitioning from that tool to built-in SQL backups, so monitoring the backupset table was the cleanest single-source monitoring trick. After we moved completely to built-in backups I probably could have done that, but I had several backup profiles and preferred to add the DBs to the correct one in the beginning (because I knew I would never get back to correcting it if I didn't do it right the first time).
    04/04/10 @ 07:11

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)