mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2025-09-03 17:06:02 +02:00
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:
25
roles/independent_backup-to-usb/README.md
Normal file
25
roles/independent_backup-to-usb/README.md
Normal 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.
|
3
roles/independent_backup-to-usb/handlers/main.yml
Normal file
3
roles/independent_backup-to-usb/handlers/main.yml
Normal file
@@ -0,0 +1,3 @@
|
||||
---
|
||||
- name: Reload udev rules
|
||||
command: udevadm control --reload-rules && udevadm trigger
|
3
roles/independent_backup-to-usb/meta/main.yml
Normal file
3
roles/independent_backup-to-usb/meta/main.yml
Normal file
@@ -0,0 +1,3 @@
|
||||
---
|
||||
dependencies:
|
||||
- role: independent_backups-cleanup-service
|
28
roles/independent_backup-to-usb/tasks/main.yml
Normal file
28
roles/independent_backup-to-usb/tasks/main.yml
Normal 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
|
@@ -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"
|
@@ -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
|
@@ -0,0 +1,35 @@
|
||||
#!/bin/sh
|
||||
backup_to_usb_destination_path="{{ mount_point }}" &&
|
||||
echo "backup to usb destination path: $backup_to_usb_destination_path" &&
|
||||
|
||||
source_path="{{ backup_to_usb_source_path }}" &&
|
||||
echo "source path: $source_path" || exit 1
|
||||
|
||||
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_usb_destination_path$machine_id/backup-to-usb/" &&
|
||||
echo "versions path: $versions_path" || exit 1
|
||||
|
||||
if [ ! -d "$versions_path" ]; then
|
||||
echo "Creating $versions_path..." &&
|
||||
mkdir -vp $versions_path || exit 1
|
||||
fi
|
||||
|
||||
previous_version_path="$(ls -d $versions_path* | tail -1)" &&
|
||||
echo "previous versions path: $previous_version_path" &&
|
||||
|
||||
current_version_path="$versions_path$(date '+%Y%m%d%H%M%S')" &&
|
||||
echo "current versions path: $current_version_path" &&
|
||||
|
||||
echo "creating backup destination folder..." &&
|
||||
mkdir -vp "$current_version_path" &&
|
||||
|
||||
echo "Starting synchronization..."
|
||||
rsync -abP --delete --delete-excluded --link-dest="$previous_version_path" "$source_path" "$current_version_path" &&
|
||||
echo "Synchronization finished." || exit 1
|
4
roles/independent_backup-to-usb/vars/main.yml
Normal file
4
roles/independent_backup-to-usb/vars/main.yml
Normal 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}}"
|
Reference in New Issue
Block a user