如何在sql server中创建具有唯一名称的每日备份

时间:2013-05-24 06:45:23

标签: sql-server sql-server-2008 sql-server-2008-r2

我想每天使用唯一名称对我的服务器的所有数据库进行完整数据库备份。为此我有一个想法,保持时间戳,使数据库副本分开。 假设服务器上有一个名为ABCD的数据库,那么它应该被备份为:

ABCD_21_03_2013
ABCD_22_03_2013

我该怎么做?我对这些类型的SQL备份JOBS了解不多。

5 个答案:

答案 0 :(得分:24)

使用诸如Filename_MM_DD_YYYY:

之类的名称创建每日备份
  1. 在SSMS中,右键单击要备份的数据库
  2. 选择任务|备份
  3. 在对话框中,选择备份的类型和位置
  4. 在此对话框的顶部,选择脚本下拉菜单中的脚本操作到作业选项
  5. 打开新作业对话框,第一步创建数据库备份 转到第一步,然后点击修改
  6. 现有代码如下:

    BACKUP DATABASE [AdventureWorks2012] TO  DISK = N'E:\Test\AdventureWorks.bak' WITH NOFORMAT, NOINIT,  NAME = N'AdventureWorks2012-Full Database Backup', SKIP, NOREWIND, NOUNLOAD,  STATS = 10
    GO
    

    将其替换为

    DECLARE @SQLStatement VARCHAR(2000) 
    SET @SQLStatement = 'E:\Test\AdventureWorks_' + CONVERT(nvarchar(30), GETDATE(), 110) +'.bak' 
    BACKUP DATABASE [AdventureWorks2012] TO  DISK = @SQLStatement
    

    6保存作业

    数据库备份将命名为:

    AdventureWorks_07-29-2013

    AdventureWorks_07-30-2013

    AdventureWorks_07-31-2013

    现在你所要做的就是制定正确的时间表

答案 1 :(得分:9)

自动备份服务器上的所有数据库。

关于工作:

http://msdn.microsoft.com/en-us/library/ms190268.aspx

<强>查询:

SET NOCOUNT ON;

DECLARE 
      @FileName NVARCHAR(1024)
    , @DBName NVARCHAR(256)
    , @PathName NVARCHAR(256)
    , @Message NVARCHAR(2048)
    , @IsCompressed BIT

SELECT 
      @PathName = 'D:\BACKUP\'
    , @IsCompressed = 1 

DECLARE db CURSOR LOCAL READ_ONLY FAST_FORWARD FOR  
    SELECT
          sd.name
        , file_path = @PathName + FileDate + '_' + name + '.bak'
    FROM sys.databases sd
    CROSS JOIN (
        SELECT FileDate = 'ABCD_' + REPLACE(CONVERT(VARCHAR(10), GETDATE(), 103), '/', '_')
    ) fd
    WHERE sd.state_desc != 'OFFLINE'
        AND sd.name NOT IN ('master', 'model', 'msdb', 'tempdb')
    ORDER BY sd.name 

OPEN db

FETCH NEXT FROM db INTO 
      @DBName
    , @FileName  

WHILE @@FETCH_STATUS = 0 BEGIN 

    DECLARE @SQL NVARCHAR(MAX)

    SELECT @Message = REPLICATE('-', 80) + CHAR(13) + CONVERT(VARCHAR(20), GETDATE(), 120) + N': ' + @DBName
    RAISERROR (@Message, 0, 1) WITH NOWAIT

    SELECT @SQL = 
    'BACKUP DATABASE [' + @DBName + ']
    TO DISK = N''' + @FileName + '''
    WITH FORMAT, ' + CASE WHEN @IsCompressed = 1 THEN N'COMPRESSION, ' ELSE '' END + N'INIT, STATS = 15;' 

    EXEC sys.sp_executesql @SQL

    FETCH NEXT FROM db INTO 
          @DBName
        , @FileName 

END   

CLOSE db   
DEALLOCATE db

