Skip to content

Commit

Permalink
change backup code
Browse files Browse the repository at this point in the history
  • Loading branch information
rezabojnordi committed Dec 25, 2021
2 parents e251852 + 96fc0e9 commit 0099e57
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 5 deletions.
5 changes: 5 additions & 0 deletions db/inventory
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@

[BR]
#galera_container1
<<<<<<< HEAD
#galera_container2
#galera3.iranserver.com
infra3-galera-container-7f8a2401
=======
galera_container2
#galera_container3
>>>>>>> 96fc0e97430f870938b2e7281f2c55b648ca55b8

[destroy]
infra1
Expand Down
4 changes: 2 additions & 2 deletions db/roles/backup_restore/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ setup_hosts: "openstack-ansible /opt/openstack-ansible/playbooks/setup-hosts.yml

setup_infrastructure: "openstack-ansible /opt/openstack-ansible/playbooks/setup-infrastructure.yml"

infra3_galera: "172.20.9.189"
infra3_galera: "infra3-galera-container-7f8a2401"

rsa_pub: "~/.ssh/id_rsa.pub"

copy_to_nfs: "~/.ssh/authorized_keys"

nfs_ip: "172.20.8.7"
nfs_ip: "nfs"

22 changes: 22 additions & 0 deletions db/roles/backup_restore/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# checkBackup.yml
# check database backup

Using this playbook, the openstack database backup validation process is performed.

This process is performed in three steps:

## First Step
Check the backup file size. If the new backup file size is less than the old backup file size, the PlayBook will be fail.

## Second Step
Restore backup and run two queries for testing.

## Third Step
Delete the backup file


## variables

* pass1: mysql root password
* backup_dir: path of backup file

22 changes: 22 additions & 0 deletions db/roles/backup_restore/tasks/backup.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<<<<<<< HEAD
# vim:ft=ansible:


Expand Down Expand Up @@ -48,17 +49,38 @@
- name: backup galera cluster
mysql_db:
login_unix_socket: /var/run/mysqld/mysqld.sock
=======
---


- name: backup galera cluster
mysql_db:
>>>>>>> 96fc0e97430f870938b2e7281f2c55b648ca55b8
state: dump
name: all
target: "{{ path_backup }}all_{{ ansible_date_time.date }}.sql"
tags:
- backup


<<<<<<< HEAD
- name: scp backup to nfs server
shell: "scp {{ path_backup }}all_{{ ansible_date_time.date }}.sql root@172.20.4.7:/storage/glance/backup_mysql_beh/"
tags:
- backup
=======

- name: scp backup to nfs server
shell: "scp {{ path_backup }}all_{{ ansible_date_time.date }}.sql root@nfs:/storage/glance/backup_mysql/"
tags:
- backup

- name: scp backup to checkDB server
shell: "scp {{ path_backup }}all_{{ ansible_date_time.date }}.sql root@checkDB:/home/mua/db_file"
tags:
- backup
- checkDB
>>>>>>> 96fc0e97430f870938b2e7281f2c55b648ca55b8

- name: restart mariadb
service: name=mysql enabled=yes state=restarted
Expand Down
110 changes: 110 additions & 0 deletions db/roles/backup_restore/tasks/checkBackup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
---
- hosts: DR
become_user: ansible_user
become: true
vars:
- pass1: "password"
- backup_dir: "/home/ansible_user/db_file/"


tasks:
- set_fact:
mysql_root_password: '{{ pass1 }}'

- debug:
msg: "root pass: {{ mysql_root_password }}"

# First Step: Check Backup Size

- name: get backup file name
find:
paths: "{{ backup_dir }}"
patterns: "all_{{ ansible_date_time.date }}.sql"
register: files_matched

- set_fact:
file_name: "{{ files_matched.files[0].path }}"
- debug:
msg: "{{ file_name }}"

- name: Read size of old_backup
slurp:
src: "/home/ansible_user/filesize.txt"
register: slurpfile
- set_fact:
old_size: "{{ slurpfile['content'] | b64decode | int}}"

- debug:
msg: "old_size: {{ old_size }}"

- name: compare with old_backup size
shell: echo "Warning! New backup size is less than old backup"
when: old_size > new_size
failed_when: old_size > new_size

- name: Save size of new backup
shell: echo {{ new_size }} > "/home/ansible_user/filesize.txt"


# Second Step: Backup Restore and run test query

- name: Remove database
mysql_db:
name: "{{ item }}"
state: absent
login_user: root
login_password: "{{ mysql_root_password }}"
loop: ['cinder', 'glance', 'heat', 'keystone', 'neutron', 'nova', 'nova_api', 'nova_cell0', 'rally']

- name: get database size
stat:
path: "{{ file_name }}"
register: file_size
- set_fact:
new_size: "{{ ( file_size.stat.size / 1024 | pow(2) ) | round | int }}"

- name: restore database
mysql_db:
name: all
state: import
target: "{{ file_name }}"
login_user: root
login_password: "{{ mysql_root_password }}"



- name: run query on glance database
community.mysql.mysql_query:
login_db: glance
query: SELECT * FROM image_locations
login_user: root
login_password: "{{ mysql_root_password }}"
register: res
- name: show query result
debug: var=res.query_result



- name: run query on nova database
community.mysql.mysql_query:
login_db: nova
query: SELECT * FROM compute_nodes
login_user: root
login_password: "{{ mysql_root_password }}"
register: res
- name: show query result
debug: var=res.query_result

# Third Step: Remove backup file

- name: get list of files
find:
paths: "{{ backup_dir }}"
patterns: "*"
register: files_to_delete

- name: Remove old files backup
file:
path: "{{ item.path }}"
state: absent
with_items: "{{ files_to_delete.files }}"
6 changes: 3 additions & 3 deletions db/roles/backup_restore/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
# tasks file for mysql_backup
- include: backup.yml
#- include: backup_one_by_one.yml
#- include: restore.yml
#- include: restore_one_by_one.yml
- include: backup_one_by_one.yml
- include: restore.yml
- include: restore_one_by_one.yml
22 changes: 22 additions & 0 deletions db/roles/backup_restore/tasks/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
### checkBackup.yml
# check database backup

Using this playbook, the openstack database backup validation process is performed.

This process is performed in three steps:

## First Step
Check the backup file size. If the new backup file size is less than the old backup file size, the PlayBook will be fail.

## Second Step
Restore backup and run two queries for testing.

## Third Step
Delete the backup file


## variables

* pass1: mysql root password
* backup_dir: path of backup file

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- /etc/haproxy
- /etc/lxc
- /etc/neutron
- /openstack
dest: "/Backup/{{inventory_hostname}}.tgz"
format: gz

Expand Down

0 comments on commit 0099e57

Please sign in to comment.