Ctrl K
ComunidadGuías…

ansible/download_db.yml

Rama main · réplica de lectura del repositorio original.

Archivo original ↗Preguntar a KiBot
---
# myNextScreen — Download Database Backup
# Usage: ansible-playbook -i hosts.ini download_db.yml -K
#
# Dumps the PostgreSQL database from the server (pg_dump custom format) and
# fetches it to a timestamped local backup.
# Output: ansible/backups/mynextscreen_YYYYMMDD_HHMMSS.dump

- name: Download database from server
  hosts: servers
  become: true
  vars:
    service_user: mynextscreen
    service_user_home: "/home/{{ service_user }}"
    backup_dir: "{{ playbook_dir }}/backups"
    remote_dump: "/tmp/mynextscreen_download.dump"

  tasks:
    - name: Verify PostgreSQL container is running
      ansible.builtin.command: podman container exists mynextscreen-postgres
      register: pg_running
      changed_when: false
      failed_when: false

    - name: Fail if PostgreSQL container is not running
      ansible.builtin.fail:
        msg: "mynextscreen-postgres container is not running on the server"
      when: pg_running.rc != 0

    - name: Dump database (pg_dump custom format)
      ansible.builtin.shell: >-
        podman exec mynextscreen-postgres pg_dump -U mynextscreen -d mynextscreen -Fc > "{{ remote_dump }}"
      args:
        executable: /bin/bash
      changed_when: true

    - name: Generate timestamp
      ansible.builtin.set_fact:
        backup_timestamp: "{{ ansible_date_time.date | replace('-', '') }}_{{ ansible_date_time.time | replace(':', '') }}"

    - name: Create local backup directory
      ansible.builtin.file:
        path: "{{ backup_dir }}"
        state: directory
        mode: "0755"
      delegate_to: localhost
      become: false

    - name: Fetch dump to local backup
      ansible.builtin.fetch:
        src: "{{ remote_dump }}"
        dest: "{{ backup_dir }}/mynextscreen_{{ backup_timestamp }}.dump"
        flat: true

    - name: Remove remote temp dump
      ansible.builtin.file:
        path: "{{ remote_dump }}"
        state: absent

    - name: Report backup location
      ansible.builtin.debug:
        msg: "Database saved to {{ backup_dir }}/mynextscreen_{{ backup_timestamp }}.dump"