SQLSERVER异机备份

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

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