我写了一个简单的存储过程来备份 FULL、Differential 和 LOG 备份。这是我的存储过程:
Alter PROCEDURE BackupDB
(
@BaseLocation varchar(1024),
@BackupType varchar(32)
)
AS
BEGIN
Declare @DBName varchar(255) = db_name()
Declare @FileName varchar(256)
Declare @Date datetime = getdate()
Set @BackupType = UPPER(@BackupType)
Set @FileName = @DBName + '_' + Cast(Year(@Date) as varchar(4)) + '_' +
Right('00' + Cast(Month(@Date) as varchar(2)),2) + '_' +
Right('00' + Cast(Day(@Date) as varchar(2)),2) + '_' +
Right('00' + Cast(DatePart(hh,@Date) as varchar(2)),2) + '_' +
Right('00' + Cast(DatePart(mi,@Date) as varchar(2)),2) + '_' +
Right('00' + Cast(DatePart(ss,@Date) as varchar(2)),2) + '_' +
Right('000' + Cast(DatePart(ms,@Date) as varchar(3)),3) + '_' + UPPER(@BackupType) +
(Case When @BackupType = 'FULL' Or @BackupType = 'DIFFERENTIAL' Then '.BAK' Else '.TRN' End)
Declare @FullPath varchar(1280) = ''
If Right(@BaseLocation,1) <> '\'
Begin
Set @BaseLocation = @BaseLocation + '\'
End
Set @FullPath = @BaseLocation + @FileName
Print @FullPath
If @BackupType = 'FULL'
Begin
Backup database @DBName To Disk = @FullPath
End
Else If @BackupType = 'DIFFERENTIAL'
Begin
Backup database @DBName To Disk = @FullPath WITH DIFFERENTIAL
End
Else If @BackupType = 'LOG'
Begin
BACKUP LOG @DBName
TO Disk = @FullPath
End
END
GO
我运行了我的存储过程,它似乎可以完美地处理传入的参数。现在下一步是为此创建一个 SQL JOB。但是在我这样做之前,我想知道这是正确的程序吗?我读过很多文章,它们似乎都指出了“测试备份策略”这一行。
现在我将每周、每天和每 5 分钟创建一个 SQL JOB,即每周完整备份、每日差异和每 5 分钟的日志备份。
你认为这个存储过程写得不正确还是你认为它应该适合我的场景?