MSDB,script,written,because,server,with,clien
Quick Search for:  in language:    
MSDB,script,written,because,server,with,clien
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
SQL Stats

 Code: 25,719 lines
 Jobs: 420 postings

 
Sponsored by:

 

You are in:

 
Login



Latest Code Ticker for SQL.
Job to script all Jobs
By Srdjan Josipovic on 6/19


Enumerate SQL Servers using SQLDMO and T-SQL
By Srdjan Josipovic on 6/19


Count Colums
By Usman Farhat on 6/19


Easy travel SQL logins
By Zoltan Tamas, Toth on 6/17


Build a sequence via derived tables
By Michael S. Trachtenberg on 6/15


Click here to see a screenshot of this code!Creating Grids
By Lewis Moten on 6/15

(Screen Shot)

Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!





Affiliate Sites



 
 
   

Backup All Databases

Print
Email
 
VB icon
Submitted on: 3/23/2001 9:12:15 AM
By: Paul Hendryx  
Level: Advanced
User Rating: By 7 Users
Compatibility:SQL Server 7.0, SQL Server 6.5 and earlier

Users have accessed this code 7841 times.
 
(About the author)
 
     This script was written because I have a server with about 115 client databases on it. It was a rather hard task to try to backup all of them by hand and I didnt want to spend a few grand on a good piece of backup software. Basicly, this script just loops thru all the databases on a given server and backs them up somewhere on that machine. From there, you can compress them and transfer them off site, etc. The backup will include MSDB and Master databases for complete disaster recovery. This script is meant to run daily. On sundays, it will perform full backups of each database and on the other days it does a differential backup. This script will NOT overwrite any files, it stores the backups in the following format: dbname_(full or diff)_20010323.bak Any questions, please email me.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for langauges that allow it) freely and with no charge.   
2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.

--**************************************
--     
-- Name: Backup All Databases
-- Description:This script was written b
--     ecause I have a server with about 115 cl
--     ient databases on it. It was a rather ha
--     rd task to try to backup all of them by 
--     hand and I didnt want to spend a few gra
--     nd on a good piece of backup software. B
--     asicly, this script just loops thru all 
--     the databases on a given server and back
--     s them up somewhere on that machine. Fro
--     m there, you can compress them and trans
--     fer them off site, etc. The backup will 
--     include MSDB and Master databases for co
--     mplete disaster recovery.
This script is meant TO run daily. ON sundays, it will perform FULL backups OF each DATABASE AND on the other days it does a differential backup.
This script will NOT overwrite ANY files, it stores the backups IN the following format:
dbname_(full OR diff)_20010323.bak
ANY questions, please email me.
-- By: Paul Hendryx
--
-- Returns:If you run this on a schedule
--     , you dont see anything, but if you run 
--     it in the query analyzer, you will see d
--     ifferent stats about each database as it
--     s backing up.
--
-- Side Effects:None. Very low CPU usage
--     .
--
--This code is copyrighted and has-- limited warranties.Please see http://
--     www.Planet-Source-Code.com/xq/ASP/txtCod
--     eId.284/lngWId.5/qx/vb/scripts/ShowCode.
--     htm--for details.--**************************************
--     

