diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2018-12-29 16:55:31 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2018-12-29 16:55:31 +0100 |
commit | fadb933360e65a0d6092b7864a5b74a88d519b42 (patch) | |
tree | 6b52a89ea11f05e85f6262d55ff41d1c5becc8a2 /ansible/roles | |
parent | 5b59f49bd64aec53fe109bc62fe821f469f8d95b (diff) | |
download | infra-fadb933360e65a0d6092b7864a5b74a88d519b42.tar.gz infra-fadb933360e65a0d6092b7864a5b74a88d519b42.tar.bz2 infra-fadb933360e65a0d6092b7864a5b74a88d519b42.tar.xz infra-fadb933360e65a0d6092b7864a5b74a88d519b42.zip |
borg: adding systemd timer for create-backup.
Diffstat (limited to 'ansible/roles')
-rw-r--r-- | ansible/roles/borg-client/handlers/main.yml | 10 | ||||
-rw-r--r-- | ansible/roles/borg-client/tasks/borg-client.yml | 113 | ||||
-rw-r--r-- | ansible/roles/borg-client/tasks/main.yml | 126 |
3 files changed, 125 insertions, 124 deletions
diff --git a/ansible/roles/borg-client/handlers/main.yml b/ansible/roles/borg-client/handlers/main.yml new file mode 100644 index 0000000..6e31199 --- /dev/null +++ b/ansible/roles/borg-client/handlers/main.yml @@ -0,0 +1,10 @@ +- name: systemd daemon-reload + become: yes + systemd: + daemon_reload: true + +- name: service start create-backup.timer + become: yes + service: + name: create-backup.timer + state: restarted diff --git a/ansible/roles/borg-client/tasks/borg-client.yml b/ansible/roles/borg-client/tasks/borg-client.yml new file mode 100644 index 0000000..97c4ec4 --- /dev/null +++ b/ansible/roles/borg-client/tasks/borg-client.yml @@ -0,0 +1,113 @@ +--- +- name: packages + tags: packages + apt: + name: "{{ items }}" + install_recommends: no + vars: + items: + - borgbackup + +- name: mkdir /etc/borg + file: + path: /etc/borg + state: directory + mode: u=rwx,go= + +- name: Generate SSH keys + command: ssh-keygen -t ed25519 -N "" -f "{{ ssh_key }}" -C "for borg @ {{ ansible_hostname }}" + args: + creates: "{{ ssh_key }}" + register: ssh_key_generated + +- when: ssh_key_generated.changed + fetch: + src: "{{ ssh_key }}.pub" + dest: "files" + +- name: /etc/borg/env + copy: + dest: /etc/borg/env + mode: u=r,go= + content: | + BORG_REPO="borg@{{ borg_client__server }}:{{ ansible_hostname }}" + BORG_RSH="ssh -i {{ ssh_key}}" + +# " + +- name: /etc/borg/excludes + when: borg_client__state == "absent" + file: + path: /etc/borg/excludes + state: absent + +- name: /etc/borg/excludes + when: borg_client__state == "present" + copy: + dest: /etc/borg/excludes + content: | + /proc + /dev + /sys + /run + +- name: /etc/borg/patterns + copy: + dest: /etc/borg/patterns + content: | + P sh + R / + {% for item in borg_client__patterns %} + {{ item }} + {% endfor %} + +- name: /usr/local/bin/create-backup + template: + dest: "/usr/local/bin/{{ item }}" + src: "bin/{{ item }}" + mode: u=rwx,go=rx + owner: root + group: root + with_items: + - create-backup + - create-backup-send-email + +- name: /etc/systemd/system/create-backup.service + copy: + dest: /etc/systemd/system/create-backup.service + content: | + [Unit] + Description=Create backup + + [Service] + Type=oneshot + WorkingDirectory=/ + ExecStart=-/usr/local/bin/create-backup + ExecStartPost=-/usr/local/bin/create-backup-send-email root@trygvis.io + SuccessExitStatus=0 1 + notify: + - systemd daemon-reload + +- name: /etc/systemd/system/create-backup.timer + copy: + dest: /etc/systemd/system/create-backup.timer + content: | + [Unit] + Description=Create backup timer + + [Timer] + OnCalendar=05:24 + + [Install] + WantedBy=timers.target + notify: + - systemd daemon-reload + - service start create-backup.timer + +# TODO: remove +- file: + path: /etc/systemd/system/create-backup-send-email.service + state: absent + register: create_backup_send_email_service + notify: + - systemd daemon-reload diff --git a/ansible/roles/borg-client/tasks/main.yml b/ansible/roles/borg-client/tasks/main.yml index 9e32b91..1afab33 100644 --- a/ansible/roles/borg-client/tasks/main.yml +++ b/ansible/roles/borg-client/tasks/main.yml @@ -1,125 +1,3 @@ ---- -- name: packages - become: yes - tags: packages - apt: - name: "{{ items }}" - install_recommends: no - vars: - items: - - borgbackup - -- name: mkdir /etc/borg - become: yes - file: - path: /etc/borg - state: directory - mode: u=rwx,go= - -- name: Generate SSH keys - become: yes - command: ssh-keygen -t ed25519 -N "" -f "{{ ssh_key }}" -C "for borg @ {{ ansible_hostname }}" - args: - creates: "{{ ssh_key }}" - register: ssh_key_generated - -- when: ssh_key_generated.changed - become: yes - fetch: - src: "{{ ssh_key }}.pub" - dest: "files" - -- name: /etc/borg/env - become: yes - copy: - dest: /etc/borg/env - mode: u=r,go= - content: | - BORG_REPO="borg@{{ borg_client__server }}:{{ ansible_hostname }}" - BORG_RSH="ssh -i {{ ssh_key}}" - -# " - -- name: /etc/borg/excludes - become: yes - when: borg_client__state == "absent" - file: - path: /etc/borg/excludes - state: absent - -- name: /etc/borg/excludes - when: borg_client__state == "present" - become: yes - copy: - dest: /etc/borg/excludes - content: | - /proc - /dev - /sys - /run - -- name: /etc/borg/patterns - become: yes - copy: - dest: /etc/borg/patterns - content: | - P sh - R / - {% for item in borg_client__patterns %} - {{ item }} - {% endfor %} - -- name: /usr/local/bin/create-backup - become: yes - template: - dest: "/usr/local/bin/{{ item }}" - src: "bin/{{ item }}" - mode: u=rwx,go=rx - owner: root - group: root - with_items: - - create-backup - - create-backup-send-email - - become: yes - copy: - dest: /etc/systemd/system/create-backup.service - content: | - [Unit] - Description=Create backup - - [Service] - Type=oneshot - WorkingDirectory=/ - ExecStart=-/usr/local/bin/create-backup - ExecStartPost=-/usr/local/bin/create-backup-send-email root@trygvis.io - SuccessExitStatus=0 1 - -# OnFailure=create-backup-send-email.service -# OnSuccess=create-backup-send-email.service - register: create_backup_service - -- when: create_backup_service.changed - become: yes - systemd: - daemon_reload: yes - -- become: yes - copy: - dest: /etc/systemd/system/create-backup-send-email.service - content: | - [Unit] - Description=Send email after creating backup - - [Service] - Type=oneshot - WorkingDirectory=/ - ExecStart=/usr/local/bin/create-backup-send-email root@trygvis.io - User=nobody - Group=systemd-journal - register: create_backup_send_email_service - -- when: create_backup_send_email_service.changed - become: yes - systemd: - daemon_reload: yes + tags: borg-client + import_tasks: borg-client.yml |