Click here to Skip to main content
Click here to Skip to main content

Backup all databases on server

, 15 Jul 2010
Rate this:
Please Sign up or sign in to vote.
T-SQL scripts: Backup all databases on server with compress feature of SQL Server 2008
USE MASTER
GO
-- Compress feature of SQL 2008
EXEC SP_CONFIGURE 'BACKUP COMPRESSION DEFAULT', '1'
GO
RECONFIGURE WITH OVERRIDE
GO
 
DECLARE @DATABASENAME SYSNAME
DECLARE @SQLCOMMAND VARCHAR(1024)
DECLARE CURDBNAME CURSOR FOR
SELECT [NAME] FROM MASTER..SYSDATABASES
WHERE [NAME] NOT IN ('TEMPDB')
 
OPEN CURDBNAME
FETCH CURDBNAME INTO @DATABASENAME
-- Foreach database
WHILE (@@FETCH_STATUS = 0)
 
BEGIN
	IF DATABASEPROPERTYEX (@DATABASENAME,'STATUS') = 'ONLINE'
	BEGIN
		SELECT @SQLCOMMAND = 'BACKUP DATABASE [' + @DATABASENAME +
		'] TO DISK = ''D:\BACKUPS\DATABASES\' + @DATABASENAME + '.BAK'' WITH FORMAT'
		EXECUTE (@SQLCOMMAND)
		PRINT @DATABASENAME + ' BACKUP COMPLETED!'
	END
	FETCH CURDBNAME INTO @DATABASENAME
END
 
CLOSE CURDBNAME
DEALLOCATE CURDBNAME 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Ngo Thanh Tung - Softech
Technical Writer Softech
Vietnam Vietnam
/* Life runs on code */

Comments and Discussions

 
GeneralReason for my vote of 4 Presentation some points in one blog... PinmemberH_Sh26913-Oct-10 12:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140709.1 | Last Updated 15 Jul 2010
Article Copyright 2010 by Ngo Thanh Tung - Softech
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid