---
# Backup
- name: Ensure snapshot directory
  file:
    state: directory
    path: "{{ snapshots.path }}"
  become: yes
- name: Ensure /usr/local/scripts exists
  file:
    state: directory
    path: '/usr/local/scripts'
  become: yes
- name: Template btrfs_backup.sh
  ansible.builtin.template:
    src: btrfs_backup.sh.j2
    dest: /usr/local/scripts/btrfs_backup.sh
    owner: root
    group: root
    mode: '0744'
  become: yes
# backup home
- name: Create btrfs_backup_home.service
  become: yes
  copy:
    dest: "/etc/systemd/system/btrfs_backup_home.service"
    content: |
      [Unit]
      Description=Takes snapshots and backs up btrfs home volume on a schedule

      [Service]
      ExecStart=/usr/local/scripts/btrfs_backup.sh /home
- name: Create btrfs_backup_home.service
  become: yes
  copy:
    dest: "/etc/systemd/system/btrfs_backup_home.timer"
    content: |
      [Unit]
      Description=btrfs home backup timer

      [Timer]
      OnCalendar=hourly
      AccuracySec=10min
      Persistent=true
      Unit=btrfs_backup_home.service

      [Install]
      WantedBy=multi-user.target
- name: Enable service btrfs_backup_home and ensure it's started
  become: yes
  ansible.builtin.systemd:
    name: btrfs_backup_home.timer
    state: started
    daemon_reload: yes
# backup root
- name: Create btrfs_backup_root.service
  become: yes
  copy:
    dest: "/etc/systemd/system/btrfs_backup_root.service"
    content: |
      [Unit]
      Description=Takes snapshots and backs up btrfs root volume on a schedule

      [Service]
      ExecStart=/usr/local/scripts/btrfs_backup.sh /
- name: Create btrfs_backup_root.service
  become: yes
  copy:
    dest: "/etc/systemd/system/btrfs_backup_root.timer"
    content: |
      [Unit]
      Description=btrfs root backup timer

      [Timer]
      OnCalendar=hourly
      AccuracySec=10min
      Persistent=true
      Unit=btrfs_backup_root.service

      [Install]
      WantedBy=multi-user.target
- name: Enable service btrfs_backup_root and ensure it's started
  become: yes
  ansible.builtin.systemd:
    name: btrfs_backup_root.timer
    state: started
    daemon_reload: yes