--**************************************
--     ***************************************
--Developed By: Paul Hendryx (paulh@tekk
--     er.com)
--Date: 3/20/01 3:21 PM
--Current Version: 1.0
--
--The only line that should be edited is
--      the @BackupDirectory line. The
--rest should only be modified by experi
--     enced professionals.
--**************************************
--     ***************************************
--Declare our variables
DECLARE @BackupFile varchar(255), @DB varchar(30), @Description varchar(255), @IsSunday bit
DECLARE @Name varchar(30), @MediaName varchar(30), @HasFullBackup bit, @BackupDirectory nvarchar(200)
--Set the backup directory.. dont forget
--      the trailing \ otherwise it wont work.
SET @BackupDirectory = 'c:\backup\'
--Loop thru all the databases that we sh
--     ould backup. Northwind is just an exampl
--     e db and tempdb and model arent needed.
DECLARE Database_Cursor cursor FOR SELECT name FROM sysdatabases WHERE name <> 'tempdb' AND name <> 'model' and name <> 'Northwind'
OPEN Database_Cursor
FETCH next FROM Database_Cursor INTO @DB
WHILE @@fetch_status = 0
    begin
    	--See IF its sunday.. 1=is sunday, 0=is NOT sunday
    	SET @IsSunday = 
    		CASE Datepart(dw, CURRENT_TIMESTAMP)
    			WHEN 1 THEN 1 ELSE 0
    		END
    	--Set SOME stuff so it looks pretty IN sql server
    	SET @Name = @DB + '( Daily BACKUP )'
    	SET @MediaName = @DB + '_Dump' + CONVERT(varchar, CURRENT_TIMESTAMP , 112)
    	SET @BackupFile = @BackupDirectory + + @DB + '_' + 
    		CASE @IsSunday 
    			WHEN 3 THEN 'Full' ELSE 'Diff' 
    		END + '_' + 
    		CONVERT(varchar, CURRENT_TIMESTAMP , 112) + '.bak'
    	SET @Description = 
    		CASE @IsSunday 
    			WHEN 1 THEN 'Normal' ELSE 'Differential' 
    		END + ' BACKUP at ' + CONVERT(varchar, CURRENT_TIMESTAMP) + '.' 
    	--Check TO see IF this db has a FULL backup. If its the master, we have to do a full backup.
    	SET @HasFullBackup = 0
    	IF (select count(*) FROM msdb.dbo.backupset WHERE database_name = @DB) > 0 or @DB = 'master'
    	BEGIN
    		--This DATABASE has a FULL backup.
    		SET @HasFullBackup = 1
    		--If its sunday, THEN we do a FULL BACKUP anyways.
    		SET @BackupFile = @BackupDirectory + @DB + '_' + 
    			CASE @IsSunday 
    				WHEN 3 THEN 'Full' ELSE 'Diff' 
    			END + '_' + 
    			CONVERT(varchar, CURRENT_TIMESTAMP , 112) + '.bak'
    		--Set SOME more pretty stuff FOR sql server.
    		SET @Description = 
    			CASE @IsSunday 
    				WHEN 1 THEN 'Full' ELSE 'Differential' 
    			END + ' BACKUP at ' + CONVERT(varchar, CURRENT_TIMESTAMP) + '.' 
    	END	
    	ELSE
    	BEGIN
    		--This db does NOT have a FULL backup.
    		SET @HasFullBackup = 0
    		--Full BACKUP time.
    		SET @BackupFile = @BackupDirectory + @DB + '_' + 'Full' + '_' + 
    			CONVERT(varchar, CURRENT_TIMESTAMP , 112) + '.bak'
    		--Set SOME more pretty stuff FOR sql server.
    		SET @Description = 'Full' + ' BACKUP at ' + CONVERT(varchar, CURRENT_TIMESTAMP) + '.' 
    	END
    	--If it IS sunday, or the CURRENT db is master, or if this db has NOT had a FULL backup, the do a full backup.
    	IF @IsSunday = 1 or @DB = 'master' or @HasFullBackup = 0
    	BEGIN
    		BACKUP DATABASE @DB TO DISK = @BackupFile 
    		WITH NAME = @Name, DESCRIPTION = @Description , 
    		MEDIANAME = @MediaName, MEDIADESCRIPTION = @Description , 
    		STATS = 10
    	END
    	--This would be a daily differentail backup.
    	IF @IsSunday = 0 AND @DB <> 'master' and @HasFullBackup <> 0
    	BEGIN
    		BACKUP DATABASE @DB TO DISK = @BackupFile 
    		WITH DIFFERENTIAL, NAME = @Name, DESCRIPTION = @Description , 
    		MEDIANAME = @MediaName, MEDIADESCRIPTION = @Description , 
    		STATS = 10
    	END
    	--Print SOME pretty info so we can debug.
    	PRINT '------------------------------------------------------------------------'
    	PRINT ''
    	PRINT 'DB: ' + @DB
    	PRINT 'Name: ' + @Name
    	PRINT 'Media Name: ' + @MediaName
    	PRINT 'Backup File: ' + @BackupFile
    	PRINT 'Description: ' + @Description
    	PRINT 'Is Sunday (0 OR 1): ' + str(@IsSunday)
    	PRINT 'DB has FULL BACKUP (0 or 1): ' + str(@HasFullBackup)
    	PRINT ''
    	PRINT '------------------------------------------------------------------------'
    	PRINT '------------------------------------------------------------------------'
    	print''
    	FETCH next FROM Database_Cursor INTO @DB
end

CLOSE Database_Cursor DEALLOCATE Database_Cursor

 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
Your Vote!

What do you think of this code(in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments
3/25/2001 5:40:58 PM:Kirminator
Nice work!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/28/2001 11:57:45 AM:DBA
Very nice script; the effort that went 
into refining it shows.
Main comment 
I have is I would suggest you back up 
both model and Northwind. Model is 
important because it governs the inital 
settings of each DB and may have 
extensive schema.
Northwind may be 
used by developers and you'll have 
someone come to you someday asking you 
to restore the DB after they blow it up.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/28/2001 7:43:46 PM:SoftwareMaker
Hi Paul,
Thanks for your reply. I 
finally got it NOW. I have always been 
working on DB4 and Access and am just 
migrating or say Upgrading to SQL 
Server. Therefore the ignorance of the 
T-SQL Syntax.
Pardon me for this 
ignorance. I have a lot to learn from 
you.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/29/2001 1:09:22 AM:QQQ
Good code. Vote u 4 stars
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/29/2001 10:07:18 AM:Paul Hendryx
Thanks QQQ, I appriciate it.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/29/2001 10:08:48 AM:Paul Hendryx
SoftwareMaker: No problem.  Glad I 
could help.  I dont claim to be the 
best programmer/scripter or a wizard or 
anything, but I know Im good at what I 
do.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/5/2001 3:53:57 PM:Charles Kincaid
Only one improvement: As you are 
running this script once per day, you 
should move the "Is It Sunday" test to 
before the main loop.<BR><BR>
I 
presume that your backups won't take 
all day but if this script were started 
late on a Sturday it might cross into 
Sunday resulting in some databases beng 
processed one way and some the other.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
2/21/2002 9:26:00 AM:Ben
Fast and simple, does the job
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this code has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular code, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | SQL Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.