sql server备份脚本

时间:2012-05-21 15:37:46

标签: sql-server sql-server-2008

我已经尝试为我的一个非常小的sql server express 2008数据库编写备份脚本。 我的要求是每晚进行一次完整备份,保留最后五个备份。这是我写一篇文章的尝试,想得到关于我是否做得对的反馈? 谢谢你的帮助。

    declare @backupfilename nvarchar(100)
    set @backupfilename='c:\...location..\filename_'+convert(varchar(10),getdate(),112) + '.bak'

    BACKUP DATABASE [dbname] TO  DISK = @backupfilename 
    WITH  RETAINDAYS = 5, NOFORMAT, NOINIT,  NAME = N'Full Database Backup Name', NOSKIP,
    NOREWIND, NOUNLOAD,  STATS = 10
    GO
    declare @backupSetId as int
    select @backupSetId = position from msdb..backupset where database_name=N'dbname'
    and backup_set_id=(select max (backup_set_id) from msdb..backupset where 
    database_name=N'dbname' )
    if @backupSetId is null begin raiserror(N'Verify failed. Backup information for database  
    ''dbname'' not found.', 16, 1) end
    RESTORE VERIFYONLY FROM  DISK =@backupfilename  WITH  FILE = @backupSetId,  NOUNLOAD,  NOREWIND
    GO

1 个答案:

答案 0 :(得分:18)

我强烈推荐Ola's backup stored procedures。他们得到很好的支持,非常扎实。如果你觉得使用它们感觉不舒服,你可以在编写自己的例行程序时至少看看它们。