Reputation: 1381
I was wondering if there was any way of making a backup of an entire SQL Server (we are using SQL Server 2008) at regular intervals to a specific location. I know we are able to backup single specific databases but for ease of use and not having to set up a backup each time I want a new database, is there a way to do this?
If there is however, I must have a way of restoring just a single database from that server backup easily as though I have backed them up individually.
The method used will just be a pure backup and not a difference so no need to worry about complications.
Upvotes: 4
Views: 3988
Reputation: 437
Consider backing up hidden resource database as well
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
Upvotes: 1
Reputation: 70718
You can run the following script, just change the @path variable to where you want to store the databases.
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
Obtained from:
http://www.mssqltips.com/sqlservertip/1070/simple-script-to-backup-all-sql-server-databases/
Upvotes: 4