# backup_playbook.yml --- - name: Load vars hosts: all tags: always tasks: - include_vars: file: prod-vars.yml - name: Backup Etherpad hosts: pad remote_user: "{{ emacsconf_user }}" become: false vars_files: - roles/pad/vars/main.yml tasks: - name: "Ensure the backup directory exists on the remote server" ansible.builtin.file: path: "{{ backup_dir_on_server }}" state: directory mode: '0755' owner: "{{ emacsconf_user }}" group: "{{ emacsconf_group }}" - name: "Backup MySQL database" community.mysql.mysql_db: name: "{{ etherpad_mysql_database_name }}" state: dump target: "{{ backup_dir_on_server }}{{ etherpad_mysql_database_name }}.sql" login_user: "{{ etherpad_mysql_database_user }}" login_password: "{{ etherpad_mysql_database_password }}" register: db_backup_result - name: "Change ownership of the backup file" ansible.builtin.file: path: "{{ backup_dir_on_server }}{{ etherpad_mysql_database_name }}.sql" owner: "{{ emacsconf_user }}" group: "{{ emacsconf_group }}" mode: '0644' become: true - name: "Log the backup status" ansible.builtin.debug: msg: "Database backup completed successfully. File is at {{ backup_dir_on_server }}{{ etherpad_mysql_database_name }}.sql" - name: "Retrieve backup from prod and save locally" hosts: localhost # This part runs on your local machine connection: local tasks: - name: "Ensure the local backup directory exists" ansible.builtin.file: path: "{{ local_backup_dir }}" state: directory mode: '0755' - name: "Fetch the database backup file" ansible.posix.synchronize: src: "{{ hostvars['pad'].ansible_ssh_user }}@{{ hostvars['pad'].ansible_host }}:{{ backup_dir_on_server }}{{ etherpad_mysql_database_name }}.sql" dest: "{{ local_backup_dir }}" mode: pull archive: yes compress: yes rsync_path: "rsync"