The beginnings of a backup solution

This commit is contained in:
Bradley Bickford 2025-08-17 21:01:27 -04:00
parent d9262852c8
commit 686543a1b9
14 changed files with 501 additions and 251 deletions

View File

@ -0,0 +1,42 @@
---
all:
hosts:
blacktide:
ansible_host: "192.168.3.2"
connection: "local"
web:
ansible_host: "10.26.48.3"
docker_backup:
- container_name: http-test
directories_to_backup:
- /var/www/html
backup_dir: /backup
backup_name_prefix: http-test
max_backups_kept: 5
database:
ansible_host: "10.12.34.3"
publicworks:
ansible_host: "10.77.7.3"
dmz:
ansible_host: "172.16.132.4"
games:
ansible_host: "10.20.24.3"
nfsserver:
ansible_host: "10.42.0.3"
openocean:
ansible_host: "172.16.132.2"
boardwalk:
ansible_host: "10.77.7.2"
children:
docker_nodes:
hosts:
web:
database:
publicworks:
dmz:
games:
pfsense_nodes:
hosts:
openocean:
boardwalk:

Binary file not shown.

View File

@ -0,0 +1,62 @@
---
- name: Backup Protocol
hosts: all
become: true
become_method: sudo
collections:
- community.docker
- community.general
vars:
env_backups_to_keep: 10
tasks:
- name: Run container mounts backup
ansible.builtin.import_role:
name: docker_backup
vars:
backup_rules: "{{ item }}"
when: docker_backup is defined and docker_backup | length != 0
loop: "{{ docker_backup }}"
- name: Stat the /root/infrastructure-compose folder
ansible.builtin.stat:
path: "/root/infrastructure-compose"
register: infra_compose_stat
- name: Find all .env files
ansible.builtin.find:
paths: "/root/infrastructure-compose"
patterns: ".*.env"
when: infra_compose_stat.stat.exists
register: all_env_files
- name: .env Backup block
when: infra_compose_stat.stat.exists and all_env_files.files is defined and all_env_files.files | length != 0
block:
- name: Archive .env files
community.general.archive:
path: "{{ all_env_files.files }}"
dest: >-
/backup/env/{{ inventory_hostname }}_{{ now().strftime("%Y%m%d%H%M%S") }}.tar.gz
format: gz
force_archive: true
- name: Find all .env backup files for the current host
ansible.builtin.find:
paths: "/backup/env"
patterns: "{{ inventory_hostname }}*"
register: backup_env_files
- name: If too many backups kept
when: backup_env_files.files | length > env_backups_to_keep
block:
- name: Get the oldest file paths
ansible.builtin.set_fact:
oldest_file_paths: >-
{{ (backup_env_files.files | sort(attribute='mtime'))[:backup_env_files.files | length - env_backups_to_keep] |
map(attribute=path) | list }}
- name: Remove the files
ansible.builtin.file:
path: "{{ item }}"
state: absent
loop: "{{ oldest_file_paths }}"

View File

@ -0,0 +1,38 @@
Role Name
=========
A brief description of the role goes here.
Requirements
------------
Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required.
Role Variables
--------------
A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well.
Dependencies
------------
A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles.
Example Playbook
----------------
Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too:
- hosts: servers
roles:
- { role: username.rolename, x: 42 }
License
-------
BSD
Author Information
------------------
An optional section for the role authors to include contact information, or a website (HTML is not allowed).

View File

@ -0,0 +1,2 @@
---
backup_rules: ""

View File

@ -0,0 +1,52 @@
galaxy_info:
author: your name
description: your role description
company: your company (optional)
# If the issue tracker for your role is not on github, uncomment the
# next line and provide a value
# issue_tracker_url: http://example.com/issue/tracker
# Choose a valid license ID from https://spdx.org - some suggested licenses:
# - BSD-3-Clause (default)
# - MIT
# - GPL-2.0-or-later
# - GPL-3.0-only
# - Apache-2.0
# - CC-BY-4.0
license: license (GPL-2.0-or-later, MIT, etc)
min_ansible_version: 2.1
# If this a Container Enabled role, provide the minimum Ansible Container version.
# min_ansible_container_version:
#
# Provide a list of supported platforms, and for each platform a list of versions.
# If you don't wish to enumerate all versions for a particular platform, use 'all'.
# To view available platforms and versions (or releases), visit:
# https://galaxy.ansible.com/api/v1/platforms/
#
# platforms:
# - name: Fedora
# versions:
# - all
# - 25
# - name: SomePlatform
# versions:
# - all
# - 1.0
# - 7
# - 99.99
galaxy_tags: []
# List tags for your role here, one per line. A tag is a keyword that describes
# and categorizes the role. Users find roles by searching for tags. Be sure to
# remove the '[]' above, if you add tags to this list.
#
# NOTE: A tag is limited to a single word comprised of alphanumeric characters.
# Maximum 20 tags per role.
dependencies: []
# List your role dependencies here, one per line. Be sure to remove the '[]' above,
# if you add dependencies to this list.

View File

@ -0,0 +1,54 @@
---
- name: Ensure backup rules contains the necessary fields
ansible.builtin.fail:
msg: "You must define {{ item }} in the backup ruleset"
when: not backup_rules[item] is defined
loop:
- container_name
- directories_to_backup
- backup_dir
- backup_name_prefix
- max_backups_kept
- name: Stop the running container
community.docker.docker_container:
name: "{{ backup_rules.container_name }}"
state: stopped
- name: Archive necessary directories
community.general.archive:
path: "{{ backup_rules.directories_to_backup }}"
dest: >-
{{ backup_rules.backup_dir }}/{{ backup_rules.backup_name_prefix }}_
{{ now().strftime("%Y%m%d%H%M%S") }}.tar.gz
format: gz
async: 3600
poll: 60
- name: Start the stopped container
community.docker.docker_container:
name: "{{ backup_rules.container_name }}"
state: started
- name: Find all files that start with the backup_name_prefix
ansible.builtin.find:
paths: "{{ backup_rules.backup_dir }}"
patterns: "{{ backup_rules.backup_name_prefix }}*"
register: all_backup_files
- name: If too many backups kept
when: all_backup_files.files | length > backup_rules.max_backups_kept
block:
- name: Get the oldest file paths
ansible.builtin.set_fact:
oldest_file_paths: >-
{{ (all_backup_files.files | sort(attribute='mtime'))[:all_backup_files.files | length - backup_rules.max_backups_kept] |
map(attribute=path) | list }}
- name: Remove the files
ansible.builtin.file:
path: "{{ item }}"
state: absent
loop: "{{ oldest_file_paths }}"