Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/daljitdokal/ansible-mysql-backup

Ansible playbook to create MySQL database backup from remote server.
https://github.com/daljitdokal/ansible-mysql-backup

ansible-playbook automation backup mysql ssh

Last synced: 20 days ago
JSON representation

Ansible playbook to create MySQL database backup from remote server.

Awesome Lists containing this project

README

        

# Backup MySql databases from remote server

** Description
I have wordpress websites running on VPS server and I need a solution to backup these databases regulerly.
We will create an [[https://www.ansible.com][ansible]] playbook (step by stepp process) to automate the solution.

** Prerequisites
- [[https://docs.oracle.com/en/cloud/cloud-at-customer/occ-get-started/generate-ssh-key-pair.html][ssh key]] (make sure public key has been uploaded to remote server)
- [[https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html][ansible]]
- A running instance of MySQL

** Step 1: Create inventory file
Create new file as 'server.inventory.ini'
#+BEGIN_SRC
touch server.inventory.ini
#+END_SRC

Add the following details
#+BEGIN_SRC
[Server-VPS]
# server address here

[Server-VPS:vars]
ansible_connection=ssh
ansible_user= # remote server username
#+END_SRC

** Step 2: Create vault file
*** Step 2.1: Create file with secrets
Create new file as 'database_vault.yaml'
#+BEGIN_SRC
touch database_vault.yaml
#+END_SRC

Add the following details
#+BEGIN_SRC
db_name: # database name here
db_user_name: # database username here
db_user_password: # database user password here
#+END_SRC

*** Step 2.2: Encrypt secret file
Please run the following command to [[https://www.digitalocean.com/community/tutorials/how-to-use-vault-to-protect-sensitive-ansible-data-on-ubuntu-16-04#:~:text=To%20create%20a%20new%20file,encrypted%20YAML%20file%20called%20vault.][encrypt]] newly created secrets file.
#+BEGIN_SRC
ansible-vault encrypt database_vault.yaml
#+END_SRC

#+BEGIN_SRC
# output
New Vault password:
Confirm New Vault password:
Encryption successful
#+END_SRC

*** Step 3: Create playbook
Create new file as 'database_backup_playbook.yaml'
#+BEGIN_SRC
touch database_backup_playbook.yaml
#+END_SRC

Add the following details
#+BEGIN_SRC yaml :tangle database_backup_playbook.yaml
---
- hosts: Server-VPS
gather_facts: true
vars_files:
- database_vault.yaml
tasks:
- name: Create variables
set_fact:
db_file_name: "{{ db_name }}_{{ ansible_date_time.date | replace('-','') }}.sql"

- name: Confirm hostname
debug:
msg: Logged into the server.

- name: Download Database to server
shell: |
mysqldump -u {{ db_user_name}} -p"{{ db_user_password }}" {{ db_name }} --quick --lock-tables=false > "{{ db_file_name }}" --no-tablespaces
no_log: true

- name: Wait until the database backup completed on server
wait_for:
path: "{{ db_file_name }}"
state: present
msg: "Timeout to find file {{ db_file_name }}"

- name: Downloading backup to local computer
ansible.builtin.fetch:
src: "{{ db_file_name }}"
dest: "{{ db_file_name }}"
flat: yes

- name: Download completed
debug:
msg: Database have been downloaded successfully.
#+END_SRC

*** Step 4: Time to playbook to run manually in command-line:

*** Step 4.1 - Run ssh-agent
If you have the passpharace added to ssh key then it will be easier to run it manually.
#+begin_src
eval $(ssh-agent)
ssh-add ./path/to/ssh/key
#+end_src

*** Step 4.2 - Execute playbook
Please execute following command to run the playbopk and start the process.
#+BEGIN_SRC bash
ansible-playbook -i server.inventory.ini database_backup_playbook.yaml --ask-vault-password
#+END_SRC

*** Step 4.3: Enter vault password.
Please enter the vault password so that ansible can decyrpt the `database_vault.yaml` variables to use in the playbook.

* Install `AWX` on `k3s`cluster

- Please use the following [[https://github.com/daljitdokal/install-awx-with-k3s-cluster][link]] to view step by step docmentation to install `awx` on `k3s`.
- Please use the following [[https://github.com/daljitdokal/raspberry-pi-ubuntu-server-k3s-awx-ansible-automated-setup][link]] to view Step by step process to build ubuntu-server on raspberry-pi with k3s, kubernetes-dashboard, awx and awx job template with ansible paybooks (mysql datbase backup from remote server).