以下为SQL脚本,本人以执行计划来调用,所以改成了执行命令,大家可根据自己需要改为存储过程使用

DECLARE @bak_path nvarchar(4000)='E:\MsBackUp\SqlAutoBackup\'       --备份路径;
DECLARE @baktype int = 0               --备份类型为全备,1为差异备,2为日志备份
DECLARE @type int = 3                  --设置需要备份的库,0为全部库,1为系统库,2为全部用户库,3为指定库,4为排除指定库;
DECLARE @dbnames nvarchar(4000)='DB_ERP_JD,DB_WMS_JD,DB_FMS_JD,TEST_ERP_JD,TEST_WMS_JD,TEST_FMS_JD'        --需要备份或排除的数据库,用,隔开,当@type=3或4时生效
DECLARE @overdueDay int = 0           --设置过期天数,默认天;
DECLARE @compression int =0               --是否采用sql2008的压缩备份,0为否,1为采用压缩--sql server 2005/2008备份/删除过期备份T-sql 版本v1.0
/*
desc  :适用于sql2005/2008备份,自动生成库文件夹,可以自定义备份类型和备份库名等可以自定义备份过期的天数删除过期备份功能不会删除最后一次备份,哪怕已经过期如果某库不再备份,那么也不会再删除之前过期的备份 如有错误请指正,谢谢.
*/set nocount on
--开启xp_cmdshell支持
exec sp_configure 'show advanced options', 1
reconfigure with override
exec sp_configure 'xp_cmdshell', 1
reconfigure with override
exec sp_configure 'show advanced options', 0
reconfigure with override
print char(13)+'------------------------'--判断是否填写路径
if isnull(@bak_path,'')=''beginprint('error:请指定备份路径')return end--判断是否指定需要备份的库
if isnull(ltrim(@baktype),'')=''beginprint('error:请指定备份类型aa:0为全备,1为差异备,2为日志备份')return end
elsebeginif @baktype not between 0 and 2beginprint('error:指定备份类型只能为,1,2:  0为全备,1为差异备,2为日志备份')return endend
--判断是否指定需要备份的库
if isnull(ltrim(@type),'')=''beginprint('error:请指定需要备份的库,0为全部库,1为系统库,2为全部用户库,3为指定库,4为排除指定库')return end
elsebeginif @type not between 0 and 4beginprint('error:请指定需要备份的库,0为全部库,1为系统库,2为全部用户库,3为指定库,4为排除指定库')return endend--判断指定库或排除库时,是否填写库名
if @type>2if @dbnames=''beginprint('error:备份类型为'+ltrim(@type)+'时,需要指定@dbnames参数')return end--判断指定指定过期时间
if isnull(ltrim(@overdueDay),'')=''
beginprint('error:必须指定备份过期时间,单位为天,0为永不过期')return
end--判断是否支持压缩
if @compression=1 if charindex('2008',@@version)=0 or charindex('Enterprise',@@version)=0beginprint('error:压缩备份只支持sql2008企业版')return end--判断是否存在该磁盘
declare @drives table(drive varchar(1),[size] varchar(20))
insert into @drives exec('master.dbo.xp_fixeddrives')
if not exists(select 1 from @drives where drive=left(@bak_path,1))beginprint('error:不存在该磁盘:'+left(@bak_path,1))return end--格式化参数
select @bak_path=rtrim(ltrim(@bak_path)),@dbnames=rtrim(ltrim(@dbnames))
if right(isnull(@bak_path,''),1)!='' set @bak_path=@bak_path+''
if isnull(@dbnames,'')!='' set @dbnames = ','+@dbnames+','
set @dbnames=replace(@dbnames,' ','')--定义变量
declare @bak_sql nvarchar(max),@del_sql nvarchar(max),@i int,@maxid int
declare @dirtree_1 table (id int identity(1,1) primary key,subdirectory nvarchar(600),depth int,files int)
declare @dirtree_2 table (id int identity(1,1) primary key,subdirectory nvarchar(600),depth int,files int,
dbname varchar(300),baktime datetime,isLastbak int)
declare @createfolder nvarchar(max),@delbackupfile nvarchar(max),@delbak nvarchar(max)--获取需要备份的库名--------------------start
declare @t table(id int identity(1,1) primary key,name nvarchar(max))
declare @sql nvarchar(max)
set @sql = 'select name from sys.databases where state=0 and name!=''tempdb''  '+ case when @baktype=2 then ' and recovery_model!=3 ' else '' end+ case @type when 0 then 'and 1=1'when 1 then 'and database_id<=4'when 2 then 'and database_id>4'when 3 then 'and charindex('',''+Name+'','','''+@dbnames+''')>0'when 4 then 'and charindex('',''+Name+'','','''+@dbnames+''')=0 and database_id>4'else '1>2' end
insert into @t exec(@sql)
--获取需要备份的库名---------------------end--获取需要创建的文件夹------------------start
insert into @dirtree_1 exec('master.dbo.xp_dirtree '''+@bak_path+''',0,1')
select @createfolder=isnull(@createfolder,'')+'exec master.dbo.xp_cmdshell ''md '+@bak_path+''+name+''',no_output '+char(13)
from @t as a left join @dirtree_1 as b on a.name=b.subdirectory and b.files=0 and depth=1 where  b.id is null
--获取需要创建的文件夹-------------------end--生成处理过期备份的sql语句-------------start
if @overdueDay>0
begininsert into @dirtree_2(subdirectory,depth,files) exec('master.dbo.xp_dirtree '''+@bak_path+''',0,1')if @baktype =0 delete from @dirtree_2 where depth=1 or files=0 or charindex('_Full_bak_',subdirectory)=0 if @baktype =1 delete from @dirtree_2 where depth=1 or files=0 or charindex('_Diff_bak_',subdirectory)=0 if @baktype=2delete from @dirtree_2 where depth=1 or files=0 or charindex('_Log_bak_',subdirectory)=0 if exists(select 1 from @dirtree_2)delete from @dirtree_2 where isdate(left(right(subdirectory,19),8)+' '+ substring(right(subdirectory,20),11,2) + ':' +  substring(right(subdirectory,20),13,2) +':'+substring(right(subdirectory,20),15,2) )=0if exists(select 1 from @dirtree_2)update @dirtree_2 set dbname = case when @baktype=0 then left(subdirectory,charindex('_Full_bak_',subdirectory)-1)when @baktype=1 then left(subdirectory,charindex('_Diff_bak_',subdirectory)-1) when @baktype=2 then left(subdirectory,charindex('_Log_bak_',subdirectory)-1) else '' end    ,baktime=left(right(subdirectory,19),8)+' '+ substring(right(subdirectory,20),11,2) + ':' +  substring(right(subdirectory,20),13,2) +':'+substring(right(subdirectory,20),15,2) from @dirtree_2 as adelete @dirtree_2 from @dirtree_2 as a left join @t as b on b.name=a.dbname where b.id is nullupdate @dirtree_2 set isLastbak= case when (select max(baktime) from @dirtree_2 where dbname=a.dbname)=baktime then 1 else 0 end from @dirtree_2 as aselect @delbak=isnull(@delbak,'')+'exec master.dbo.xp_cmdshell ''del '+@bak_path+''+dbname+''+subdirectory+''',no_output '+char(13) from @dirtree_2 where isLastbak=0 and datediff(day,baktime,getdate())>@overdueDay
end
--生成处理过期备份的sql语句--------------endbegin tryprint(@createfolder)  --创建备份所需文件夹exec(@createfolder)   --创建备份所需文件夹
end try
begin catchprint 'err:'+ltrim(error_number())print 'err:'+error_message()return
end catchselect @i=1 ,@maxid=max(id) from @t
while @i<=@maxid
beginselect @bak_sql=''+char(13)+'backup '+ case when @baktype=2 then 'log ' else 'database ' end+quotename(Name)+' to disk='''+@bak_path + Name+''+Name+ case when @baktype=0 then '_Full_bak_' when @baktype=1 then '_Diff_bak_' when @baktype=2 then '_Log_bak_' else null end + case when @compression=1 then 'compression_' else '' end+replace(replace(replace(convert(varchar(20),getdate(),120),'-',''),' ','_'),':','')+case when @baktype=2 then '.trn' when @baktype=1 then '.dif' else '.bak' end +'''' + case when @compression=1 or @baktype=1 then ' with ' else '' end+ case when @compression=1 then 'compression,' else '' end+ case when @baktype=1 then 'differential,' else '' end+ case when @compression=1 or @baktype=1 then ' noformat' else '' end from @t where id=@iset @i=@i+1begin tryprint(@bak_sql)--循环执行备份exec(@bak_sql) --循环执行备份end trybegin catchprint 'err:'+ltrim(error_number())print 'err:'+error_message()end catch
endbegin tryprint(@delbak)   --删除超期的备份exec(@delbak)    --删除超期的备份
end try
begin catchprint 'err:'+ltrim(error_number())print 'err:'+error_message()
end catch--关闭xp_cmdshell支持
exec sp_configure 'show advanced options', 1
reconfigure with override
exec sp_configure 'xp_cmdshell', 1
reconfigure with override
exec sp_configure 'show advanced options', 0
reconfigure with override

转载于:https://www.cnblogs.com/teamate/p/3967747.html

[Irving] SQL 2005/SQL 2008 备份数据库并自动删除N天前备份的脚本相关推荐

  1. linux删除mysql临时文件_linux下mysql自动备份数据库与自动删除临时文件_MySQL

    bitsCN.com linux下mysql自动备份数据库与自动删除临时文件 一.每日23:00自动删除临时文件 首先查看一下crontab的任务列表:crontab -l然后新建:crontab - ...

  2. linux mysql 临时文件_linux下mysql自动备份数据库与自动删除临时文件

    一.每日23:00自动删除临时文件 首先查看一下crontab的任务列表: crontab -l 然后新建: crontab -e 添加一行: 00 03 * * * rm -rf /www/cmst ...

  3. 删除mysql临时文件_mysql自动备份数据库与自动删除临时文件

    mysql自动备份数据库与自动删除临时文件,有需要的朋友可以参考下. 一.每日23:00自动删除临时文件 首先查看一下crontab的任务列表: crontab -l 然后新建: crontab -e ...

  4. Linux自动删除n天前备份

    Linux是一个很能自动产生文件的系统,日志.邮件.备份等.因此需要设置让系统定时清理一些不需要的文件. 语句写法: find 对应目录 -mtime +天数 -name "文件名" ...

  5. Linux下定时切割Mongodb数据库日志并删除指定天数前的日志记录(转)

    文章转自:http://www.osyunwei.com/archives/8998.html 说明: 操作系统:CentOS Mongodb安装目录:/usr/local/mongodb Mongo ...

  6. mysql差异备份数据库get shell_shell进行完整和增量备份mysql数据库

    shell进行完整和增量备份mysql数据库 文档介绍 本文档采用mysqldump 对数据库进行备份,mysqldump 是采用SQL级别的备份机制,它将数据表导成 SQL脚本文件,在不同的 MyS ...

  7. RMAN备份数据库_制作和更新RMAN增量备份(Incremental Backup)

    增量备份只拷贝从指定的之前的备份以后更改过的数据文件块.使用BACKUP命令来创建增量备份. 增量备份要么是累积增量备份,要么是差异增量备份. 虽然备份的内容相同,BACKUP DATABASE和BA ...

  8. linux备份数据库软件有哪些内容,Linux网络备份MySQL数据库的应用方法

    Linux网络备份MySQL的方法 案例:一个中小Linux网站的管理员,系统构架是:RHEL 4.0 + PHP 4.3 + Mysql 4.03 +Apache 1.23.日均IP访问量在200次 ...

  9. 2压缩备份数据库_为什么您的企业需要备份数据库

    数据是开展业务的重要组成部分.如果任何企业意外丢失数据,则可能导致巨大的损失.因此,为了保护有价值的信息,企业需要备份其数据库. 本文提供了有关数据库备份的重要性以及如何进行备份的所有信息. 什么是数 ...

  10. windows 下 用 dos 备份 oracle 数据库,并删除七天前的备份

    本文参考了其他 dos 计算日期的指令,参考比较多所以没一一指明,若有相关作者看到需要注明出处,请联系我,我必改正,注明出处[联系方式在博文尾部]: 备份[导出] @echo offsetlocal ...

最新文章

  1. CRM 2016解读
  2. 实验结果报告与实验总结_教科版科学四年级上册实验报告
  3. 最近比较火的一款字节产品
  4. 一个老干部对即将从政的儿子的赠言
  5. php 日期format不要零_PHP格式化日期用法代码,包括前导零示例
  6. python 比较运算符_Python比较运算符
  7. C#净化版WebApi框架
  8. 我晕,原来是这个问题!
  9. OCR文字识别技术总结(二)
  10. matlab中.m文件访问simulink
  11. 无线中继后要不要关闭dhcp服务器,tplink无线路由器WDS桥接后副路由开启DHCP 好还是关闭好。...
  12. DEFCON携手百度安全落地中国,打造国际化网络安全交流平台
  13. 【数据结构和算法】赫夫曼树 | 实战演练(二)
  14. 皇帝成长计划html文件打不开,皇帝成长计划源代码修改(共6篇).doc
  15. python---引用其他py文件中的函数
  16. OWA附件隐藏excle格式下载按钮
  17. 首席商学院新媒体运营黎想:抖音直播入门教学,点这!
  18. 数据结构考纲笔记概览
  19. 漏洞通告 | ​Apache Spark UI命令漏洞;Grails远程代码漏洞;Confluence Questions漏洞
  20. 编程练习【验证外星语词典】

热门文章

  1. zipkin+elk微服务日志收集分析系统
  2. 计算机的删除快捷键,电脑上的删除快捷键是什么?
  3. 集集自助服务密码重置
  4. wifi无法获取ip地址的解决方法
  5. 心电算法(Discuss of ECG Analysis Algorithm)
  6. php工程师的学习之道以及需要掌握的知识体系
  7. cvpr 深度估计_近两年 CVPR ICCV ECCV 相机位姿估计、视觉定位、SLAM相关论文汇总...
  8. 什么是数据科学家与数据科学
  9. 百度地图定位不准的问题
  10. 5大国外广告联盟赚美金项目,诱人的美金在向你招手!