USE [master]
GO
/****** Object:  StoredProcedure [dbo].[shiming_backup]    Script Date: 12/03/2013 11:41:44 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[shiming_backup] AS
declare
@prefix nvarchar(100),
@datefile nvarchar(100),
@bakfile nvarchar(100),
@rarfile nvarchar(100),
@delcmd nvarchar(100),
@rarcmd nvarchar(150)
set @prefix='E:/dbbackup/' --备份文件夹
set @datefile='shiming'+convert(varchar(10),getdate(),120)
set @bakfile=@prefix+@datefile+'.bak'
set @rarfile=@prefix+@datefile+'.rar'
BACKUP DataBASE shiming TO DISK = @bakfile WITH INIT , NOUNLOAD , NAME = N'shiming数据备份', NOSKIP , STATS = 10, NOFORMAT
set @delcmd = 'del '+@prefix+@datefile+'.rar'   --先清空RAR备份文件夹
set @rarcmd ='rar a -df -ep1 '+@rarfile+' '+@bakfile --压缩后删除源文件并在压缩文件中删除路径

EXEC sp_configure 'show advanced options', 1;RECONFIGURE;
EXEC sp_configure 'xp_cmdshell', 1;RECONFIGURE;

exec master..xp_cmdshell  @delcmd
exec master..xp_cmdshell  @rarcmd

EXEC sp_configure 'xp_cmdshell', 0;RECONFIGURE;
EXEC sp_configure 'show advanced options', 0;RECONFIGURE;