Red Hat Certified Engineer RHCE · Free Practice Question Hard
Question 2
Your organization relies on a MySQL database for critical applications, and data backup is essential for disaster recovery. You are tasked with automating daily MySQL backups using Ansible, ensuring backups are securely stored and old backups are deleted after 7 days to conserve disk space. How would you achieve this using Ansible?
-
A
- - name: Automate MySQL Backups and Cleanup
- hosts: db_servers
- tasks:
- - name: Create a backup directory
- file:
- path: /backup
- state: directory
- mode: '0755'
- - name: Backup MySQL databases
- shell: >
- mysqldump -u root -pPASSWORD --all-databases >
- /backup/db_backup_$(date +\%F).sql
- args:
- creates: "/backup/db_backup_$(date +'%F').sql"
- - name: Remove backups older than 7 days
- find:
- paths: /backup
- patterns: "*.sql"
- age: 7d
- recurse: yes
- state: absent
-
B
This is a performance based question and not multiple choice. Therefore the answer is in #1. If you require additional explanation, please ask your question in the Q&A section.
Reveal correct answer
Correct answer: A
A.
This Ansible playbook automates daily MySQL database backups and cleanup of old backup files to ensure effective disaster recovery and optimize disk space. It first creates a designated backup directory with the required permissions if it doesn’t already exist. Then, it uses the mysqldump utility to back up all databases, saving the files with a timestamped name. Finally, it removes backup files older than 7 days from the backup directory to prevent storage overload. This automated solution ensures data is consistently backed up and old files are systematically purged, reducing administrative overhead.
Line-by-Line Explanation
- - name: Automate MySQL Backups and Cleanup
Provides a descriptive name for the playbook to indicate its purpose.
- hosts: db_servers
Specifies that the playbook will target the group of hosts labeled db_servers.
- tasks:
Defines the tasks that will be executed in this playbook.
- - name: Create a backup directory
Describes the task to ensure a directory exists for storing backups.
- file:
Uses the file module to manage the state of files or directories.
- path: /backup
Specifies the path for the backup directory.
- state: directory
Ensures that the path is a directory.
- mode: '0755'
Sets the permissions of the directory to allow appropriate access.
- - name: Backup MySQL databases
Describes the task to back up all MySQL databases.
- shell:
Uses the shell module to execute the mysqldump command.
- mysqldump -u root -pPASSWORD --all-databases > /backup/db_backup_$(date +\%F).sql
Runs the mysqldump utility to export all databases to a file named with the current date.
- args:
Defines arguments for the task.
- creates: "/backup/db_backup_$(date +'%F').sql"
Ensures the task only runs if the backup file doesn’t already exist.
- - name: Remove backups older than 7 days
Describes the task to delete old backups from the directory.
- find:
Uses the find module to locate and remove files.
- paths: /backup
Specifies the directory to search for files.
- patterns: "*.sql"
Searches for files matching the .sql pattern (database backups).
- age: 7d
Targets files older than 7 days.
- recurse: yes
Ensures the search includes subdirectories.
- state: absent
Deletes the files that match the criteria.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
