14

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

ABCD_21_03_2013
ABCD_22_03_2013

我怎样才能做到这一点。我对这些类型的 SQL Backup JOBS 了解不多。

4

5 回答 5

25

要创建名称为 Filename_MM_DD_YYYY 的每日备份:

  1. 在 SSMS 中,右键单击要备份的数据库
  2. 选择任务 | 备份
  3. 在对话框中,选择备份的类型和位置
  4. 在此对话框的顶部,在脚本下拉菜单中选择脚本操作到作业选项
  5. 将打开一个New Job对话框,第一步创建数据库备份 转到第一步并单击Edit

现有代码如下所示:

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

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

于 2013-07-29T11:13:33.793 回答
9

Automatic backup of all databases on the server.

About Jobs:

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

Query:

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

Output:

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;

Results:

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).

.....
于 2013-05-24T06:56:48.117 回答
2

I think, best way to perform schedule back-up is to create Job. Add your back-up job and schedule that on particular date and time.

How to create job

于 2013-05-24T06:57:08.263 回答
0

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

于 2014-04-10T09:21:29.093 回答
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
于 2017-08-14T14:28:35.223 回答