Goodspeed

导航

备份DB

alter procedure dbo.usp_backup_database
  @p_dbname varchar(50),        -- 备份数据库名称
  @p_type varchar(50)          -- 备份类型,full or log
as
  declare @sql varchar(1000)
  declare @role tinyint
  select 
    @role = dhar.role
  from master.sys.availability_replicas as ar
  left join master.sys.dm_hadr_availability_replica_states as dhar on ar.replica_id = dhar.replica_id
  where exists
  (
    select * from master.sys.availability_groups
    where name='ag_crm_db'
    and ar.group_id = group_id
  )and ar.replica_server_name = SERVERPROPERTY('MachineName') 

  if @role != 1
  begin
    return
  end

  if @p_type = 'full'
  begin
      set @sql = 'backup database [' + @p_dbname + '] to disk = ''D:\backup\' + @p_dbname + '.full.bak'' with format, COMPRESSION'
  end

  if @p_type = 'diff'
  begin
      set @sql = 'backup database [' + @p_dbname + '] to disk = ''D:\backup\' + @p_dbname + '.'+ CAST(DATEPART(weekday,getdate()) as char(1))+'.diff'' with DIFFERENTIAL, format, COMPRESSION'
  end

  if @p_type = 'log'
  begin
      set @sql = 'backup log  [' + @p_dbname + '] to disk = ''D:\backup\' + @p_dbname + '.'+ CAST(DATEPART(hour,getdate()) as varchar(2))+'.log'' with format, COMPRESSION'
  end

  execute(@sql)

 

posted on 2005-03-17 14:27  Goodspeed  阅读(1669)  评论(2编辑  收藏  举报