---

- name: Create archive dir
  file: path={{ squashtm_root_dir }}/archives/{{ squashtm_current_version }} state=directory
  tags: squashtm

- name: Stop service during upgrade
  service: name={{ item }} state=stopped
  loop:
    - squash-tm
  tags: squashtm

- name: Archive current version
  synchronize:
    src: "{{ squashtm_root_dir }}/{{ item }}"
    dest: "{{ squashtm_root_dir }}/archives/{{ squashtm_current_version }}/"
    delete: True
    compress: False
  delegate_to: "{{ inventory_hostname }}"
  loop:
    - app
  tags: squashtm

- when: squashtm_db_engine == 'mysql'
  block:
    - name: Install mysqldump
      package: name=mariadb

    - name: Dump the database
      mysql_db:
        state: dump
        name: "{{ squashtm_db_name }}"
        target: "{{ squashtm_root_dir }}/archives/{{ squashtm_current_version }}/{{ squashtm_db_name }}.sql"
        login_host: "{{ squashtm_db_server }}"
        login_port: "{{ squashtm_db_port }}"
        login_user: "{{ squashtm_db_user }}"
        login_password: "{{ squashtm_db_pass }}"
        quick: True
        single_transaction: True

  tags: squashtm

- when: squashtm_db_engine == 'postgres'
  block:
    - name: Install pg_dump
      package: name=postgresql14

    - name: Dump the database
      command: >
        /usr/pgsql-14/bin/pg_dump
          --clean
          --create
          --host={{ squashtm_db_server | quote }}
          --port={{ squashtm_db_port | quote }}
          --username={{ squashtm_db_user | quote }} {{ squashtm_db_name | quote }}
          --file="{{ squashtm_root_dir }}/archives/{{ squashtm_current_version }}/{{ squashtm_db_name }}.sql"
      environment:
        - PGPASSWORD: "{{ squashtm_db_pass }}"
  tags: squashtm

