--要备份到哪一服务的IP网络位置,要提前打开文件夹共享.这里还要输入用户名和密码,下面这一行是建立共享
exec master..xp_cmdshell ‘net use \\192.168.8.108\DBBak Password /user:192.168.1.108\administrator‘,NO_OUTPUT
go
declare @s nvarchar(200),@del nvarchar(200)
select @s=‘‘,@del=‘‘
--自动备份,并删除4天前的备份。可以随意改数字.
declare datebak cursor for
select
[bak]=‘backup database ‘+quotename(Name)+‘ to disk =‘‘\\192.168.8.108\DBBak\‘+Name+‘(‘+convert(varchar(10),getdate(),120)+‘)‘+‘.bak‘‘ with init‘,
[del]=‘exec master..xp_cmdshell ‘‘ del \\192.168.8.108\DBBak\‘+Name+‘(‘+convert(varchar(10),getdate()-4,120)+‘)‘+‘.bak‘‘, no_output‘
from master..sysdatabases where dbid>4 --不备份系统数据库
open datebak
fetch next from datebak into @s,@del
while @@fetch_status=0
begin
exec (@del)
exec(@s)
fetch next from datebak into @s,@del
end
close datebak
deallocate datebak
go
--删除共享链接
exec master..xp_cmdshell ‘net use \\192.168.8.108\DBBak/delete‘
go
原文地址:https://www.cnblogs.com/q149072205/p/9068438.html