企业管理器
--管理
--SQL Server代理
--右键作业
--新建作业
--"常规"项中输入作业名称
--"步骤"项
--新建
--"步骤名"中输入步骤名
--"类型"中选择"Transact-SQL 脚本(TSQL)"
--"数据库"选择执行命令的数据库
--"命令"中输入要执行的语句:
--确定
--"调度"项
--新建调度
--"名称"中输入调度名称
--"调度类型"中选择你的作业执行安排
--如果选择"反复出现"
--点"更改"来设置你的时间安排为一天一次
然后将SQL Agent服务启动,并设置为自动启动,否则你的作业不会被执行
设置方法:
我的电脑--控制面板--管理工具--服务--右键 SQLSERVERAGENT--属性--启动类型--选择"自动启动"--确定.
/*******************完整备份作业*******************/ --完整备份,每周一次 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
====================================================================== SQL还原 ====================================================================== 1、验证备份 ------------------------------------------------------------ restore headeronly from bak3 restore filelistonly from bak3 with file=1 restore labelonly from bak3 restore verifyonly from bak3 ---------------------------------------------------------------------- 2、从备份中还原 ------------------------------------------------------------------------- restore headeronly from bak1 restore database d1 from bak1 with file=2 --从完全备份中恢复 ---------------------------------------------------------------------- restore headeronly from bak2 --从差异备份中恢复 restore database d2 from bak2 with file=1,norecovery restore database d2 from bak2 with file=5,recovery ---------------------------------------------------------------------- restore headeronly from bak3 --从日志备份中恢复 restore database d3 from bak3 with file=1,norecovery restore log d3 from bak3 with file=2,norecovery restore log d3 from bak3 with file=3,norecovery restore log d3 from bak3 with file=4,norecovery restore log d3 from bak3 with file=5,recovery ---------------------------------------------------------------------- restore database d3 from bak3 with file=1,norecovery --恢复到指定时间 restore log d3 from bak3 with file=2,norecovery restore log d3 from bak3 with file=3,norecovery restore log d3 from bak3 with file=4,recovery,stopat='2003-08-15 11:29:00.000' ---------------------------------------------------------------------- restore database d5 filegroup='FG2' from bak5 with file=4,norecovery --还原文件组备份 restore log d5 from bak5 with file=5,norecovery restore log d5 from bak5 with file=7,recovery ---------------------------------------------------------------------- restore headeronly from bak6 --还原文件备份 restore database d5 file='d5_data3' from bak6 with file=6,norecovery restore log d5 from bak6 with file=7,norecovery restore log d5 from bak6 with file=9,recovery ---------------------------------------------------------------------- restore database d5 from bak6 with replace --删除现有数据库,从备份中重建数据库 ---------------------------------------------------------------------- create database d6 --move to将数据库文件移动到新位置 on primary (name=d6_data, filename='E:\Program Files\Microsoft SQL Server\MSSQL\data\d6_Data.MDF', size=2MB) log on (name=d6_log, filename='E:\Program Files\Microsoft SQL Server\MSSQL\data\d6_log.ldf', size=2MB) go backupdatabase d6 to bak6 with init drop database d6 restore database d6 from bak6 with move 'd6_data' to 'e:\data\d6\d6_data.mdf', move 'd6_log'to 'e:\data\d6\d6_log.ldf' sp_helpdb d6 ---------------------------------------------------------------------- 3、分离与重连接数据库 -------------------------------------- sp_detach_db 'd6' sp_attach_db 'd6','e:\data\d6\d6_data.mdf','e:\data\d6\d6_log.ldf' -------------------------------------- sp_detach_db d6 go create database d6 on primary (filename='e:\data\d6\d6_data.mdf') for attach go ---------------------------------------------------------------------- 4、恢复损坏的系统数据库 ---------------------------------------------------------------------- 1)先备份MASTER、MSDB 2)停止SQL服务,将MASTER数据库文件删除或者重命名。这样,SQL服务将不能启动。 3)系统数据库的还原 ----------------------------------------------- (1)如果SQL服务还能启动,则从备份中恢复系统数据库。 (2)如果SQL服务不能启动,则需要重建系统数据库。 使用SQL文件夹TOOLS\BINN目录下的Rebuildm.exe重建master数据库。 (3)创建备份设备,指向以前的备份设备。 (4)以单用户模式启动SQL cd programe files\microsoft sql server\mssql\binn sqlservr.exe -c -m (5)进查询分析器,从备份中恢复master数据库。 restore database master from masterbak restore database msdb from disk='e:\bak\msdb.bak' MASTER还原后,SQL中用户数据库的信息也会恢复。 (6)如果MASTER没有备份,则需要用sp_attach_db命令将用户数据库附加到新的MASTER数据库中。