<强>输出:

BACKUP DATABASE [AdventureWorks2008R2]
    TO DISK = N'D:\BACKUP\ABCD_24_05_2013_AdventureWorks2008R2.bak'
    WITH FORMAT, COMPRESSION, INIT, STATS = 15;

BACKUP DATABASE [AdventureWorks2008R2_Live]
    TO DISK = N'D:\BACKUP\ABCD_24_05_2013_AdventureWorks2008R2_Live.bak'
    WITH FORMAT, COMPRESSION, INIT, STATS = 15;

BACKUP DATABASE [AdventureWorks2012]
    TO DISK = N'D:\BACKUP\ABCD_24_05_2013_AdventureWorks2012.bak'
    WITH FORMAT, COMPRESSION, INIT, STATS = 15;

<强>结果:

2013-05-24 09:54:34: AdventureWorks2008R2
15 percent processed.
30 percent processed.
45 percent processed.
60 percent processed.
75 percent processed.
90 percent processed.
Processed 23416 pages for database 'AdventureWorks2008R2', file 'AdventureWorks2008R2_Data' on file 1.
Processed 1 pages for database 'AdventureWorks2008R2', file 'AdventureWorks2008R2_Log' on file 1.
BACKUP DATABASE successfully processed 23417 pages in 4.052 seconds (45.148 MB/sec).

.....

答案 2 :(得分:2)

我认为,执行计划备份的最佳方法是创建Job。添加备份工作并在特定日期和时间安排。

How to create job

答案 3 :(得分:0)

某些第三方备份程序(例如:EMS SQL Backup)允许设置备份文件名的模板。可以将时间戳,服务器实例名称,数据库名称和其他信息添加到文件名中。

答案 4 :(得分:0)

感谢您的帖子 我只想分享我在脚本上做的一个小更新,以进行日志数据库备份,并自动跳过所有不允许日志备份的recovery model = simple数据库。希望能帮助到你... 是的,Ravi先生是对的,最好的方法是创造一份工作,我已经创建了一个SP并且正在从事工作。

CREATE PROCEDURE sp_logbackup 
AS
SET NOCOUNT ON;
DECLARE 
  @FileName NVARCHAR(1024)
, @DBName NVARCHAR(256)
, @PathName NVARCHAR(256)
, @Message NVARCHAR(2048)
, @IsCompressed BIT
SELECT 
  @PathName = '\\myServer\...' 
, @IsCompressed = 1 
DECLARE db CURSOR LOCAL READ_ONLY FAST_FORWARD FOR  
SELECT
      sd.name
    , file_path = @PathName + name + '_' + FileDate + '.trn'
FROM sys.databases sd
CROSS JOIN (
    SELECT FileDate = REPLACE(REPLACE(REPLACE(CONVERT(varchar,GETDATE(), 20),'-','_'),':',''),' ','') 
) fd
WHERE sd.state_desc != 'OFFLINE'
    AND sd.recovery_model != 3
    AND sd.name NOT IN ('master', 'model', 'msdb', 'tempdb')
ORDER BY sd.name 
OPEN db
FETCH NEXT FROM db INTO 
  @DBName
, @FileName  
WHILE @@FETCH_STATUS = 0 BEGIN 
DECLARE @SQL NVARCHAR(MAX)
SELECT @Message = REPLICATE('-', 80) + CHAR(13) + CONVERT(VARCHAR(20), GETDATE(), 120) + N': ' + @DBName
RAISERROR (@Message, 0, 1) WITH NOWAIT

SELECT @SQL = 
'BACKUP LOG [' + @DBName + ']
TO DISK = N''' + @FileName + '''
WITH FORMAT, NAME = N''' + @DBName + ''', SKIP, REWIND, NOUNLOAD, STATS = 10;' 

EXEC sys.sp_executesql @SQL
FETCH NEXT FROM db INTO 
      @DBName
    , @FileName 
END   
CLOSE db   
DEALLOCATE db