Devops/Ansible

병렬 백업과 복원을 위한 async 작업

BabyTT 2022. 8. 30. 20:56

Ansible을 통한 병렬 백업/복원 작업 순서

1. 병렬 작업이 될 수 있도록 백업, 복원 대상이 되는 DB 카탈로그 명을 변수로 작업하는 SP 생성
2. 1번 sp를 배포하고 필요한 TargetDB를 변수로 async 모드 실행 (async: 780/ poll: 0)으로 설정
- async 780은 DB 백업에 필요한 총 시간 기준 산정
- poll 0은 0으로 설정할 경우 with_items에 있는 TargetDB 숫자까지 win_shell 작업을 바로 또 실행하여 병렬 처리처럼 작동하게 된다. (약 1~2초 간의 작업 지연 발생 가능)

- name: "Execute restore_parallel SP"
  win_shell: "invoke-sqlcmd -Querytimeout 0 -Serverinstance \"{{ sql_localhostname }}.도메인,port정보\" -Username \"{{ sql_user }}\" -Password \"{{ sqluser_pwd }}\"  -Database \"master\"  -Query \"EXEC usp_restore_parallel '{{ item.TargetDB }}'\""
  with_items: "{{ TargetDB }}"
  async: 780   # wait for up to 780 sec, poll every 0 sec means polling immediately
  poll : 0 # When you set poll: 0, Ansible starts the task and immediately moves on to the next task without waiting for a result
  register: restore_result
  
- name: "Check on an async restore task"
  async_status:
    jid: "{{ item.ansible_job_id }}"
  register: job_result
  until: job_result.finished
  retries: 13  # retry 13, wait 60 sec then retry
  delay: 60  # wait 60 sec
  with_items: "{{ restore_result.results }}"

- name: "Clean async job cache"
  async_status:
    jid: "{{ item.ansible_job_id }}"
    mode: cleanup
  loop: "{{ job_result.results }}"