须事先准备一个工具curl,把它放在c盘。然后,在数据库所在服务器安装7z。最后把这2个存储过程执行,在sqlserver的代理中新建作业,即可实现备份操作。
--备份指定数据库到本地和远程指定位置(7-zip压缩) Create PROCEDURE [dbo].[BackUpDB7z] @dbname sysname --数据库名 AS declare @backupfile nvarchar(200) --本地备份文件名 declare @backuplogfile nvarchar(200) --本地日志备份文件名 declare @archivefile nvarchar(200) --本地压缩文件名 declare @archivelogfile nvarchar(200) --本地压缩文件名 declare @target_localpath nvarchar(800) --本地备份路径 declare @target_backupfile nvarchar(1000) --本地备份文件完整路径 declare @target_backuplogfile nvarchar(1000) --本地备份日志文件完整路径 declare @target_archivefile nvarchar(1000) --本地压缩文件完整路径 declare @target_archivelogfile nvarchar(1000) --本地日志压缩文件完整路径 declare @descr nvarchar(100) --备份文件描述 declare @date datetime declare @date_str varchar(100) --日期时间字符串 declare @cmd nvarchar(4000) --要执行的命令 declare @log_file nvarchar(200) -- declare @cmd_7z nvarchar(200) declare @7z_opt nvarchar(1000) declare @cmd_tool nvarchar(200)=‘C:\curl\curl -T‘ declare @remotepath nvarchar(800)=‘‘ --远程备份路径 declare @ftp_user nvarchar(300)=‘‘--ftp帐号 declare @ftp_pw nvarchar(200)=‘‘--ftp密码 declare @ftp_opt nvarchar(1000)=‘ftp://‘ declare @localpath nvarchar(800)=‘‘ --本地备份路径 --参数设置 select @log_file=‘d:\shellcmd_log.txt‘ select @cmd_7z=‘7z ‘ select @7z_opt=‘a -t7z -mx=9 -mmt=on‘ select @date=getdate() select @[email protected]+cast(year(@date) as nvarchar)+‘年‘+cast(month(@date) as nvarchar)+‘月‘+cast(day(@date) as nvarchar)+‘日完全备份‘ select @[email protected]+‘.bak‘ select @[email protected]+‘.Log.bak‘ select @date_str=convert(varchar(100), @date, 120) select @date_str=REPLACE(@date_str,‘-‘,‘‘) select @date_str=REPLACE(@date_str,‘ ‘,‘_‘) select @date_str=REPLACE(@date_str,‘:‘,‘‘) select @[email protected]+‘_‘[email protected]_str+‘.7z‘ select @[email protected]+‘_‘[email protected]_str+‘.Log.7z‘ if (@localpath<>‘‘ and right(@localpath,1)<>‘\‘) select @[email protected]+‘\‘ else select @target_localpath=@localpath select @[email protected]_localpath+@backupfile select @[email protected]_localpath+@backuplogfile select @[email protected]_localpath+@archivefile select @[email protected]_localpath+@archivelogfile --收缩数据库 --dump transaction @dbname with no_log --DBCC SHRINKDATABASE (@dbname, 0,TRUNCATEONLY) --备份数据库 backup database @dbname to disk[email protected]_backupfile with FORMAT , description=@descr --备份日志 backup log @dbname to disk=@target_backuplogfile with FORMAT exec [ClearDbLog] @dbname --压缩数据库 select @[email protected]_7z+‘ ‘[email protected]_opt+‘ ‘[email protected]_archivefile+‘ ‘ +@target_backupfile --执行命令 exec xp_cmdshell @cmd --压缩日志 select @[email protected]_7z+‘ ‘[email protected]_opt+‘ ‘[email protected]_archivelogfile+‘ ‘ +@target_backuplogfile --执行命令 exec xp_cmdshell @cmd --传送压缩文件到远程服务器 if @remotepath<>‘‘ begin select @[email protected]_tool+‘ ‘[email protected]_archivefile+‘ -u ‘[email protected]_user+‘:‘[email protected]_pw+‘ ‘[email protected]_opt+‘‘+@remotepath --print(@cmd) --执行命令 exec xp_cmdshell @cmd select @[email protected]_tool+‘ ‘[email protected]_archivelogfile+‘ -u ‘[email protected]_user+‘:‘[email protected]_pw+‘ ‘[email protected]_opt+‘‘+@remotepath --print(@cmd) --执行命令 exec xp_cmdshell @cmd end --删除本地备份bak文件 select @cmd=‘del ‘+@target_backupfile --print(@cmd) --执行命令 exec xp_cmdshell @cmd select @cmd=‘del ‘+@target_backuplogfile --print(@cmd) --执行命令 exec xp_cmdshell @cmd --删除本地备份7z文件 select @cmd=‘del ‘+@target_archivefile --print(@cmd) --执行命令 exec xp_cmdshell @cmd select @cmd=‘del ‘+@target_archivelogfile --print(@cmd) --执行命令 exec xp_cmdshell @cmd
--清除数据库日志 CREATE PROCEDURE [dbo].[ClearDbLog] @DataBase sysname AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; declare @sql2005 varchar(max) declare @sql2008 varchar(max) set @sql2005=‘ DUMP TRANSACTION ‘[email protected]+‘ WITH NO_LOG; DBCC SHRINKDATABASE (‘[email protected]+‘, 0,TRUNCATEONLY); ‘ set @sql2008=‘ ALTER DATABASE ‘[email protected]+‘ SET RECOVERY SIMPLE; DBCC SHRINKDATABASE (‘[email protected]+‘, 0,TRUNCATEONLY); ALTER DATABASE ‘[email protected]+‘ SET RECOVERY FULL; ‘ --print(@sql); exec(@sql2008); END GO
时间: 2024-10-09 18:46:02