SQL Server:备份所有数据库 [英] SQL Server: backup all databases
问题描述
我想知道是否有任何方法可以定期将整个SQL Server(我们正在使用SQL Server 2008)备份到特定位置.我知道我们能够备份单个特定的数据库,但是为了易于使用,并且每次我想要一个新数据库时都不必设置备份,有没有办法做到这一点?
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.
推荐答案
您可以运行以下脚本,只需将@path变量更改为要存储数据库的位置.
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
摘自:
http://www .mssqltips.com/sqlservertip/1070/simple-script-to-backup-all-sql-server-databases/
这篇关于SQL Server:备份所有数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!