SQL2000关于用代码进行备份等操作

来源:未知 责任编辑:智问网络 发表时间:2013-10-30 11:11 点击:
-参考/*******************完整备份作业*******************/
--完整备份,每周一次
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企业管理器提取的SQL脚本
 
-- 2012-02-22/10:10 上生成的脚本
-- 由: HEQUANXIN\Tyler
-- 服务器: (LOCAL)
 
BEGIN TRANSACTION           
  DECLARE @JobID BINARY(16) 
  DECLARE @ReturnCode INT   
  SELECT @ReturnCode = 0    
IF (SELECT COUNT(*) FROM msdb.dbo.syscategories WHERE name = N'Database Maintenance') < 1
  EXECUTE msdb.dbo.sp_add_category @name = N'Database Maintenance'
 
  -- 删除同名的警报(如果有的话)。
  SELECT @JobID = job_id    
  FROM   msdb.dbo.sysjobs   
  WHERE (name = N'DB 维护计划“FGPSN备份计划”的DB 备份作业。')      
  IF (@JobID IS NOT NULL)   
  BEGIN 
  -- 检查此作业是否为多重服务器作业 
  IF (EXISTS (SELECT  *
              FROM    msdb.dbo.sysjobservers
              WHERE   (job_id = @JobID) AND (server_id <> 0)))
  BEGIN
    -- 已经存在,因而终止脚本
    RAISERROR (N'无法导入作业“DB 维护计划“FGPSN备份计划”的DB 备份作业。”,因为已经有相同名称的多重服务器作业。', 16, 1)
    GOTO QuitWithRollback 
  END
  ELSE
    -- 删除[本地]作业
    EXECUTE msdb.dbo.sp_delete_job @job_name = N'DB 维护计划“FGPSN备份计划”的DB 备份作业。'
    SELECT @JobID = NULL
  END
 
BEGIN
 
  -- 添加作业
  EXECUTE @ReturnCode = msdb.dbo.sp_add_job @job_id = @JobID OUTPUT , @job_name = N'DB 维护计划“FGPSN备份计划”的DB 备份作业。', @owner_login_name = N'sa', @description = N'没有可用的描述。', @category_name = N'Database Maintenance', @enabled = 1, @notify_level_email = 0, @notify_level_page = 0, @notify_level_netsend = 0, @notify_level_eventlog = 2, @delete_level= 0
  IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
 
  -- 添加作业步骤
  EXECUTE @ReturnCode = msdb.dbo.sp_add_jobstep @job_id = @JobID, @step_id = 1, @step_name = N'第1 步', @command = N'EXECUTE master.dbo.xp_sqlmaint N''-PlanID FFC3A233-504F-4735-BD9A-7E96C9AD551D -WriteHistory  -VrfyBackup -BkUpMedia DISK -BkUpDB  -UseDefDir  -DelBkUps 1MONTHS -BkExt "BAK"''', @database_name = N'master', @server = N'', @database_user_name = N'', @subsystem = N'TSQL', @cmdexec_success_code = 0, @flags = 4, @retry_attempts = 0, @retry_interval = 0, @output_file_name = N'', @on_success_step_id = 0, @on_success_action = 1, @on_fail_step_id = 0, @on_fail_action = 2
  IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
  EXECUTE @ReturnCode = msdb.dbo.sp_update_job @job_id = @JobID, @start_step_id = 1
 
  IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
 
  -- 添加作业调度
  EXECUTE @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id = @JobID, @name = N'第1 调度', @enabled = 1, @freq_type = 4, @active_start_date = 20120222, @active_start_time = 20000, @freq_interval = 1, @freq_subday_type = 1, @freq_subday_interval = 0, @freq_relative_interval = 0, @freq_recurrence_factor = 0, @active_end_date = 99991231, @active_end_time = 235959
  IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
 
  -- 添加目标服务器
  EXECUTE @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @JobID, @server_name = N'(local)'
  IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
 
END
COMMIT TRANSACTION         
GOTO   EndSave             
QuitWithRollback:
  IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
EndSave:
 

摘自 Tyler 的专栏
    发表评论
    请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
    用户名: 验证码:点击我更换图片
    最新评论 更多>>

    推荐热点

    • sql常见面试题
    • SQL SERVER 2005性能之跟踪
    • SQL编程(一)
    • LINUX上RMAN自动备份脚本
    • sql server面试题
    • 如何将多个SQL查询统计结果一次显示出来
    • 浅谈SQL Server中的事务日志(三)----在简单恢复模式下日志的角色
    • SQL小技巧系列 --- 行转列合并
    • sql server 列转行
    网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
    Copyright © 2008-2015 计算机技术学习交流网. 版权所有

    豫ICP备11007008号-1