---
- name: install borg and tools
  pacman: name=borg state=present

- name: check if borg repository already exists
  command: borg list {{ item['host'] }}/{{ item['dir'] }}
  environment:
    BORG_RELOCATED_REPO_ACCESS_IS_OK: "yes"
  register: borg_list
  ignore_errors: true
  loop: "{{ backup_hosts }}"
  changed_when: borg_list.stdout | length > 0

- name: init borg repository
  command: borg init -e keyfile {{ item['host'] }}/{{ item['dir'] }}
  when: borg_list is failed
  environment:
    BORG_PASSPHRASE: ""
  ignore_errors: true  # This can sometimes fail if a backup is in progress :/
  loop: "{{ backup_hosts }}"

- name: install convenience scripts
  template: src=borg.j2 dest=/usr/local/bin/borg{{ item['suffix'] }} owner=root group=root mode=0755
  loop: "{{ backup_hosts }}"

- name: install borg backup scripts
  template: src=borg-backup.sh.j2 dest=/usr/local/bin/borg-backup{{ item['suffix'] }}.sh owner=root group=root mode=0755
  loop: "{{ backup_hosts }}"

- name: install postgres backup script
  template: src=backup-postgres.sh.j2 dest=/usr/local/bin/backup-postgres.sh owner=root group=root mode=0755
  when: postgres_backup_dir is defined

- name: check whether postgres user exists
  command: getent passwd postgres
  register: check_postgres_user
  ignore_errors: true
  changed_when: check_postgres_user.stdout | length > 0

- name: make postgres backup directory
  file: path={{ postgres_backup_dir }} owner=root group=root mode=0755 state=directory
  when: check_postgres_user is succeeded and postgres_backup_dir is defined

- name: install mysql backup script
  template: src=backup-mysql.sh.j2 dest=/usr/local/bin/backup-mysql.sh owner=root group=root mode=0755
  when: mysql_backup_dir is defined

- name: install mysql backup config
  template: src=backup-my.cnf.j2 dest={{ mysql_backup_defaults }} owner=root group=root mode=0644
  when: mysql_backup_defaults is defined

- name: create mysql backup directory
  file: path={{ mysql_backup_dir }} state=directory owner=root group=root mode=0755
  when: mysql_backup_dir is defined

- name: install gitlab backup script
  template: src=backup-gitlab.sh.j2 dest=/usr/local/bin/backup-gitlab.sh owner=root group=root mode=0755
  when: inventory_hostname == "gitlab.archlinux.org"

- name: install systemd services for backup
  template: src={{ item }}.j2 dest=/etc/systemd/system/{{ item }} owner=root group=root mode=0644
  with_items:
    - borg-backup.service
    - borg-backup-offsite.service

- name: install systemd timer for backup
  copy: src=borg-backup.timer dest=/etc/systemd/system/borg-backup.timer owner=root group=root mode=0644

- name: activate systemd timer for backup
  systemd: name=borg-backup.timer enabled=yes state=started daemon-reload=yes
