tripleo-ansible/tripleo_ansible/roles/backup_and_restore/tasks/db_backup.yml

185 lines
6.5 KiB
YAML

---
# Copyright 2019 Red Hat, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# Create a backup for each database into separate files.
- name: Get database root password
command: |
hiera -c '{{ tripleo_backup_and_restore_hiera_config_file }}' 'mysql::server::root_password'
when: mysql_password is undefined
register: mysql_password
no_log: "{{ tripleo_backup_and_restore_hide_sensitive_logs | bool }}"
tags:
- bar_create_recover_image
- name: Get galera bind_address
command: |
hiera -c '{{ tripleo_backup_and_restore_hiera_config_file }}' 'tripleo::profile::pacemaker::database::mysql::bind_address'
when: tripleo_backup_and_restore_pacemaker_galera_bind_address is undefined
register: tripleo_backup_and_restore_pacemaker_galera_bind_address
tags:
- bar_create_recover_image
- name: Disable galera when there is no pacemaker mysql bind address
set_fact:
enabled_galera: false
when: tripleo_backup_and_restore_pacemaker_galera_bind_address.stdout == 'nil'
tags:
- bar_create_recover_image
- name: Enable galera when there is pacemaker mysql bind address
set_fact:
enabled_galera: true
when: tripleo_backup_and_restore_pacemaker_galera_bind_address.stdout != 'nil'
tags:
- bar_create_recover_image
- name: Ensure pacemaker is running
command: pcs cluster start --all
when:
- enabled_galera
- tripleo_backup_and_restore_service_manager|bool
- not tripleo_backup_and_restore_enable_snapshots|bool
run_once: true
tags:
- bar_create_recover_image
- name: Ensure the node is back into the cluster
command: pcs cluster start
when:
- enabled_galera
- tripleo_backup_and_restore_enable_snapshots|bool
tags:
- bar_create_recover_image
- name: Wait until pacemaker has Galera up&running
shell: |
set -o pipefail
ss -tunlp | grep ":3306 " | sed -e 's/.*\///'
register: mysql_result
retries: 300
until: mysql_result is search('mysqld')
delay: 5
when:
- enabled_galera
tags:
- bar_create_recover_image
- name: Get the mysql container id when galera is enabled
shell: |
set -o pipefail
{{ tripleo_container_cli }} ps -a | grep galera | awk '{print $1}'
when: enabled_galera
register: galera_container_id
tags:
- bar_create_recover_image
- name: Set the tripleo_backup_and_restore_mysql_container id
set_fact:
tripleo_backup_and_restore_mysql_container: "{{ galera_container_id.stdout }}"
when: enabled_galera
tags:
- bar_create_recover_image
- name: Ensure mysql container is running
command: "{{ tripleo_container_cli }} unpause {{ tripleo_backup_and_restore_mysql_container }}"
when:
- mysql_password.stderr is defined
- tripleo_backup_and_restore_mysql_container == "mysql"
- not enabled_galera
- tripleo_backup_and_restore_service_manager|bool
failed_when: false
tags:
- bar_create_recover_image
- name: Galera desync the MySQL node
shell: |
set -o pipefail
{{ tripleo_container_cli }} exec {{ tripleo_backup_and_restore_mysql_container }} bash -c "mysql -p -u root \
-p{{ mysql_password.stdout }} --execute 'SET GLOBAL wsrep_desync = ON'"
register: desync_output
retries: 300
delay: 5
until: not desync_output.stderr
when:
- mysql_password.stderr is defined
- enabled_galera
- tripleo_backup_and_restore_enable_snapshots|bool or not tripleo_backup_and_restore_service_manager|bool
tags:
- bar_create_recover_image
no_log: "{{ tripleo_backup_and_restore_hide_sensitive_logs | bool }}"
- name: MySQL Grants backup
shell: |
set -o pipefail
{{ tripleo_container_cli }} exec {{ tripleo_backup_and_restore_mysql_container }} bash -c "mysql -uroot \
-p{{ mysql_password.stdout }} -s -N \
-e \"SELECT CONCAT('\\\"SHOW GRANTS FOR ''',user,'''@''',host,''';\\\"') \
FROM mysql.user where (length(user) > 0 and \
(Host = 'localhost' or Host='%' or IS_IPV4(Host) or IS_IPV6(Host)))\" | xargs -n1 mysql \
-uroot -p{{ mysql_password.stdout }} -s -N -e | sed 's/$/;/' " > {{ tripleo_backup_and_restore_mysql_grants_file }}
when: mysql_password.stderr is defined
tags:
- bar_create_recover_image
no_log: "{{ tripleo_backup_and_restore_hide_sensitive_logs | bool }}"
- name: MySQL BBDDs backup
shell: |
set -o pipefail
{{ tripleo_container_cli }} exec {{ tripleo_backup_and_restore_mysql_container }} bash -c "mysql -uroot \
-p{{ mysql_password.stdout }} -s -N \
-e \"select distinct table_schema from information_schema.tables \
where engine='innodb' and table_schema != 'mysql';\" | xargs mysqldump -uroot \
-p{{ mysql_password.stdout }} --single-transaction --databases" > {{ tripleo_backup_and_restore_mysql_backup_file }}
when: mysql_password.stderr is defined
tags:
- bar_create_recover_image
no_log: "{{ tripleo_backup_and_restore_hide_sensitive_logs | bool }}"
- name: Galera sync the MySQL node
shell: |
set -o pipefail
{{ tripleo_container_cli }} exec {{ tripleo_backup_and_restore_mysql_container }} bash -c "mysql -p -u root \
-p{{ mysql_password.stdout }} --execute 'SET GLOBAL wsrep_desync = OFF'"
when:
- mysql_password.stderr is defined
- enabled_galera
- tripleo_backup_and_restore_enable_snapshots|bool or not tripleo_backup_and_restore_service_manager|bool
tags:
- bar_create_recover_image
no_log: "{{ tripleo_backup_and_restore_hide_sensitive_logs | bool }}"
- name: Pause mysql.
command: "{{ tripleo_container_cli }} pause {{ tripleo_backup_and_restore_mysql_container }}"
when:
- mysql_password.stderr is defined
- tripleo_backup_and_restore_mysql_container == "mysql"
- not enabled_galera
- tripleo_backup_and_restore_service_manager|bool
- not tripleo_backup_and_restore_enable_snapshots|bool
failed_when: false
tags:
- bar_create_recover_image
- name: Stop pacemaker
command: pcs cluster stop --all
when:
- enabled_galera
- tripleo_backup_and_restore_service_manager|bool
- not tripleo_backup_and_restore_enable_snapshots|bool
run_once: true
tags:
- bar_create_recover_image