role_borg_backups/tasks/main.yml

114 lines
3.2 KiB
YAML

---
###########################################################################
#
# This role will set up our borg backup configuration.
#
###########################################################################
# tasks file for borg_backups
- debug: msg="Setting up borg backups (borg-bu)"
############################################################
- name: Make sure borgbackup package is installed (Debian)
apt:
name: borgbackup
state: latest
when: ansible_os_family == "Debian"
############################################################
- name: Make sure borgbackup package is installed (Arch)
pacman:
name: borgbackup
state: latest
when: ansible_os_family == "Archlinux"
############################################################
- name: Create /usr/local/log directory
file:
path: "/usr/local/log"
state: directory
owner: "root"
group: "root"
mode: "0755"
############################################################
- name: checkout borg_backups repository
ansible.builtin.git:
repo: 'http://git.lan/radar231/borg_backups'
dest: "/tmp/{{ ansible_hostname }}/borg_backups"
delegate_to: 127.0.0.1
############################################################
- name: Copy files to /usr/local/bin
copy:
src: "/tmp/{{ ansible_hostname }}/borg_backups/{{ item }}"
dest: "/usr/local/bin/{{ item }}"
owner: "root"
group: "root"
mode: "0755"
loop:
- borg-bu
- borg-bu-wrapper
- borg-check
- borg-info
- borg-init
- borg-list
- borg-repo
- borg-vars
- borg-offsite-vars
- borg-user-vars
- borg-user-offsite-vars
############################################################
- name: Copy bu-list to /usr/local/etc (if it doesn't exist)
copy:
src: "/tmp/{{ ansible_hostname }}/borg_backups/bu-list"
dest: "/usr/local/etc/bu-list"
owner: "root"
group: "root"
mode: "0644"
force: false
############################################################
- name: Add backup entry to root crontab
cron:
name: "Daily backup"
minute: "00"
hour: "01"
job: "/usr/local/bin/borg-bu-wrapper /usr/local/bin/borg-vars >>/usr/local/log/borg_cron_run.log 2>&1 &"
############################################################
- name: Generate ssh keys for root user (if they dont already exist)
openssh_keypair:
path: "/root/.ssh/id_rsa"
comment: "root@{{ ansible_hostname }}"
owner: "root"
group: "root"
force: false
regenerate: never
############################################################
- name: Display root public ssh key
command: "cat /root/.ssh/id_rsa.pub"
register: command_output
############################################################
- name: Print message and ssh key
debug:
msg:
- "The following steps must be followed before backups will work;"
- ""
- "1. Copy the following public ssh key to the borg authorized_keys file on the backup server host:"
- ""
- "{{ command_output.stdout }}"
- ""
- "2. Run the 'borg-init' script to initialize the backup repository"
- ""
############################################################
- name: clean up cloned git repo
command: 'rm -rf /tmp/{{ ansible_hostname }}/borg_backups'
delegate_to: 127.0.0.1
# EOF