---

- name: Create the archive dir
  file:
    path: "{{ n8n_root_dir }}/archives/{{ n8n_current_version }}"
    state: directory
  tags: n8n

- name: Archive previous version
  synchronize:
    src: "{{ n8n_root_dir }}/{{ item }}"
    dest: "{{ n8n_root_dir }}/archives/{{ n8n_current_version }}"
    recursive: True
    delete: True
    compress: False
  loop:
    - app
    - etc
    - data
  delegate_to: "{{ inventory_hostname }}"
  tags: n8n

- name: Backup the database
  command: >
    /usr/pgsql-15/bin/pg_dump
    --format=custom
    --clean
    --create
    --host={{ n8n_db_server }}
    --port={{ n8n_db_port }}
    --username={{ n8n_db_user }}
    {{ n8n_db_name }}
    --file={{ n8n_root_dir }}/archives/{{ n8n_current_version }}/{{ n8n_db_name }}.sqlc
  environment:
    - PGPASSWORD: "{{ n8n_db_pass }}"
  tags: n8n
