SQL Server: backing up all databases

I was wondering if there is a way to back up the entire SQL Server (we use SQL Server 2008) at specific intervals in a specific place. I know that we can back up individual specific databases, but for ease of use and not back up every time I want a new database, is there a way to do this?

If, however, I should have a way to restore only one database from a server backup, as if I supported them separately.

The method used will be just a clean backup, not a difference, so there is no need to worry about complications.

+3
source share
2 answers

script, @path , .

DECLARE @name VARCHAR(50) -- database name  
DECLARE @path VARCHAR(256) -- path for backup files  
DECLARE @fileName VARCHAR(256) -- filename for backup  
DECLARE @fileDate VARCHAR(20) -- used for file name 

SET @path = 'C:\Backup\'  

SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112) 

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

OPEN db_cursor   
FETCH NEXT FROM db_cursor INTO @name   

WHILE @@FETCH_STATUS = 0   
BEGIN   
       SET @fileName = @path + @name + '_' + @fileDate + '.BAK'  
       BACKUP DATABASE @name TO DISK = @fileName  

       FETCH NEXT FROM db_cursor INTO @name   
END   

CLOSE db_cursor   
DEALLOCATE db_cursor

:

http://www.mssqltips.com/sqlservertip/1070/simple-script-to-backup-all-sql-server-databases/

+3

Use master
GO

--enable
sp_configure 'show advanced options'
GO
/* 0 = Disabled , 1 = Enabled */
sp_configure 'xp_cmdshell', 1
GO
RECONFIGURE WITH OVERRIDE
GO


--===Resource file details
SELECT 'ResourceDB' AS 'Database Name'
    , NAME AS [Database File]
    , FILENAME AS [Database File Location] 
FROM sys.sysaltfiles 
    WHERE DBID = 32767
ORDER BY [Database File]
GO

--===Copy resource files
DECLARE @ResDataFile VARCHAR(1000), @ResLogFile VARCHAR(1000)
SELECT 
    @ResDataFile = CASE NAME WHEN  'Data' THEN FILENAME ELSE @ResDataFile END,
    @ResLogFile = CASE NAME WHEN 'Log' THEN FILENAME ELSE @ResLogFile END
FROM sys.sysaltfiles 
WHERE DBID = 32767

SELECT @ResDataFile, @ResLogFile

DECLARE @cmd VARCHAR(1000)

--===Copy data file
SELECT @cmd = 'COPY /Y "' + @ResDataFile + '" "G:\SQLBackups"'
--PRINT @cmd
EXEC xp_cmdshell @cmd

--===Copy log file
SELECT @cmd = 'COPY /Y "' + @ResLogFile + '" "G:\SQLBackups"'
--PRINT @cmd
EXEC xp_cmdshell @cmd

GO
--Disable
sp_configure 'show advanced options'
GO
/* 0 = Disabled , 1 = Enabled */
sp_configure 'xp_cmdshell', 0
GO
RECONFIGURE WITH OVERRIDE
GO
+1

All Articles