主要四個步驟
第一步備份出BAK數(shù)據(jù)庫文件工扎,
第二步開啟 XP_CMDSHLL,
第三步打包壓縮數(shù)據(jù)庫備份文件后并刪除原備份文件
第二步關閉 XP_CMDSHLL,
一、執(zhí)行備份的命令:
declare @filename varchar(200)
set @filename='D:\'+convert(char(10),getdate(),120)+'.bak'? --設置備份文件的路徑和文件名
print @filename
backup database[DatabaseName] to disk=@filename with NOINIT,NOUNLOAD,NAME='backup',NOSKIP,STATS=10,NOFORMAT? --執(zhí)行備份
二蝇庭、開啟 xp_cmdshell (SQL 2012為安全默認不開啟的)
-- To allow advanced options to be changed.
EXEC sp_configure 'show advanced options', 1
GO
-- To update the currently configured value for advanced options.
RECONFIGURE
GO
-- To enable the feature.
EXEC sp_configure 'xp_cmdshell', 1
GO
-- To update the currently configured value for this feature.
RECONFIGURE
GO
三、壓縮并刪除源文件的命令:(winrar不能放在有空格的文件夾)
declare @file varchar(200)
set @file='c:\winrar.exe a -ep -df D:\DatebaseName_'+convert(char(10),getdate(),120)+'.rar D:\'+convert(char(10),getdate(),120)+'.bak'? --將壓縮備份文件并刪除源文件的winrar命令行命令
exec master..xp_cmdshell @file? --執(zhí)行命令
四捡硅、為了安全可以備份完成后關閉xp_cmdshell
-- To allow advanced options to be changed.
EXEC sp_configure 'show advanced options', 1
GO
-- To update the currently configured value for advanced options.
RECONFIGURE
GO
-- To enable the feature.
EXEC sp_configure 'xp_cmdshell', 0
GO
-- To update the currently configured value for this feature.
RECONFIGURE
GO