我尝试为我的一个非常小的 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