Optimized udev rules for backup to usb with the help of chat gpt https://chat.openai.com/share/a75ca771-d8a4-4b75-9912-c515ba371ae4

This commit is contained in:
Kevin Veen-Birkenbach 2023-05-27 23:04:22 +02:00
parent 3341fc56ac
commit b8a23f95db
23 changed files with 86 additions and 85 deletions

View File

@ -16,7 +16,6 @@ on_calendar_docker_compose_restart_unhealthy: "*-*-* 09,10,11,12,13,14,15,16,17,
on_calendar_pull_primary_backups: "*-*-* 21:30:00" on_calendar_pull_primary_backups: "*-*-* 21:30:00"
on_calendar_renew_lets_encrypt_certificates: "*-*-* 12,00:30:00" on_calendar_renew_lets_encrypt_certificates: "*-*-* 12,00:30:00"
on_calendar_deploy_mailu_certificates: "*-*-* 13,01:30:00" on_calendar_deploy_mailu_certificates: "*-*-* 13,01:30:00"
on_calendar_backup_to_swappable: "hourly"
# Space Variables # Space Variables
size_percent_maximum_backup: 75 size_percent_maximum_backup: 75

View File

@ -301,7 +301,7 @@
- name: setup backup to swappable - name: setup backup to swappable
hosts: backup_to_swappable hosts: backup_to_usb
become: true become: true
roles: roles:
- independent_backup-to-swappable - independent_backup-to-usb

View File

@ -1,8 +0,0 @@
# independent_auto-usb-backup
This tool role allows to make an automatic backup to a swappable device.
## More information
- https://unix.stackexchange.com/questions/67464/how-to-get-uuid-for-a-usb-drive
- https://unix.stackexchange.com/questions/65891/how-to-execute-a-shellscript-when-i-plug-in-a-usb-device
- https://askubuntu.com/questions/25071/how-to-run-a-script-when-a-specific-flash-drive-is-mounted
- https://wiki.archlinux.org/title/udev

View File

@ -1,12 +0,0 @@
- name: "reload backup-to-swappable.service"
systemd:
name: backup-to-swappable.service
state: reloaded
enabled: yes
daemon_reload: yes
- name: "restart backup-to-swappable.timer"
systemd:
name: backup-to-swappable.timer
state: restarted
enabled: yes
daemon_reload: yes

View File

@ -1,4 +0,0 @@
dependencies:
- independent_user-administrator
- independent_systemd-email
- independent_backups-cleanup-service

View File

@ -1,22 +0,0 @@
- name: "create {{backup_to_swappable_folder}}"
file:
path: "{{backup_to_swappable_folder}}"
state: directory
mode: 0755
- name: create backup-to-swappable.sh
copy:
src: backup-to-swappable.sh
dest: "{{backup_to_swappable_folder}}backup-to-swappable.sh"
- name: create backup-to-swappable.service
template:
src: backup-to-swappable.service.j2
dest: /etc/systemd/system/backup-to-swappable.service
notify: reload backup-to-swappable.service
- name: create backup-to-swappable.timer
template:
src: backup-to-swappable.timer.j2
dest: /etc/systemd/system/backup-to-swappable.timer
notify: restart backup-to-swappable.timer

View File

@ -1,9 +0,0 @@
[Unit]
Description=backing up data
OnFailure=systemd-email@%n.service
ConditionPathExists={{backup_to_swappable_destination_path}}
Wants=backups-cleanup.service
[Service]
Type=oneshot
ExecStart=/usr/bin/bash {{backup_to_swappable_script_path}} {{backup_to_swappable_destination_path}} {{backup_to_swappable_source_path}}

View File

@ -1,10 +0,0 @@
[Unit]
Description=starts backup-to-swappable.service
[Timer]
OnCalendar={{on_calendar_backup_to_swappable}}
RandomizedDelaySec={{randomized_delay_sec}}
Persistent=true
[Install]
WantedBy=timers.target

View File

@ -1,2 +0,0 @@
backup_to_swappable_folder: "{{path_administrator_scripts}}backup-to-swappable/"
backup_to_swappable_script_path: "{{backup_to_swappable_folder}}backup-to-swappable.sh"

View File

@ -0,0 +1,25 @@
# independent_backup-to-usb
This Ansible role automates the process of performing backups to a swappable USB device.
## Features
- Automatically starts the backup process when a specific USB device is plugged in.
- Provides a systemd service to run the backup script at boot if the USB device is already connected.
- Supports customization of the backup source path and mount point.
## Configuration
The following variables can be customized in the `vars/main.yml` file:
- `mount_point`: The mount point where the USB device will be mounted.
- `backup_to_usb_script_path`: The path to the backup script that will be executed when the USB device is connected.
## Credits
This role was created and maintained by Kevin Veen-Birkenbach.
Contact: kevin@veen.world
## More Information
For more details on how the `independent_backup-to-usb` role works, please refer to the Ansible documentation and the role's source code.

