---

- name: "Backup storage server | create system user"
  become: yes
  ansible.builtin.user:
    name: '{{ system_user }}'
    comment: "user for backup"
    shell: /bin/bash
  register: create_user

- name: "Create .ssh dir and backups dir"
  become: yes
  file:
    path: '/home/{{ system_user }}/{{ item.name }}/'
    mode: '{{ item.mode }}'
    owner: '{{ system_user }}'
    group: '{{ system_user }}'
    state: directory
  loop:
    - name: '.ssh'
      mode: '0700'
    - name: 'backups'
      mode: '0775'

- name: "Create/Resize LVM for datadir"
  include_role:
    name: lvm_with_hetzner_volumes
  vars:
    lvm_with_hetzner_volumes__volprefix: backup_datadir
    lvm_with_hetzner_volumes__volsize: "{{ backup_lvm_hcloudvol_size }}"
    lvm_with_hetzner_volumes__volcount: "{{ backup_lvm_hcloudvol_count }}"
    lvm_with_hetzner_volumes__mountpath: "{{ backup_lvm_hcloudvol_mountpath }}"

- name: "Providing SSH priv.key"
  no_log: true
  become: yes
  copy:
    dest: '/home/{{ system_user }}/.ssh/id_rsa'
    mode: '0400'
    owner: '{{ system_user }}'
    group: '{{ system_user }}'
    content: '{{ backup_user_ssh_privkey_vault }}'

- name: "Providing rsync script"
  become: yes
  copy:
    src: '{{ item }}'
    dest: '/home/{{ system_user }}/{{ item }}'
    mode: '0755'
    owner: '{{ system_user }}'
    group: '{{ system_user }}'
  with_items:
    - pull_remote_backups.sh
    - push_backups_to_restore_server.sh

- name: Touch metrics.prom if not exists
  file:
    path: "/home/{{ system_user }}/metrics.prom"
    state: touch
    mode: '0744'
    owner: '{{ system_user }}'
    group: '{{ system_user }}'

- name: Touch backup_status_maria.prom if not exists
  file:
    path: "/home/{{ system_user }}/backup_status_maria.prom"
    state: touch
    mode: '0744'
    owner: '{{ system_user }}'
    group: '{{ system_user }}'

- name: Touch backup_status_postgres.prom if not exists
  file:
    path: "/home/{{ system_user }}/backup_status_postgres.prom"
    state: touch
    mode: '0744'
    owner: '{{ system_user }}'
    group: '{{ system_user }}'

- name: Create symbolic link for node_exporter text metrics
  file:
    src: "/home/{{ system_user }}/metrics.prom"
    dest: "/var/lib/prometheus/node-exporter/offsite-metrics.prom"
    state: link

- name: Create symbolic link for node_exporter text metrics backup_status_maria
  file:
    src: "/home/{{ system_user }}/backup_status_maria.prom"
    dest: "/var/lib/prometheus/node-exporter/backup_status_maria.prom"
    state: link

- name: Create symbolic link for node_exporter text metrics backup_status_postgres
  file:
    src: "/home/{{ system_user }}/backup_status_postgres.prom"
    dest: "/var/lib/prometheus/node-exporter/backup_status_postgres.prom"
    state: link
