Wednesday, April 11, 2012

Script To Generate Backup of All Databases

DECLARE @db_name VARCHAR(50)

DECLARE @file_path VARCHAR(150)
DECLARE @file_Name VARCHAR(150)

SET @file_path = 'C:\Temp\'

IF left(REVERSE(@file_path ),1) <> '\'
            SET @file_path = @file_path + '\'

DECLARE db_cursor CURSOR FOR
         SELECT name FROM master..sysdatabases WHERE name NOT IN ('tempdb')

OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @db_name
WHILE @@FETCH_STATUS = 0
BEGIN
             SET @file_Name = @file_path + @db_name + '_' + replace(CONVERT(varchar, getdate(), 
                               101),'/','') + '_Full.BAK'
             BACKUP DATABASE @db_name TO DISK = @file_Name
             FETCH NEXT FROM db_cursor INTO @db_name
END
CLOSE db_cursor
DEALLOCATE db_cursor

No comments: