From b8a23f95dba069fabd5656e9d5987deb400e8e42 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Sat, 27 May 2023 23:04:22 +0200 Subject: [PATCH] Optimized udev rules for backup to usb with the help of chat gpt https://chat.openai.com/share/a75ca771-d8a4-4b75-9912-c515ba371ae4 --- group_vars/all | 1 - playbook.yml | 4 +-- .../independent_backup-to-swappable/README.md | 8 ------ .../handlers/main.yml | 12 -------- .../meta/main.yml | 4 --- .../tasks/main.yml | 22 --------------- .../templates/backup-to-swappable.service.j2 | 9 ------ .../templates/backup-to-swappable.timer.j2 | 10 ------- .../vars/main.yml | 2 -- roles/independent_backup-to-usb/README.md | 25 +++++++++++++++++ .../handlers/main.yml | 3 ++ roles/independent_backup-to-usb/meta/main.yml | 3 ++ .../independent_backup-to-usb/tasks/main.yml | 28 +++++++++++++++++++ .../templates/99-usbstick.rules.j2 | 1 + .../templates/backup-to-usb.service.j2 | 10 +++++++ .../templates/backup-to-usb.sh.j2} | 18 ++++++------ roles/independent_backup-to-usb/vars/main.yml | 4 +++ .../defaults/main.yml | 0 .../handlers/main.yml | 0 .../meta/main.yml | 0 .../tasks/main.yml | 6 ---- .../templates/caffeine.service.j2 | 0 roles/pc_system-aur-helper/tasks/main.yml | 1 + 23 files changed, 86 insertions(+), 85 deletions(-) delete mode 100644 roles/independent_backup-to-swappable/README.md delete mode 100644 roles/independent_backup-to-swappable/handlers/main.yml delete mode 100644 roles/independent_backup-to-swappable/meta/main.yml delete mode 100644 roles/independent_backup-to-swappable/tasks/main.yml delete mode 100644 roles/independent_backup-to-swappable/templates/backup-to-swappable.service.j2 delete mode 100644 roles/independent_backup-to-swappable/templates/backup-to-swappable.timer.j2 delete mode 100644 roles/independent_backup-to-swappable/vars/main.yml create mode 100644 roles/independent_backup-to-usb/README.md create mode 100644 roles/independent_backup-to-usb/handlers/main.yml create mode 100644 roles/independent_backup-to-usb/meta/main.yml create mode 100644 roles/independent_backup-to-usb/tasks/main.yml create mode 100644 roles/independent_backup-to-usb/templates/99-usbstick.rules.j2 create mode 100644 roles/independent_backup-to-usb/templates/backup-to-usb.service.j2 rename roles/{independent_backup-to-swappable/files/backup-to-swappable.sh => independent_backup-to-usb/templates/backup-to-usb.sh.j2} (62%) create mode 100644 roles/independent_backup-to-usb/vars/main.yml rename roles/{pc_application_caffeine => pc_application-caffeine}/defaults/main.yml (100%) rename roles/{pc_application_caffeine => pc_application-caffeine}/handlers/main.yml (100%) rename roles/{pc_application_caffeine => pc_application-caffeine}/meta/main.yml (100%) rename roles/{pc_application_caffeine => pc_application-caffeine}/tasks/main.yml (63%) rename roles/{pc_application_caffeine => pc_application-caffeine}/templates/caffeine.service.j2 (100%) diff --git a/group_vars/all b/group_vars/all index f72ea1e2..f10907c0 100644 --- a/group_vars/all +++ b/group_vars/all @@ -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_renew_lets_encrypt_certificates: "*-*-* 12,00:30:00" on_calendar_deploy_mailu_certificates: "*-*-* 13,01:30:00" -on_calendar_backup_to_swappable: "hourly" # Space Variables size_percent_maximum_backup: 75 diff --git a/playbook.yml b/playbook.yml index 181dfe78..a7a90dfc 100644 --- a/playbook.yml +++ b/playbook.yml @@ -301,7 +301,7 @@ - name: setup backup to swappable - hosts: backup_to_swappable + hosts: backup_to_usb become: true roles: - - independent_backup-to-swappable \ No newline at end of file + - independent_backup-to-usb \ No newline at end of file diff --git a/roles/independent_backup-to-swappable/README.md b/roles/independent_backup-to-swappable/README.md deleted file mode 100644 index 832c2c27..00000000 --- a/roles/independent_backup-to-swappable/README.md +++ /dev/null @@ -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 \ No newline at end of file diff --git a/roles/independent_backup-to-swappable/handlers/main.yml b/roles/independent_backup-to-swappable/handlers/main.yml deleted file mode 100644 index 5ba8dcc0..00000000 --- a/roles/independent_backup-to-swappable/handlers/main.yml +++ /dev/null @@ -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 diff --git a/roles/independent_backup-to-swappable/meta/main.yml b/roles/independent_backup-to-swappable/meta/main.yml deleted file mode 100644 index 3d3b8bb0..00000000 --- a/roles/independent_backup-to-swappable/meta/main.yml +++ /dev/null @@ -1,4 +0,0 @@ -dependencies: -- independent_user-administrator -- independent_systemd-email -- independent_backups-cleanup-service diff --git a/roles/independent_backup-to-swappable/tasks/main.yml b/roles/independent_backup-to-swappable/tasks/main.yml deleted file mode 100644 index 00821f0e..00000000 --- a/roles/independent_backup-to-swappable/tasks/main.yml +++ /dev/null @@ -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 \ No newline at end of file diff --git a/roles/independent_backup-to-swappable/templates/backup-to-swappable.service.j2 b/roles/independent_backup-to-swappable/templates/backup-to-swappable.service.j2 deleted file mode 100644 index ab2e42ed..00000000 --- a/roles/independent_backup-to-swappable/templates/backup-to-swappable.service.j2 +++ /dev/null @@ -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}} \ No newline at end of file diff --git a/roles/independent_backup-to-swappable/templates/backup-to-swappable.timer.j2 b/roles/independent_backup-to-swappable/templates/backup-to-swappable.timer.j2 deleted file mode 100644 index b9f30838..00000000 --- a/roles/independent_backup-to-swappable/templates/backup-to-swappable.timer.j2 +++ /dev/null @@ -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 diff --git a/roles/independent_backup-to-swappable/vars/main.yml b/roles/independent_backup-to-swappable/vars/main.yml deleted file mode 100644 index 0c31a298..00000000 --- a/roles/independent_backup-to-swappable/vars/main.yml +++ /dev/null @@ -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" \ No newline at end of file diff --git a/roles/independent_backup-to-usb/README.md b/roles/independent_backup-to-usb/README.md new file mode 100644 index 00000000..5a69ee89 --- /dev/null +++ b/roles/independent_backup-to-usb/README.md @@ -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. \ No newline at end of file diff --git a/roles/independent_backup-to-usb/handlers/main.yml b/roles/independent_backup-to-usb/handlers/main.yml new file mode 100644 index 00000000..4a05c470 --- /dev/null +++ b/roles/independent_backup-to-usb/handlers/main.yml @@ -0,0 +1,3 @@ +--- +- name: Reload udev rules + command: udevadm control --reload-rules && udevadm trigger diff --git a/roles/independent_backup-to-usb/meta/main.yml b/roles/independent_backup-to-usb/meta/main.yml new file mode 100644 index 00000000..3c52d866 --- /dev/null +++ b/roles/independent_backup-to-usb/meta/main.yml @@ -0,0 +1,3 @@ +--- +dependencies: + - role: independent_backups-cleanup-service diff --git a/roles/independent_backup-to-usb/tasks/main.yml b/roles/independent_backup-to-usb/tasks/main.yml new file mode 100644 index 00000000..03376b83 --- /dev/null +++ b/roles/independent_backup-to-usb/tasks/main.yml @@ -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 diff --git a/roles/independent_backup-to-usb/templates/99-usbstick.rules.j2 b/roles/independent_backup-to-usb/templates/99-usbstick.rules.j2 new file mode 100644 index 00000000..8b0540fc --- /dev/null +++ b/roles/independent_backup-to-usb/templates/99-usbstick.rules.j2 @@ -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" diff --git a/roles/independent_backup-to-usb/templates/backup-to-usb.service.j2 b/roles/independent_backup-to-usb/templates/backup-to-usb.service.j2 new file mode 100644 index 00000000..9418fde6 --- /dev/null +++ b/roles/independent_backup-to-usb/templates/backup-to-usb.service.j2 @@ -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 diff --git a/roles/independent_backup-to-swappable/files/backup-to-swappable.sh b/roles/independent_backup-to-usb/templates/backup-to-usb.sh.j2 similarity index 62% rename from roles/independent_backup-to-swappable/files/backup-to-swappable.sh rename to roles/independent_backup-to-usb/templates/backup-to-usb.sh.j2 index 432ac9ff..7783cb95 100644 --- a/roles/independent_backup-to-swappable/files/backup-to-swappable.sh +++ b/roles/independent_backup-to-usb/templates/backup-to-usb.sh.j2 @@ -1,19 +1,19 @@ -#! /bin/sh -backup_to_swappable_destination_path="$1" && -echo "backup to swappable destination path: $backup_to_swappable_destination_path" && +#!/bin/sh +backup_to_usb_destination_path="{{ mount_point }}" && +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 -if [ ! -d "$backup_to_swappable_destination_path" ]; then - echo "Directory $backup_to_swappable_destination_path does not exist" && +if [ ! -d "$backup_to_usb_destination_path" ]; then + echo "Directory $backup_to_usb_destination_path does not exist" && exit 1 fi machine_id="$(sha256sum /etc/machine-id | head -c 64 )" && 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 if [ ! -d "$versions_path" ]; then @@ -30,6 +30,6 @@ echo "current versions path: $current_version_path" && echo "creating backup destination folder..." && 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" && -echo "Syncronization finished." || exit 1 \ No newline at end of file +echo "Synchronization finished." || exit 1 diff --git a/roles/independent_backup-to-usb/vars/main.yml b/roles/independent_backup-to-usb/vars/main.yml new file mode 100644 index 00000000..fc51dd91 --- /dev/null +++ b/roles/independent_backup-to-usb/vars/main.yml @@ -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}}" diff --git a/roles/pc_application_caffeine/defaults/main.yml b/roles/pc_application-caffeine/defaults/main.yml similarity index 100% rename from roles/pc_application_caffeine/defaults/main.yml rename to roles/pc_application-caffeine/defaults/main.yml diff --git a/roles/pc_application_caffeine/handlers/main.yml b/roles/pc_application-caffeine/handlers/main.yml similarity index 100% rename from roles/pc_application_caffeine/handlers/main.yml rename to roles/pc_application-caffeine/handlers/main.yml diff --git a/roles/pc_application_caffeine/meta/main.yml b/roles/pc_application-caffeine/meta/main.yml similarity index 100% rename from roles/pc_application_caffeine/meta/main.yml rename to roles/pc_application-caffeine/meta/main.yml diff --git a/roles/pc_application_caffeine/tasks/main.yml b/roles/pc_application-caffeine/tasks/main.yml similarity index 63% rename from roles/pc_application_caffeine/tasks/main.yml rename to roles/pc_application-caffeine/tasks/main.yml index 1482282f..a22c6a58 100644 --- a/roles/pc_application_caffeine/tasks/main.yml +++ b/roles/pc_application-caffeine/tasks/main.yml @@ -4,12 +4,6 @@ name: - 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 template: src: caffeine.service.j2 diff --git a/roles/pc_application_caffeine/templates/caffeine.service.j2 b/roles/pc_application-caffeine/templates/caffeine.service.j2 similarity index 100% rename from roles/pc_application_caffeine/templates/caffeine.service.j2 rename to roles/pc_application-caffeine/templates/caffeine.service.j2 diff --git a/roles/pc_system-aur-helper/tasks/main.yml b/roles/pc_system-aur-helper/tasks/main.yml index 4c414988..3fc4281c 100644 --- a/roles/pc_system-aur-helper/tasks/main.yml +++ b/roles/pc_system-aur-helper/tasks/main.yml @@ -19,6 +19,7 @@ validate: 'visudo -cf %s' - name: Upgrade the system using yay, only act on AUR packages. + become: false kewlfft.aur.aur: upgrade: yes use: yay