Skip to content
This repository was archived by the owner on Oct 8, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/roles/database_backup/database_backup-mysql.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ mysql_backup:
- database: "{{ (project_name + '_' + build_type) | regex_replace('-', '_') }}" # avoid hyphens in MySQL database names
user: "{{ (project_name + '_' + build_type) | truncate(32, true, '', 0) }}" # 32 char limit
credentials_file: "/home/{{ deploy_user }}/.mysql.creds"
#handling: static # optional override to the main handling method on a per database basis

```

Expand Down
1 change: 1 addition & 0 deletions roles/database_backup/database_backup-mysql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ mysql_backup:
- database: "{{ (project_name + '_' + build_type) | regex_replace('-', '_') }}" # avoid hyphens in MySQL database names
user: "{{ (project_name + '_' + build_type) | truncate(32, true, '', 0) }}" # 32 char limit
credentials_file: "/home/{{ deploy_user }}/.mysql.creds"
#handling: static # optional override to the main handling method on a per database basis

```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ mysql_backup:
- database: "{{ (project_name + '_' + build_type) | regex_replace('-', '_') }}" # avoid hyphens in MySQL database names
user: "{{ (project_name + '_' + build_type) | truncate(32, true, '', 0) }}" # 32 char limit
credentials_file: "/home/{{ deploy_user }}/.mysql.creds"
#handling: static # optional override to the main handling method on a per database basis
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,23 @@
- ansible.builtin.set_fact:
_mysql_previous_build_database_name: "{{ database.database }}"

- name: Check if the database exists already.
ansible.builtin.shell: "set -o pipefail && mysql --defaults-extra-file={{ database.credentials_file }} -e 'SHOW DATABASES;' | grep -w {{ _mysql_build_database_name }}"
register: _build_db_status
failed_when: _build_db_status.rc > 1 # only exit on abnormal errors
run_once: true
args:
executable: /bin/bash

# Create database if this is an initial build or it doesn't exist yet
- name: Create initial database.
community.mysql.mysql_db:
name: "{{ _mysql_build_database_name }}"
state: present
login_host: "{{ _mysql_host }}"
login_user: "{{ _mysql_user }}"
login_password: "{{ _mysql_password }}"
when: previous_build_number == 0
when: previous_build_number == 0 or _build_db_status.rc == 1
run_once: true

- name: Ensure the dump directory exists.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@
- ansible.builtin.set_fact:
_mysql_previous_build_database_name: "{{ database.database }}"

- name: Check if the database exists already.
ansible.builtin.shell: "set -o pipefail && mysql --defaults-extra-file={{ database.credentials_file }} -e 'SHOW DATABASES;' | grep -w {{ _mysql_build_database_name }}"
register: _build_db_status
failed_when: _build_db_status.rc > 1 # only exit on abnormal errors
run_once: true
args:
executable: /bin/bash

# Create database if this is an initial build or it doesn't exist yet
- name: Create initial database.
community.mysql.mysql_db:
name: "{{ _mysql_build_database_name }}"
state: present
login_host: "{{ _mysql_host }}"
login_user: "{{ _mysql_user }}"
login_password: "{{ _mysql_password }}"
when: previous_build_number == 0
when: previous_build_number == 0 or _build_db_status.rc == 1
run_once: true
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,46 @@
_mysql_build_database_name: "{{ database.database }}_{{ build_number }}"
- ansible.builtin.set_fact:
_mysql_previous_build_database_name: "{{ database.database }}_{{ previous_build_number }}"
# Note: we don't use the mysql_db Ansible module on purpose.
# If database already exists, we want to fail and not override it
# with previous build.
# @TODO fix this so we check if the database exists and exit with
# the proper plugin instead of using command.
- name: Create new database.
ansible.builtin.command: mysql --defaults-extra-file={{ database.credentials_file }} -e "CREATE DATABASE `{{ _mysql_build_database_name }}`;"

- name: Check if the new database exists already.
ansible.builtin.shell: "set -o pipefail && mysql --defaults-extra-file={{ database.credentials_file }} -e 'SHOW DATABASES;' | grep -w {{ _mysql_build_database_name }}"
register: _build_db_status
failed_when: _build_db_status.rc == 0 # we want the build to fail if the database exists
run_once: true
args:
executable: /bin/bash

- name: Create a new database.
community.mysql.mysql_db:
name: "{{ _mysql_build_database_name }}"
state: present
config_file: "{{ database.credentials_file }}"
config_overrides_defaults: true
run_once: true

#- name: Create a new database.
# community.mysql.mysql_db:
# name: "{{ _mysql_build_database_name }}"
# state: present
# config_file: "{{ database.credentials_file }}"
# config_overrides_defaults: true
# run_once: true
- name: Check if the previous database exists.
ansible.builtin.shell: "set -o pipefail && mysql --defaults-extra-file={{ database.credentials_file }} -e 'SHOW DATABASES;' | grep -w {{ _mysql_previous_build_database_name }}"
register: _previous_db_status
failed_when: _previous_db_status.rc > 1 # only fail if we get an unexpected exitcode
run_once: true
args:
executable: /bin/bash

# Importing with shell for speed, we cannot import with mysql_db unless we create a dump file first
- name: Populate new database.
ansible.builtin.shell: "set -o pipefail && mysqldump --defaults-extra-file={{ database.credentials_file }} {{ mysql_backup.mysqldump_params }} {{ _mysql_previous_build_database_name }} | mysql --defaults-extra-file={{ database.credentials_file }} {{ _mysql_build_database_name }}"
args:
executable: /bin/bash
when: previous_build_number > 0
when:
- previous_build_number > 0
- _previous_db_status.rc == 0 # only run if we have a database
run_once: true

# Making it clear if we skipped the population of the new database
- name: Populating database skipped.
ansible.builtin.debug:
msg: "### Attention - the new database was NOT populated!"
when:
- previous_build_number > 0
- _previous_db_status.rc != 0
run_once: true
13 changes: 12 additions & 1 deletion roles/database_backup/database_backup-mysql/tasks/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,18 @@
_mysql_build_password: "{{ lookup('password', '/dev/shm/{{ project_name }}_{{ build_type }}_{{ build_number }}') }}"
when: mysql_backup.credentials_handling == 'rotate'

- ansible.builtin.include_tasks: "deploy-{{ mysql_backup.handling }}.yml"
- name: "Set database handling type to {{ mysql_backup.handling }}."
ansible.builtin.set_fact:
_mysql_handling: "{{ mysql_backup.handling }}"
# If we have a specific instruction for handling this database differently, use it.
- name: Override database handling type for this database, if specified.
ansible.builtin.set_fact:
_mysql_handling: "{{ database.handling }}"
when:
- database.handling is defined
- database.handling | length > 0
- name: Execute backup tasks.
ansible.builtin.include_tasks: "deploy-{{ _mysql_handling }}.yml"

# We append privileges instead of replacing,
# to allow this role to be looped over,
Expand Down