sql-server – sql server备份脚本

我试图为我的一个非常小的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
我强烈推荐Ola’s backup stored procedures.它们得到很好的支持,非常坚固.如果你不舒服使用它们,你至少可以在编写自己的例程时看看他们.
翻译自:https://stackoverflow.com/questions/10688274/sql-server-backup-script

转载注明原文:sql-server – sql server备份脚本