SQLSERVER异机备份

 1 /*
 2 作者:landv
 3 功能:异机备份
 4 开发时间:2016年7月2日 15:27:08
 5 
 6 */
 7 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_backupdb]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) 
 8 drop procedure [dbo].[p_backupdb] 
 9 GO
10 create proc p_backupdb 
11 @dbname sysname='AIS20110306202234',
12 @bkpath nvarchar(260)='\\k3ser\landvback\',--exec master..xp_cmdshell 'net use \\计算机名\共享目录 "密码" /USER:计算机名\用户我';(\\计算机名\共享目录\备份文件名)
13 @bkfname nvarchar(260)='\DBNAME\_backup_\DATE\_\TIME\.BAK',
14 @bktype nvarchar(10)='DB',
15 @appendfile bit=1
16 as 
17 declare @sql varchar(8000) 
18 if isnull(@dbname,'')='' set @dbname=db_name() 
19 if isnull(@bkpath,'')='' set @bkpath=''
20 if isnull(@bkfname,'')='' set @bkfname='\DBNAME\_\DATE\_\TIME\.BAK' 
21 set @bkfname=replace(replace(replace(@bkfname,'\DBNAME\',@dbname)
22 ,'\DATE\',convert(varchar,getdate(),112)) 
23 ,'\TIME\',replace(convert(varchar,getdate(),108),':','')) 
24 set @sql='backup '+case @bktype when 'LOG' then 'log ' else 'database ' end +@dbname 
25 +' to disk='''+@bkpath+@bkfname 
26 +''' with '+case @bktype when 'DF' then 'DIFFERENTIAL,' else '' end 
27 +case @appendfile when 1 then 'NOINIT' else 'INIT' end 
28 print @sql 
29 exec(@sql) 
30 go
31 exec p_backupdb

 实在是太懒的用系统自带了,设置起来好麻烦呀,直接敲码吧,哈哈

posted @ 2016-07-02 15:54  landv  阅读(1416)  评论(0编辑  收藏  举报