78 lines
2.3 KiB
YAML
78 lines
2.3 KiB
YAML
---
|
|
- name: Backup OpenWrt Configuration
|
|
hosts: openwrt
|
|
remote_user: root
|
|
gather_facts: false
|
|
roles:
|
|
- community.openwrt.init
|
|
vars:
|
|
backup_dir: "/tmp/backups/{{ inventory_hostname }}"
|
|
unarchive_where: "/tmp/openwrt"
|
|
remote_tmp_file: "/tmp/backup-{{ inventory_hostname }}-{{ lookup('pipe', 'date +%Y-%m-%d') }}.tar.gz"
|
|
tasks:
|
|
- name: Ensure local backup directory exists
|
|
ansible.builtin.file:
|
|
path: "{{ backup_dir }}"
|
|
state: directory
|
|
mode: '0755'
|
|
delegate_to: localhost
|
|
|
|
- name: Ensure unarchive_where directory exists
|
|
ansible.builtin.file:
|
|
path: "{{ unarchive_where }}"
|
|
state: directory
|
|
mode: '0755'
|
|
delegate_to: localhost
|
|
|
|
- name: Generate sysupgrade backup archive on OpenWrt
|
|
community.openwrt.command:
|
|
cmd: "sysupgrade -b {{ remote_tmp_file }}"
|
|
|
|
- name: Read backup archive into memory
|
|
community.openwrt.slurp:
|
|
src: "{{ remote_tmp_file }}"
|
|
register: backup_archive
|
|
|
|
- name: Save backup archive to Ansible controller
|
|
ansible.builtin.copy:
|
|
content: "{{ backup_archive.content | b64decode }}"
|
|
dest: "{{ backup_dir }}/{{ remote_tmp_file | basename }}"
|
|
mode: '0600'
|
|
delegate_to: localhost
|
|
|
|
- name: Remove temporary backup file from OpenWrt
|
|
community.openwrt.file:
|
|
path: "{{ remote_tmp_file }}"
|
|
state: absent
|
|
|
|
- name: Unarchive the file copied from OpenWRT
|
|
ansible.builtin.unarchive:
|
|
src: "{{ backup_dir }}/{{ remote_tmp_file | basename }}"
|
|
dest: "{{ unarchive_where }}"
|
|
remote_src: false
|
|
delegate_to: localhost
|
|
|
|
- name: Attempt to trigger a borg backup
|
|
ansible.builtin.shell:
|
|
cmd: >
|
|
borg create --stats --progress --compression lz4
|
|
ssh://borg@10.42.0.3:2222/var/backups/borg/openwrt::{now:%Y-%m-%d-%H-%M-%s}
|
|
{{ unarchive_where }}/
|
|
stdin: "{{ borg_password }}"
|
|
register: borg_backup
|
|
delegate_to: localhost
|
|
|
|
- name: Cleanup backup dir
|
|
ansible.builtin.file:
|
|
path: "{{ backup_dir }}"
|
|
state: absent
|
|
recurse: true
|
|
delegate_to: localhost
|
|
|
|
- name: Cleanup unarchive_where
|
|
ansible.builtin.file:
|
|
path: "{{ unarchive_where }}"
|
|
state: absent
|
|
recurse: true
|
|
delegate_to: localhost
|
|
|