운영중인 DB가 기 구성된 로그전달이 복원되지 않는 상황에서 미러링으로 이중화 전환이 필요

피크타임 기준 분당 최대 기가단위 TRN이 적재되어 작업 직전까지 N회 TRN 백업 복원 작업 반복 필요

(사전작업(작업당일)의 항목 2 반복 실행)

 

 

스크립트 : 

사전작업 

---------------------------------------------------------
---------------------------------------------------------
--0. 계정생성 (In Secondary)
---------------------------------------------------------
---------------------------------------------------------
 /*
 주서버에서 아래 링크 참고하여 수행한 결과 추출 후 보조서버에서 실행
 https://docs.microsoft.com/ko-kr/troubleshoot/sql/security/transfer-logins-passwords-between-instances
 */ 
 
 
---------------------------------------------------------
---------------------------------------------------------
--1.서버수준 권한 부여 (In Secondary)
---------------------------------------------------------
---------------------------------------------------------
/*
주서버에서 하기 쿼리 수행 후 결과를 보조 서버에서 실행 
*/ 
 
select  'EXEC master..sp_addsrvrolemember @loginame = N'''+spr.name+ ''', @rolename = N'''+suser_name(srm.role_principal_id)+ ''''
 
from sys.server_principals as spr with (nolock)
       inner join sys.server_permissions as spm (nolock) on spm.grantee_principal_id = spr.principal_id
       left join sys.server_role_members as srm with (nolocK) on spr.name = suser_name(srm.member_principal_id)
where spr.type in ('S','U','K', 'G') --C, R 
       --  and spr.name = 'test_jy'  -- 유저로하나찾기
       and name not like '%$%' and name not in ('sa','NT AUTHORITY\SYSTEM') 
        and spm.permission_name = 'CONNECT SQL' and srm.role_principal_id is not null
order by spr.type,spr.name
  
select   'GRANT ' +spm.permission_name collate Korean_Wansung_Unicode_CS_AS +' TO ['+ spr.name +'];' 
from sys.server_principals as spr with (nolock)
       inner join sys.server_permissions as spm (nolock) on spm.grantee_principal_id = spr.principal_id
       left join sys.server_role_members as srm with (nolocK) on spr.name = suser_name(srm.member_principal_id)
where spr.type in ('S','U','K', 'G') --C, R 
       --    and spr.name = 'test_jy'  -- 유저로하나찾기
       and name not like '%$%' and name not in ('sa','NT AUTHORITY\SYSTEM') and name not like '##%'  
      --and spm.permission_name != 'CONNECT SQL' 
order by spr.type,spr.name  
 
---------------------------------------------------------
---------------------------------------------------------
--2. Linked Server 구성 및 계정 맵핑 (In Secondary)
---------------------------------------------------------
---------------------------------------------------------
 /* 
 주서버에서 아래 링크를 참고하여 수행한 결과 추출 후 보조서버에서 실행
 https://sqlseverjoa.tistory.com/7
 */

 

사전작업 (작업 당일) 

---------------------------------------------------------
---------------------------------------------------------
--0. || Secondary || LogShipping DB Drop  
---------------------------------------------------------
---------------------------------------------------------
use master;
 
restore database [] with recovery;
 
DROP DATABASE [];
 
 
---------------------------------------------------------
---------------------------------------------------------
--1. || Secondary || 당일자 DB Full 백업 norecovery 복원
---------------------------------------------------------
---------------------------------------------------------
 
---------------------------------------------------------
--1.1 Full 백업 파일 이동
---------------------------------------------------------
/* 백업 서버 or Primary --> Secondary 이동 (E:\DBBackup\FULL\) */
 
---------------------------------------------------------
--1.2.Full 백업 norecovery 복원
---------------------------------------------------------
USE [master];
/* 1.1의 Full 백업 명 기입 후 복원 */
RESTORE DATABASE [] FROM DISK = N'E:\DBBackup\FULL\  '
    WITH MOVE '[]' TO ' ',
        MOVE '[]' TO ' ',
        MOVE '[]' TO ' ',
    NORECOVERY, STATS = 10
GO
 
 
---------------------------------------------------------
---------------------------------------------------------
--2. TRN 백업 norecovery 복원
---------------------------------------------------------
---------------------------------------------------------
 
---------------------------------------------------------
--2.1. || Secondary || TRN 백업 파일 이동
---------------------------------------------------------
/* 백업 서버 or Primary --> Secondary  이동 (E:\DBBackup\Log\) */
 
---------------------------------------------------------
--2.2. || Primary || TRN복원쿼리 추출 스크립트 수행
---------------------------------------------------------
/* 첨부파일 'TRN복원추출(In 주서버).txt' 실행 */
 
---------------------------------------------------------
--2.3. || Secondary || TRN 복원 
---------------------------------------------------------
/* 2.2의 추출 결과 수행 */
 
---------------------------------------------------------
--2.4. || Secondary ||  마지막 복원 시점 파악
---------------------------------------------------------
SELECT TOP 1 b.type,a.destination_database_name,b.name,a.restore_date,b.backup_start_date, b.first_lsn, b.last_lsn, b.checkpoint_lsn, b.database_backup_lsn
FROM msdb..restorehistory a
INNER JOIN msdb..backupset b ON a.backup_set_id = b.backup_set_id
WHERE a.destination_database_name = ' '
ORDER BY a.restore_date DESC
 
/* (필요시) 백업파일 헤더정보 확인 */
RESTORE HEADERONLY from disk = 'E:\DBBackup\Log\ '

 

본작업 

---------------------------------------------------------
---------------------------------------------------------
--0. Full/TRN 백업 중지
---------------------------------------------------------
---------------------------------------------------------
 
---------------------------------------------------------
--0.1 || Primary ||
---------------------------------------------------------
/*작업 전 하기 쿼리 수행 결과에서 대상 쿼리만 실행*/
 SELECT 'EXEC msdb.dbo.sp_update_job @job_name='''+name+''''+', @enabled=0' [Script_To_Disable_Job]
FROM msdb.dbo.sysjobs where [enabled]=1 order by name
 
---------------------------------------------------------
--0.2. || Secondary ||
---------------------------------------------------------
/*작업 전 하기 쿼리 수행 결과에서 대상 쿼리만 실행*/
SELECT 'EXEC msdb.dbo.sp_update_job @job_name='''+name+''''+', @enabled=0' [Script_To_Disable_Job]
FROM msdb.dbo.sysjobs where [enabled]=1 order by name
 
 
---------------------------------------------------------
---------------------------------------------------------
--1. 잔여 DB TRN 백업 norecovery 복원
---------------------------------------------------------
---------------------------------------------------------
 
---------------------------------------------------------
--1.0. || Secondary || 복원 필요 시점 파악
---------------------------------------------------------
SELECT TOP 1 b.type,a.destination_database_name,b.name,a.restore_date,b.backup_start_date, b.first_lsn, b.last_lsn, b.checkpoint_lsn, b.database_backup_lsn
FROM msdb..restorehistory a
INNER JOIN msdb..backupset b ON a.backup_set_id = b.backup_set_id
WHERE a.destination_database_name = ' '
ORDER BY a.restore_date DESC
 
/* (필요시) 백업파일 헤더정보 확인 */
RESTORE HEADERONLY from disk = 'E:\DBBackup\Log\ '
 
---------------------------------------------------------
--1.1 TRN 백업 파일 이동
---------------------------------------------------------
/* 1.0 토대로 Primary --> Secondary 이동 (E:\DBBackup\Log\) */
 
---------------------------------------------------------
--1.2. || Primary|| TRN복원쿼리 추출 스크립트 수행
---------------------------------------------------------
/* 첨부파일 'TRN복원추출(In 주서버).txt' 실행 */
 
---------------------------------------------------------
--1.3.|| Secondary ||  TRN 복원 
---------------------------------------------------------
/* 1.2의 추출 결과 수행 */
 
---------------------------------------------------------
--1.4. || Secondary || 복원 완료 체크
---------------------------------------------------------
/* 1.0의 쿼리 동일 반복 */
 
 
---------------------------------------------------------
---------------------------------------------------------
--2. || Primary || 미러링 구성
---------------------------------------------------------
---------------------------------------------------------
/* (partner:APIDB01-M/Witness:DA-DBMON) */
alter database [ ] set partner = 'TCP:// ';
ALTER DATABASE [ ] SET WITNESS = 'TCP:// ';
 
ALTER DATABASE [ ] SET PARTNER TIMEOUT 30;
 
---------------------------------------------------------
---------------------------------------------------------
--3. || Primary ||  미러링 상태 (이중화/오류로그/배치/세션유입 등)확인 
---------------------------------------------------------
---------------------------------------------------------
 
 
SELECT mirroring_connection_timeout,* from sys.database_mirroring;
 
/*하기 URL 참고*/ 
https://sqlseverjoa.tistory.com/manage/posts/
 
---------------------------------------------------------
---------------------------------------------------------
--4. 신규 유지관리 Job 시작
---------------------------------------------------------
--------------------------------------------------------- 
---------------------------------------------------------
--4.1 || Primary ||
---------------------------------------------------------
/*작업 전 하기 쿼리 수행 결과에서 대상 쿼리만 실행*/
 SELECT 'EXEC msdb.dbo.sp_update_job @job_name='''+name+''''+', @enabled=0' [Script_To_Disable_Job]
FROM msdb.dbo.sysjobs where [enabled]=1 order by name
 
---------------------------------------------------------
--4.2. || Secondary ||
---------------------------------------------------------
/*작업 전 하기 쿼리 수행 결과에서 대상 쿼리만 실행*/
SELECT 'EXEC msdb.dbo.sp_update_job @job_name='''+name+''''+', @enabled=0' [Script_To_Disable_Job]
FROM msdb.dbo.sysjobs where [enabled]=1 order by name

첨부파일

TRN복원추출(In 주서버) (2).txt
0.00MB

'SQL Server > 개념&작업 정리' 카테고리의 다른 글

BMT  (0) 2021.09.14
미러링 클라이언트 리커넥션 설정  (0) 2021.06.12
인덱스 구조 분석  (0) 2021.06.04
SQL Server 2008 EOS에 따른 2019 기능 검토  (0) 2021.03.18
[작업] SQL Server EOS 이관 계획  (0) 2021.02.18

+ Recent posts