Optimized svc-bkp-loc-2-usb

This commit is contained in:
Kevin Veen-Birkenbach 2025-07-15 20:29:01 +02:00
parent 5e83f306b4
commit f6c767f122
No known key found for this signature in database
GPG Key ID: 44D8F11FD62F878E
6 changed files with 43 additions and 22 deletions

View File

@ -1,30 +1,30 @@
# Docker 🐳 # Backup to USB
## Description ## Description
This Ansible role installs and manages Docker on Arch Linux systems. It ensures that Docker and Docker Compose are available, configured, and ready to run containerized workloads, while enabling seamless integration with system roles and administrative tasks. This Ansible role automates backups to a removable USB device on Arch Linux systems. It ensures that a custom Python backup script is deployed, the necessary systemd units are configured, and backups are triggered whenever the specified USB mount point becomes available.
Checkout the [administration reference](./Administration.md) for volume cleanup, container resets, and Docker network recovery.
## Overview ## Overview
Tailored for Arch Linux, this role handles the installation of Docker and Docker Compose using the systems package manager. It sets up a secure environment for managing Compose instances and ensures the Docker service is properly enabled and restarted. In addition, the role flags its state so that dependent roles can execute conditionally. Designed for Arch Linux, this role validates configuration variables (`mount`, `target`, `source`), installs the backup script, generates a systemd service, and sets up a corresponding mount unit. When the USB device is mounted, the service runs the script to synchronize files from the source directory to the USB target, preserving previous snapshots via hard links.
## Purpose ## Purpose
The purpose of this role is to automate the provisioning of Docker environments in a consistent and maintainable way. It reduces manual setup steps and enables clean integration with other infrastructure roles, making it ideal for production or homelab deployments. The purpose of this role is to provide a reliable, idempotent solution for local backups to a swappable USB drive. By automating the entire workflow—from variable checks and script deployment to service orchestration and snapshot management—it reduces manual intervention and integrates seamlessly with other CyMaIS roles for comprehensive system automation.
## Features ## Features
- **Installs Docker & Docker Compose:** Uses `pacman` to install necessary packages. * **Configuration Validation:** Fails early if any of `backup_to_usb_mount`, `backup_to_usb_target`, or `backup_to_usb_source` is empty.
- **Service Management:** Automatically enables and restarts the Docker service. * **Script Deployment:** Copies the `svc-bkp-loc-2-usb.py` backup script to the target path with correct ownership and permissions.
- **Secure Directory Creation:** Creates a secure location for Docker Compose instance files. * **Systemd Integration:** Generates and installs a systemd mount unit for the USB device and a oneshot service that triggers backup upon mount.
- **Run-once Setup Logic:** Ensures idempotent execution by controlling task flow with internal flags. * **Snapshot Backups:** Uses `rsync --link-dest` to create incremental snapshots and preserve previous versions without duplicating unchanged files.
- **System Role Integration:** Sets internal state (`docker_enabled`) for use by other CyMaIS roles. * **Idempotent Runs:** Ensures tasks only run when needed and leverages Ansibles `assert` and state management for consistent behavior.
* **Service Reload Handlers:** Automatically reloads the systemd service when template changes occur.
## Credits 📝 ## Credits
Developed and maintained by **Kevin Veen-Birkenbach**. Developed and maintained by **Kevin Veen-Birkenbach**.
Learn more at [www.veen.world](https://www.veen.world) Visit [veen.world](https://www.veen.world) for more information.
Part of the [CyMaIS Project](https://github.com/kevinveenbirkenbach/cymais) Part of the [CyMaIS Project](https://github.com/kevinveenbirkenbach/cymais)
License: [CyMaIS NonCommercial License (CNCL)](https://s.veen.world/cncl) License: [CyMaIS NonCommercial License (CNCL)](https://s.veen.world/cncl)

View File

@ -0,0 +1,3 @@
mount: "" # Place where the USB Drive will be mounted to
target: "" # Target directory to which the backups will be copied
source: "" # Source from which the backups will be copied

View File

@ -1,6 +1,20 @@
- name: Fail if any backup_to_usb variable is empty
assert:
that:
- backup_to_usb_mount != ""
- backup_to_usb_target != ""
- backup_to_usb_source != ""
fail_msg: |
One or more of the configuration variables are empty!
Please set:
- mount
- target
- source
to nonempty values in your configuration file.
- name: Copy backup script to the scripts directory - name: Copy backup script to the scripts directory
copy: copy:
src: svc-bkp-loc-2-usb.python src: svc-bkp-loc-2-usb.py
dest: "{{ backup_to_usb_script_path }}" dest: "{{ backup_to_usb_script_path }}"
owner: root owner: root
group: root group: root

View File

@ -1,6 +1,10 @@
backup_to_usb_script_path: /usr/local/sbin/svc-bkp-loc-2-usb.python application_id: "svc-bkp-loc-2-usb"
backup_to_usb_destination: '{{backup_to_usb_mount}}{{backup_to_usb_destination_subdirectory}}'
backups_folder_path: '{{backup_to_usb_destination}}' backup_to_usb_script_path: "/usr/local/sbin/svc-bkp-loc-2-usb.py"
systemctl_mount_service_name: '{{ backup_to_usb_mount | trim(''/'') | replace(''/'', backup_to_usb_destination: '{{ backup_to_usb_mount}}{{ backup_to_usb_targed }}'
''-'') }}.mount' backups_folder_path: '{{ backup_to_usb_destination }}'
application_id: svc-bkp-loc-2-usb systemctl_mount_service_name: '{{ backup_to_usb_mount | trim(''/'') | replace(''/'',''-'') }}.mount'
backup_to_usb_mount: "{{ applications | get_app_conf(application_id, 'mount') }}"
backup_to_usb_targed: "{{ applications | get_app_conf(application_id, 'target') }}"
backup_to_usb_source: "{{ applications | get_app_conf(application_id, 'source') }}"