gate_s

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
 --新建文件夹
 sp_configure 'show advanced options', 1;   
GO   
RECONFIGURE;   
GO   
sp_configure 'xp_cmdShell', 1;   
GO   
RECONFIGURE   
GO
declare @currentDate datetime,@Day varchar(20)
set @currentDate=getdate()
set @Day= cast(year(@currentDate) as varchar(4))+'_'+cast(month(@currentDate) as varchar(2))+'_'+cast(day(@currentDate) as varchar(2))
declare @cmd varchar(1000)
--set @cmd=N'mkdir D:\Program Files\Microsoft SQL Server实例根\MSSQL11.SQL0\MSSQL\Backup\AdventureWorks2012_'+@Day
set @cmd=N'mkdir D:\Program Files\AdventureWorks2012_'+@Day
--set @cmd=N'mkdir D:\db\AdventureWorks2012_'+@Day
 select @cmd
exec xp_cmdshell @cmd

--完备
go
declare @strDate varchar(30),@backupFile varchar(200),@backupLogFile varchar(200),@currentDate datetime,@Day varchar(20)
set @currentDate=getdate()
set @Day= cast(year(@currentDate) as varchar(4))+'_'+cast(month(@currentDate) as varchar(2))+'_'+cast(day(@currentDate) as varchar(2))
set @strDate=@Day+'_'+cast(datepart(hour,@currentDate) as varchar(2))+'_'+cast(datepart(minute,@currentDate) as varchar(2))+'_'+cast(datepart(ss,@currentDate) as varchar(2))

set @backupFile = 'D:\db\AdventureWorks2012_'+@Day+'\AdventureWorks2012_'+@strDate+'.bak'
backup database AdventureWorks2012 to disk=@backupFile WITH NOFORMAT, NOINIT, 
NAME = N'AdventureWorks2012-完整 数据库 备份', 
SKIP, NOREWIND, NOUNLOAD

--log备份
go
declare @strDate varchar(30),@backupFile varchar(200),@backupLogFile varchar(200),@currentDate datetime,@Day varchar(20)
set @currentDate=getdate()
set @Day= cast(year(@currentDate) as varchar(4))+'_'+cast(month(@currentDate) as varchar(2))+'_'+cast(day(@currentDate) as varchar(2))
set @strDate=@Day+'_'+cast(datepart(hour,@currentDate) as varchar(2))+'_'+cast(datepart(minute,@currentDate) as varchar(2))+'_'+cast(datepart(ss,@currentDate) as varchar(2))

set @backupLogFile='D:\db\AdventureWorks2012_'+@Day+'\AdventureWorks2012_'+@strDate+'.trn'
backup log AdventureWorks2012 to disk=@backupLogFile

 

posted on 2015-12-28 16:49  gate_s  阅读(293)  评论(0编辑  收藏  举报