经常性忘了给数据库备份,结果当数据库发生问题的时候,才发现备份是1个月以前的,那个后悔与懊恼还加惭愧啊,别提有对难受了。要认为的记住去备份比较难,每天事情又那么多,所以有了这个自动备份就不用愁了。先拷过来,再慢慢研究。
/*******************完整备份作业*******************/
--完整备份,每周一次
USE Master
GO
declare @str varchar(100)
set @str='D:\DBtext\jgj\DBABak\FullBak'+replace(replace(replace(convert(varchar,getdate(),20),'-',''),'',''),':','')+'.bak'
BACKUP DATABASE [demo] TO DISK=@str
WITH RETAINDAYS=15,NOFORMAT,NOINIT,
NAME=N'Demo完整备份',SKIP,NOREWIND,
NOUNLOAD,STATS=10
GO
/*******************差异备份作业*******************/
--截断日志
USE Master
GO
BACKUP LOG Demo WITH NO_LOG
GO
--收缩日志文件
USE Demo
GO
DBCC SHRINKFILE (N'Demo_log',0,TRUNCATEONLY)
GO
--差异备份,每天一次
USE Master
GO
declare @str varchar(100)
set @str='D:\DBtext\jgj\DBABak\DiffBak'+replace(replace(replace(convert(varchar,getdate(),20),'-',''),'',''),':','')+'.diff'
BACKUP DATABASE [Demo] TO DISK=@str
WITH DIFFERENTIAL,RETAINDAYS=8,NOFORMAT,NOINIT,
NAME=N'Demo差异备份',SKIP,NOREWIND,
NOUNLOAD,STATS=10
GO
/******************日志备份作业*******************/
--日志备份,每小时一次
USE Demo
GO
declare @str varchar(100)
set @str='D:\DBtext\jgj\DBABak\logbak'+replace(replace(replace(convert(varchar,getdate(),20),'-',''),'',''),':','')+'.trn'
BACKUP LOG [Demo] TO DISK=@str
WITH RETAINDAYS=3,NOFORMAT,NOINIT,
NAME=N'Demo日志备份',SKIP,NOREWIND,
NOUNLOAD,STATS=10
GO
--删除过期的备份文件,每天两次
declare @str varchar(100),@dir varchar(100),@fileName varchar(30)
set @dir='del D:\DBtext\jgj\DBABak\'
set @filename=left(replace(replace(replace(convert(varchar,getdate()-15,20),'-',''),'',''),':',''),8)
set @str=@dir+'fullbak'+@filename+'*.bak'
exec xp_cmdshell @str
set @filename=left(replace(replace(replace(convert(varchar,getdate()-8,20),'-',''),'',''),':',''),8)
set @str=@dir+'diffbak'+@filename+'*.diff'
exec xp_cmdshell @str
set @filename=left(replace(replace(replace(convert(varchar,getdate()-8,20),'-',''),'',''),':',''),8)
set @str=@dir+'logbak'+@filename+'*.trn'
exec xp_cmdshell @str
--完整备份,每周一次
USE Master
GO
declare @str varchar(100)
set @str='D:\DBtext\jgj\DBABak\FullBak'+replace(replace(replace(convert(varchar,getdate(),20),'-',''),'',''),':','')+'.bak'
BACKUP DATABASE [demo] TO DISK=@str
WITH RETAINDAYS=15,NOFORMAT,NOINIT,
NAME=N'Demo完整备份',SKIP,NOREWIND,
NOUNLOAD,STATS=10
GO
/*******************差异备份作业*******************/
--截断日志
USE Master
GO
BACKUP LOG Demo WITH NO_LOG
GO
--收缩日志文件
USE Demo
GO
DBCC SHRINKFILE (N'Demo_log',0,TRUNCATEONLY)
GO
--差异备份,每天一次
USE Master
GO
declare @str varchar(100)
set @str='D:\DBtext\jgj\DBABak\DiffBak'+replace(replace(replace(convert(varchar,getdate(),20),'-',''),'',''),':','')+'.diff'
BACKUP DATABASE [Demo] TO DISK=@str
WITH DIFFERENTIAL,RETAINDAYS=8,NOFORMAT,NOINIT,
NAME=N'Demo差异备份',SKIP,NOREWIND,
NOUNLOAD,STATS=10
GO
/******************日志备份作业*******************/
--日志备份,每小时一次
USE Demo
GO
declare @str varchar(100)
set @str='D:\DBtext\jgj\DBABak\logbak'+replace(replace(replace(convert(varchar,getdate(),20),'-',''),'',''),':','')+'.trn'
BACKUP LOG [Demo] TO DISK=@str
WITH RETAINDAYS=3,NOFORMAT,NOINIT,
NAME=N'Demo日志备份',SKIP,NOREWIND,
NOUNLOAD,STATS=10
GO
--删除过期的备份文件,每天两次
declare @str varchar(100),@dir varchar(100),@fileName varchar(30)
set @dir='del D:\DBtext\jgj\DBABak\'
set @filename=left(replace(replace(replace(convert(varchar,getdate()-15,20),'-',''),'',''),':',''),8)
set @str=@dir+'fullbak'+@filename+'*.bak'
exec xp_cmdshell @str
set @filename=left(replace(replace(replace(convert(varchar,getdate()-8,20),'-',''),'',''),':',''),8)
set @str=@dir+'diffbak'+@filename+'*.diff'
exec xp_cmdshell @str
set @filename=left(replace(replace(replace(convert(varchar,getdate()-8,20),'-',''),'',''),':',''),8)
set @str=@dir+'logbak'+@filename+'*.trn'
exec xp_cmdshell @str
SQLServer2005利用维护计划自动备份数据库
PS:1、SQL Server Management Studio Express数据库管理系统并没有维护计划,要在开发版或企业版才有。
2、需要在SQL Server Configuration Manager中,开启服务SQL Server Agent(实例名)。如果服务启动,在Management Studio中新建维护计划时,提示以下错误信息:
“代理XP”组件已作为此服务器安全配置的一部分被关闭。系统管理员可以使用sp_configure来启用“代理XP”。有关启用“代理XP”的详细信息,请参阅SQL Server联机丛书中的“外围应用配置器”。(ObjectExplorer)