|
网上找到一篇东西,觉得不错,就把它贴出来,供有此需要的朋友参考
数据库自动备份方案
1建立自动任务:
EXECUTE pr_AutoBackup "baktest","d:\Database",23,3
设置为每小时执行一次。
2.在Server – SQL Server – Configuration 中选中Auto Start Executive at Boot Time
3.在Master数据库建立存储过程
CREATE PROCEDURE pr_AutoBackup @db_to_bak varchar(20),
@file_path varchar(20),
@on_hour int,
@remain_day int
AS
DECLARE @this_day int,
@bak_count int,
@str_file_name varchar(30),
@str_del_device varchar(20),
@str_last_device varchar(20),
@last_day int,
@this_hour int,
@strSQL varchar(60)
SELECT @this_day = datepart(dayofyear,getdate())
SELECT @bak_count = count(name),@str_last_device = max(name),
@last_day = max(convert(int,substring(name,charindex("_",name)+1,3)))
FROM sysdevices
WHERE charindex("_",name)=datalength(@db_to_bak)+1
AND substring(name,1,datalength(@db_to_bak))=@db_to_bak
IF SUBSTRING(@file_path,datalength(@file_path),1)<>”\”
SELECT @file_path = @file_path + “\”
SELECT @this_hour = datepart(hour,getdate())
IF ((@this_hour = @on_hour)and(@this_day<>@last_day))or(@bak_count = 0)
BEGIN
SELECT @str_last_device = @db_to_bak+"_"+convert(varchar(3),@this_day)
SELECT @str_file_name = @file_path + @str_last_device + “.dat”
SELECT @strSQL =“DUMP TRANSACTION ” + @db_to_bak + “ WITH TRUNCATE_ONLY”
EXECUTE(@strSQL)
SELECT @strSQL =“sp_addumpdevice ‘DISK’,’”+@str_last_device + “’,’”+@str_file_name+”’”
EXECUTE(@strSQL)
SELECT @strSQL =“DUMP DATABASE “+ @db_to_bak + “ TO “ + @str_last_device
EXECUTE(@strSQL)
SELECT @str_del_device = @db_to_bak + ”_” + convert(varchar(4),@this_day - @remain_day)
SELECT @bak_count = count(name)
FROM sysdevices
WHERE name = @str_del_device
IF (@bak_count=1)
BEGIN
SELECT @strSQL =“sp_dropdevice “+ @str_del_device +”,DELFILE”
EXECUTE(@strSQL)
END
END
ELSE
BEGIN
SELECT @strSQL =“DUMP TRANSACTION “+ @db_to_bak + “ TO “ + @str_last_device
EXECUTE(@strSQL)
END |
|