View File

@ -0,0 +1,3 @@
---
- name: Reload udev rules
command: udevadm control --reload-rules && udevadm trigger

View File

@ -0,0 +1,3 @@
---
dependencies:
- role: independent_backups-cleanup-service

View File

@ -0,0 +1,28 @@
---
- name: Copy udev rule to the rules directory
template:
src: 99-usbstick.rules.j2
dest: /etc/udev/rules.d/
notify: Reload udev rules
- name: Copy backup script to the scripts directory
template:
src: backup-to-usb.sh.j2
dest: "{{ backup_to_usb_script_path }}"
owner: root
group: root
mode: '0755'
- name: Copy systemd service to systemd directory
template:
src: backup-to-usb.service.j2
dest: /etc/systemd/system/backup-to-usb.service
owner: root
group: root
mode: '0644'
- name: Enable and start service
systemd:
name: backup-to-usb
enabled: yes
state: started

View File

@ -0,0 +1 @@
ACTION=="add", SUBSYSTEM=="block", ENV{ID_SERIAL_SHORT}=="{{ backup_to_usb_serial_short }}", RUN+="/usr/bin/systemd-mount --no-block $devnode {{ mount_point }}", SYMLINK+="backup_usb"

View File

@ -0,0 +1,10 @@
[Unit]
Description=Backup to USB when it's plugged in
After=local-fs.target
OnFailure=systemd-email@%n.service
[Service]
ExecStart={{ backup_to_usb_script_path }} {{ mount_point }}/{{ backup_to_usb_subdirectory }} {{ backup_to_usb_source_path }}
[Install]
WantedBy=multi-user.target

View File

@ -1,19 +1,19 @@
#! /bin/sh #!/bin/sh
backup_to_swappable_destination_path="$1" && backup_to_usb_destination_path="{{ mount_point }}" &&
echo "backup to swappable destination path: $backup_to_swappable_destination_path" && echo "backup to usb destination path: $backup_to_usb_destination_path" &&
source_path="$2" && source_path="{{ backup_to_usb_source_path }}" &&
echo "source path: $source_path" || exit 1 echo "source path: $source_path" || exit 1
if [ ! -d "$backup_to_swappable_destination_path" ]; then if [ ! -d "$backup_to_usb_destination_path" ]; then
echo "Directory $backup_to_swappable_destination_path does not exist" && echo "Directory $backup_to_usb_destination_path does not exist" &&
exit 1 exit 1
fi fi
machine_id="$(sha256sum /etc/machine-id | head -c 64 )" && machine_id="$(sha256sum /etc/machine-id | head -c 64 )" &&
echo "machine id: $machine_id" && echo "machine id: $machine_id" &&
versions_path="$backup_to_swappable_destination_path$machine_id/backup-to-swappable/" && versions_path="$backup_to_usb_destination_path$machine_id/backup-to-usb/" &&
echo "versions path: $versions_path" || exit 1 echo "versions path: $versions_path" || exit 1
if [ ! -d "$versions_path" ]; then if [ ! -d "$versions_path" ]; then
@ -30,6 +30,6 @@ echo "current versions path: $current_version_path" &&
echo "creating backup destination folder..." && echo "creating backup destination folder..." &&
mkdir -vp "$current_version_path" && mkdir -vp "$current_version_path" &&
echo "Starting syncronization..." echo "Starting synchronization..."
rsync -abP --delete --delete-excluded --link-dest="$previous_version_path" "$source_path" "$current_version_path" && rsync -abP --delete --delete-excluded --link-dest="$previous_version_path" "$source_path" "$current_version_path" &&
echo "Syncronization finished." || exit 1 echo "Synchronization finished." || exit 1

View File

@ -0,0 +1,4 @@
---
mount_point: "/mnt/usbstick_{{ backup_to_usb_serial_short }}"
backup_to_usb_script_path: "/usr/local/sbin/backup-to-usb.sh"
backups_folder_path: "{{mount_point}}{{backup_to_usb_subdirectory}}"

View File

@ -4,12 +4,6 @@
name: name:
- caffeine-ng - caffeine-ng
- name: Check if caffeine is installed
command: command -v caffeine
register: caffeine_installed
changed_when: False
failed_when: caffeine_installed.rc != 0
- name: Copy caffeine systemd service file - name: Copy caffeine systemd service file
template: template:
src: caffeine.service.j2 src: caffeine.service.j2

View File

@ -19,6 +19,7 @@
validate: 'visudo -cf %s' validate: 'visudo -cf %s'
- name: Upgrade the system using yay, only act on AUR packages. - name: Upgrade the system using yay, only act on AUR packages.
become: false
kewlfft.aur.aur: kewlfft.aur.aur:
upgrade: yes upgrade: yes
use: yay use: yay