SQL Server存储过程备份数据库的脚本-干货
拿AAAAAAAA数据库为例子,除了汉字标注的替换真实的数据,其他都不动
CREATE proc [dbo].[p_log_backupAAAAAAAA]
@dbname sysname='',
@bkflogname nvarchar(260)='',
@bkpath nvarchar(260)='w:'
as
declare @sql varchar(8000)
exec master..xp_cmdshell 'net use w: \\IP\共享目录 远程访问密码 /user:dataserver\远程访问账户'
if isnull(@dbname,'')='' set @dbname=db_name()
if right(@bkpath,1)<>'\' set @bkpath=@bkpath+'\'
if isnull(@bkflogname,'')='' set @bkflogname='\DBNAME\_\DATE\_\TIME\.bak'
set @bkpath=@bkpath+convert(varchar,getdate(),112)+'\'+'EVENT_LOG'
set @sql='md '+@bkpath+'\'+@dbname
exec master..xp_cmdshell @sql,no_output
set @bkflogname=replace(replace(replace(@bkflogname,'\DBNAME\',@dbname)
,'\DATE\',convert(varchar,getdate(),112))
,'\TIME\',replace(convert(varchar,getdate(),108),':',''))
set @sql='backup LOG '+@dbname
+' to disk='''+@bkpath+'\'+@dbname+'\'+@bkflogname+''''+' WITH RETAINDAYS = 3, NOFORMAT, NOINIT, SKIP, REWIND, NOUNLOAD, STATS = 10'
exec(@sql)
exec master..xp_cmdshell 'net use w: /del'