From 8327323a4525385a0d131bfcf0d831e2177755fa Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Wed, 26 Apr 2023 22:12:40 +0200 Subject: [PATCH] Finished backup-to-swappable draft --- group_vars/all | 2 +- playbook.yml | 9 ++++- .../meta/main.yml | 2 +- .../templates/usb-auto-backup.rule.j2 | 1 - .../templates/usb-auto-backup.service.j2 | 7 ---- .../templates/usb-auto-backup.sh.j2 | 8 ----- .../README.md | 0 .../files/wireguard-ip.conf | 0 .../handlers/main.yml | 0 .../tasks/main.yml | 0 .../README.md | 7 ++-- .../files/backup-to-swappable.sh | 35 +++++++++++++++++++ .../handlers/main.yml | 12 +++++++ .../meta/main.yml | 1 + .../tasks/main.yml | 27 ++++++++++++++ .../templates/backup-to-swappable.rule.j2 | 1 + .../templates/backup-to-swappable.service.j2 | 7 ++++ .../templates/backup-to-swappable.timer.j2} | 4 +-- .../vars/main.yml | 3 ++ .../README.md | 2 +- .../tasks/main.yml | 0 .../tasks/main.yml | 0 roles/independent_system-update/meta/main.yml | 4 +-- .../README.md} | 0 .../handlers/main.yml | 0 .../tasks/main.yml | 0 .../templates/msmtprc.conf.j2 | 0 .../templates/systemd-email.sh.j2 | 0 .../templates/systemd-email@.service.j2 | 0 .../vars/main.yml | 0 roles/server_docker-mailu/meta/main.yml | 2 +- .../meta/main.yml | 2 +- .../meta/main.yml | 2 +- .../meta/main.yml | 2 +- .../server_native-certbot-nginx/meta/main.yml | 2 +- .../meta/main.yml | 2 +- .../meta/main.yml | 2 +- .../meta/main.yml | 2 +- .../meta/main.yml | 2 +- .../meta/main.yml | 2 +- 40 files changed, 115 insertions(+), 37 deletions(-) delete mode 100644 roles/independent-auto-usb-backup/templates/usb-auto-backup.rule.j2 delete mode 100644 roles/independent-auto-usb-backup/templates/usb-auto-backup.service.j2 delete mode 100644 roles/independent-auto-usb-backup/templates/usb-auto-backup.sh.j2 rename roles/{independent-application-wireguard => independent_application-wireguard}/README.md (100%) rename roles/{independent-application-wireguard => independent_application-wireguard}/files/wireguard-ip.conf (100%) rename roles/{independent-application-wireguard => independent_application-wireguard}/handlers/main.yml (100%) rename roles/{independent-application-wireguard => independent_application-wireguard}/tasks/main.yml (100%) rename roles/{independent-auto-usb-backup => independent_backup-to-swappable}/README.md (60%) create mode 100644 roles/independent_backup-to-swappable/files/backup-to-swappable.sh create mode 100644 roles/independent_backup-to-swappable/handlers/main.yml rename roles/{independent-auto-usb-backup => independent_backup-to-swappable}/meta/main.yml (62%) create mode 100644 roles/independent_backup-to-swappable/tasks/main.yml create mode 100644 roles/independent_backup-to-swappable/templates/backup-to-swappable.rule.j2 create mode 100644 roles/independent_backup-to-swappable/templates/backup-to-swappable.service.j2 rename roles/{independent-auto-usb-backup/templates/usb-auto-backup.timer.j2 => independent_backup-to-swappable/templates/backup-to-swappable.timer.j2} (53%) create mode 100644 roles/independent_backup-to-swappable/vars/main.yml rename roles/{independent-system-apt-update => independent_system-apt-update}/README.md (80%) rename roles/{independent-system-apt-update => independent_system-apt-update}/tasks/main.yml (100%) rename roles/{independent-system-pacman => independent_system-pacman}/tasks/main.yml (100%) rename roles/{server_native-systemd-email/readme.md => independent_systemd-email/README.md} (100%) rename roles/{server_native-systemd-email => independent_systemd-email}/handlers/main.yml (100%) rename roles/{server_native-systemd-email => independent_systemd-email}/tasks/main.yml (100%) rename roles/{server_native-systemd-email => independent_systemd-email}/templates/msmtprc.conf.j2 (100%) rename roles/{server_native-systemd-email => independent_systemd-email}/templates/systemd-email.sh.j2 (100%) rename roles/{server_native-systemd-email => independent_systemd-email}/templates/systemd-email@.service.j2 (100%) rename roles/{server_native-systemd-email => independent_systemd-email}/vars/main.yml (100%) diff --git a/group_vars/all b/group_vars/all index b6ee55b9..05e812f8 100644 --- a/group_vars/all +++ b/group_vars/all @@ -16,7 +16,7 @@ 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_usb_auto_backup: "*-*-* 09,12,15,18,21,24,03,06:30:00" +on_calendar_backup_to_swappable: "*-*-* 09,12,15,18,21,24,03,06:30:00" # Space Variables size_percent_maximum_backup: 75 diff --git a/playbook.yml b/playbook.yml index d8847f41..5deacbf6 100644 --- a/playbook.yml +++ b/playbook.yml @@ -20,7 +20,7 @@ hosts: wireguard_server become: true roles: - - independent-application-wireguard + - independent_application-wireguard - name: setup wireguard client behind firewall\nat hosts: wireguard_behind_firewall @@ -290,3 +290,10 @@ become: true roles: - pc_application-docker + + +- name: setup backup to swappable + hosts: backup_to_swappable + become: true + roles: + - independent_backup-to-swappable \ No newline at end of file diff --git a/roles/client_application-wireguard/meta/main.yml b/roles/client_application-wireguard/meta/main.yml index 8f894353..47d5b8da 100644 --- a/roles/client_application-wireguard/meta/main.yml +++ b/roles/client_application-wireguard/meta/main.yml @@ -1,2 +1,2 @@ dependencies: -- independent-application-wireguard \ No newline at end of file +- independent_application-wireguard \ No newline at end of file diff --git a/roles/independent-auto-usb-backup/templates/usb-auto-backup.rule.j2 b/roles/independent-auto-usb-backup/templates/usb-auto-backup.rule.j2 deleted file mode 100644 index f843fc92..00000000 --- a/roles/independent-auto-usb-backup/templates/usb-auto-backup.rule.j2 +++ /dev/null @@ -1 +0,0 @@ -ATTRS{idVendor}=="152d", ATTRS{idProduct}=="2329", RUN+="/tmp/test.sh" \ No newline at end of file diff --git a/roles/independent-auto-usb-backup/templates/usb-auto-backup.service.j2 b/roles/independent-auto-usb-backup/templates/usb-auto-backup.service.j2 deleted file mode 100644 index f2d91958..00000000 --- a/roles/independent-auto-usb-backup/templates/usb-auto-backup.service.j2 +++ /dev/null @@ -1,7 +0,0 @@ -[Unit] -Description=delete old backups -OnFailure=systemd-email@%n.service - -[Service] -Type=oneshot -ExecStart=/usr/bin/python {{docker_backups_cleanup}}backups-cleanup.py --backups-folder-path {{backups_folder_path}} --maximum-backup-size-percent {{size_percent_maximum_backup}} diff --git a/roles/independent-auto-usb-backup/templates/usb-auto-backup.sh.j2 b/roles/independent-auto-usb-backup/templates/usb-auto-backup.sh.j2 deleted file mode 100644 index 821e1d30..00000000 --- a/roles/independent-auto-usb-backup/templates/usb-auto-backup.sh.j2 +++ /dev/null @@ -1,8 +0,0 @@ -#! /bin/sh - -env >>/tmp/test.log -file "/sys${DEVPATH}" >>/tmp/test.log - -if [ "${ACTION}" = add -a -d "/sys${DEVPATH}" ]; then -echo "add ${DEVPATH}" >>/tmp/test.log -fi \ No newline at end of file diff --git a/roles/independent-application-wireguard/README.md b/roles/independent_application-wireguard/README.md similarity index 100% rename from roles/independent-application-wireguard/README.md rename to roles/independent_application-wireguard/README.md diff --git a/roles/independent-application-wireguard/files/wireguard-ip.conf b/roles/independent_application-wireguard/files/wireguard-ip.conf similarity index 100% rename from roles/independent-application-wireguard/files/wireguard-ip.conf rename to roles/independent_application-wireguard/files/wireguard-ip.conf diff --git a/roles/independent-application-wireguard/handlers/main.yml b/roles/independent_application-wireguard/handlers/main.yml similarity index 100% rename from roles/independent-application-wireguard/handlers/main.yml rename to roles/independent_application-wireguard/handlers/main.yml diff --git a/roles/independent-application-wireguard/tasks/main.yml b/roles/independent_application-wireguard/tasks/main.yml similarity index 100% rename from roles/independent-application-wireguard/tasks/main.yml rename to roles/independent_application-wireguard/tasks/main.yml diff --git a/roles/independent-auto-usb-backup/README.md b/roles/independent_backup-to-swappable/README.md similarity index 60% rename from roles/independent-auto-usb-backup/README.md rename to roles/independent_backup-to-swappable/README.md index aad4ae16..832c2c27 100644 --- a/roles/independent-auto-usb-backup/README.md +++ b/roles/independent_backup-to-swappable/README.md @@ -1,7 +1,8 @@ -# independent-auto-usb-backup -This tool role allows to make an automatic backup to an usb stick +# 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 \ No newline at end of file +- 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/files/backup-to-swappable.sh b/roles/independent_backup-to-swappable/files/backup-to-swappable.sh new file mode 100644 index 00000000..432ac9ff --- /dev/null +++ b/roles/independent_backup-to-swappable/files/backup-to-swappable.sh @@ -0,0 +1,35 @@ +#! /bin/sh +backup_to_swappable_destination_path="$1" && +echo "backup to swappable destination path: $backup_to_swappable_destination_path" && + +source_path="$2" && +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" && + 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/" && +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 syncronization..." +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 diff --git a/roles/independent_backup-to-swappable/handlers/main.yml b/roles/independent_backup-to-swappable/handlers/main.yml new file mode 100644 index 00000000..5ba8dcc0 --- /dev/null +++ b/roles/independent_backup-to-swappable/handlers/main.yml @@ -0,0 +1,12 @@ +- 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-auto-usb-backup/meta/main.yml b/roles/independent_backup-to-swappable/meta/main.yml similarity index 62% rename from roles/independent-auto-usb-backup/meta/main.yml rename to roles/independent_backup-to-swappable/meta/main.yml index 11dcdd3f..b05984a6 100644 --- a/roles/independent-auto-usb-backup/meta/main.yml +++ b/roles/independent_backup-to-swappable/meta/main.yml @@ -1,2 +1,3 @@ dependencies: - independent_user-administrator +- independent_systemd-email diff --git a/roles/independent_backup-to-swappable/tasks/main.yml b/roles/independent_backup-to-swappable/tasks/main.yml new file mode 100644 index 00000000..569bd7a8 --- /dev/null +++ b/roles/independent_backup-to-swappable/tasks/main.yml @@ -0,0 +1,27 @@ +- name: "create {{backup_to_swappable_folder}}" + file: + path: "{{backup_to_swappable_folder}}" + state: directory + mode: 0755 + +- name: create backup-to-swappable.rule + template: + src: backup-to-swappable.rule.j2 + dest: "/etc/udev/rules.d/backup-to-swappable.rule" + +- 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.rule.j2 b/roles/independent_backup-to-swappable/templates/backup-to-swappable.rule.j2 new file mode 100644 index 00000000..1aacf227 --- /dev/null +++ b/roles/independent_backup-to-swappable/templates/backup-to-swappable.rule.j2 @@ -0,0 +1 @@ +ACTION=="add", KERNEL=="sd[a-z]", ENV{ID_SERIAL_SHORT}=="{{backup_to_swappable_serial_short}}", RUN+="systemctl start backup-to-swappable.service" \ 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 new file mode 100644 index 00000000..9c90136f --- /dev/null +++ b/roles/independent_backup-to-swappable/templates/backup-to-swappable.service.j2 @@ -0,0 +1,7 @@ +[Unit] +Description=backing up data +OnFailure=systemd-email@%n.service + +[Service] +Type=oneshot +ExecStart=/usr/bin/bash {{backup_to_swappable_script_path}} \ No newline at end of file diff --git a/roles/independent-auto-usb-backup/templates/usb-auto-backup.timer.j2 b/roles/independent_backup-to-swappable/templates/backup-to-swappable.timer.j2 similarity index 53% rename from roles/independent-auto-usb-backup/templates/usb-auto-backup.timer.j2 rename to roles/independent_backup-to-swappable/templates/backup-to-swappable.timer.j2 index 141a511d..b9f30838 100644 --- a/roles/independent-auto-usb-backup/templates/usb-auto-backup.timer.j2 +++ b/roles/independent_backup-to-swappable/templates/backup-to-swappable.timer.j2 @@ -1,8 +1,8 @@ [Unit] -Description=starts usb-auto-backup.service +Description=starts backup-to-swappable.service [Timer] -OnCalendar={{on_calendar_usb_auto_backup}} +OnCalendar={{on_calendar_backup_to_swappable}} RandomizedDelaySec={{randomized_delay_sec}} Persistent=true diff --git a/roles/independent_backup-to-swappable/vars/main.yml b/roles/independent_backup-to-swappable/vars/main.yml new file mode 100644 index 00000000..ca44b8f7 --- /dev/null +++ b/roles/independent_backup-to-swappable/vars/main.yml @@ -0,0 +1,3 @@ +backup_to_swappable_folder: "{{path_administrator_scripts}}backup-to-swappable/" +backup_to_swappable_destination_path: "{{backup_to_swappable_destination_path}}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-system-apt-update/README.md b/roles/independent_system-apt-update/README.md similarity index 80% rename from roles/independent-system-apt-update/README.md rename to roles/independent_system-apt-update/README.md index b53e4086..0e70a21f 100644 --- a/roles/independent-system-apt-update/README.md +++ b/roles/independent_system-apt-update/README.md @@ -1,4 +1,4 @@ -# role independent-system-apt-update +# role independent_system-apt-update This role updates the packages # upgrade diff --git a/roles/independent-system-apt-update/tasks/main.yml b/roles/independent_system-apt-update/tasks/main.yml similarity index 100% rename from roles/independent-system-apt-update/tasks/main.yml rename to roles/independent_system-apt-update/tasks/main.yml diff --git a/roles/independent-system-pacman/tasks/main.yml b/roles/independent_system-pacman/tasks/main.yml similarity index 100% rename from roles/independent-system-pacman/tasks/main.yml rename to roles/independent_system-pacman/tasks/main.yml diff --git a/roles/independent_system-update/meta/main.yml b/roles/independent_system-update/meta/main.yml index 11b6128f..ac09ce80 100644 --- a/roles/independent_system-update/meta/main.yml +++ b/roles/independent_system-update/meta/main.yml @@ -1,3 +1,3 @@ dependencies: - - { role: independent-system-pacman, when: ansible_distribution == 'Archlinux' } - - { role: independent-system-apt-update, when: ansible_distribution == "Debian" } + - { role: independent_system-pacman, when: ansible_distribution == 'Archlinux' } + - { role: independent_system-apt-update, when: ansible_distribution == "Debian" } diff --git a/roles/server_native-systemd-email/readme.md b/roles/independent_systemd-email/README.md similarity index 100% rename from roles/server_native-systemd-email/readme.md rename to roles/independent_systemd-email/README.md diff --git a/roles/server_native-systemd-email/handlers/main.yml b/roles/independent_systemd-email/handlers/main.yml similarity index 100% rename from roles/server_native-systemd-email/handlers/main.yml rename to roles/independent_systemd-email/handlers/main.yml diff --git a/roles/server_native-systemd-email/tasks/main.yml b/roles/independent_systemd-email/tasks/main.yml similarity index 100% rename from roles/server_native-systemd-email/tasks/main.yml rename to roles/independent_systemd-email/tasks/main.yml diff --git a/roles/server_native-systemd-email/templates/msmtprc.conf.j2 b/roles/independent_systemd-email/templates/msmtprc.conf.j2 similarity index 100% rename from roles/server_native-systemd-email/templates/msmtprc.conf.j2 rename to roles/independent_systemd-email/templates/msmtprc.conf.j2 diff --git a/roles/server_native-systemd-email/templates/systemd-email.sh.j2 b/roles/independent_systemd-email/templates/systemd-email.sh.j2 similarity index 100% rename from roles/server_native-systemd-email/templates/systemd-email.sh.j2 rename to roles/independent_systemd-email/templates/systemd-email.sh.j2 diff --git a/roles/server_native-systemd-email/templates/systemd-email@.service.j2 b/roles/independent_systemd-email/templates/systemd-email@.service.j2 similarity index 100% rename from roles/server_native-systemd-email/templates/systemd-email@.service.j2 rename to roles/independent_systemd-email/templates/systemd-email@.service.j2 diff --git a/roles/server_native-systemd-email/vars/main.yml b/roles/independent_systemd-email/vars/main.yml similarity index 100% rename from roles/server_native-systemd-email/vars/main.yml rename to roles/independent_systemd-email/vars/main.yml diff --git a/roles/server_docker-mailu/meta/main.yml b/roles/server_docker-mailu/meta/main.yml index e67b2e12..0815269b 100644 --- a/roles/server_docker-mailu/meta/main.yml +++ b/roles/server_docker-mailu/meta/main.yml @@ -1,3 +1,3 @@ dependencies: - server_native-docker-reverse-proxy -- server_native-systemd-email +- independent_systemd-email diff --git a/roles/server_native-backups-cleanup/meta/main.yml b/roles/server_native-backups-cleanup/meta/main.yml index 046da348..7b67d496 100644 --- a/roles/server_native-backups-cleanup/meta/main.yml +++ b/roles/server_native-backups-cleanup/meta/main.yml @@ -1,3 +1,3 @@ dependencies: - server_native-python-pip - - server_native-systemd-email + - independent_systemd-email diff --git a/roles/server_native-backups-consumer/meta/main.yml b/roles/server_native-backups-consumer/meta/main.yml index f9a3a4f7..3e69c643 100644 --- a/roles/server_native-backups-consumer/meta/main.yml +++ b/roles/server_native-backups-consumer/meta/main.yml @@ -1,4 +1,4 @@ dependencies: - server_native-git - - server_native-systemd-email + - independent_systemd-email - server_native-backups-cleanup diff --git a/roles/server_native-btrfs-health-check/meta/main.yml b/roles/server_native-btrfs-health-check/meta/main.yml index 40dfc526..cd9e921a 100644 --- a/roles/server_native-btrfs-health-check/meta/main.yml +++ b/roles/server_native-btrfs-health-check/meta/main.yml @@ -1,2 +1,2 @@ dependencies: - - server_native-systemd-email + - independent_systemd-email diff --git a/roles/server_native-certbot-nginx/meta/main.yml b/roles/server_native-certbot-nginx/meta/main.yml index b6aed93d..b48ccb6c 100644 --- a/roles/server_native-certbot-nginx/meta/main.yml +++ b/roles/server_native-certbot-nginx/meta/main.yml @@ -1,3 +1,3 @@ dependencies: - server_native-nginx -- server_native-systemd-email +- independent_systemd-email diff --git a/roles/server_native-disc-space-check/meta/main.yml b/roles/server_native-disc-space-check/meta/main.yml index 40dfc526..cd9e921a 100644 --- a/roles/server_native-disc-space-check/meta/main.yml +++ b/roles/server_native-disc-space-check/meta/main.yml @@ -1,2 +1,2 @@ dependencies: - - server_native-systemd-email + - independent_systemd-email diff --git a/roles/server_native-docker-health-check/meta/main.yml b/roles/server_native-docker-health-check/meta/main.yml index 40dfc526..cd9e921a 100644 --- a/roles/server_native-docker-health-check/meta/main.yml +++ b/roles/server_native-docker-health-check/meta/main.yml @@ -1,2 +1,2 @@ dependencies: - - server_native-systemd-email + - independent_systemd-email diff --git a/roles/server_native-docker-volume-backup/meta/main.yml b/roles/server_native-docker-volume-backup/meta/main.yml index 1128530d..a4134a9c 100644 --- a/roles/server_native-docker-volume-backup/meta/main.yml +++ b/roles/server_native-docker-volume-backup/meta/main.yml @@ -1,4 +1,4 @@ dependencies: - server_native-git - server_native-backups-provider - - server_native-systemd-email + - independent_systemd-email diff --git a/roles/server_native-free-disc-space/meta/main.yml b/roles/server_native-free-disc-space/meta/main.yml index 40dfc526..cd9e921a 100644 --- a/roles/server_native-free-disc-space/meta/main.yml +++ b/roles/server_native-free-disc-space/meta/main.yml @@ -1,2 +1,2 @@ dependencies: - - server_native-systemd-email + - independent_systemd-email diff --git a/roles/server_native-journalctl-health-check/meta/main.yml b/roles/server_native-journalctl-health-check/meta/main.yml index 40dfc526..cd9e921a 100644 --- a/roles/server_native-journalctl-health-check/meta/main.yml +++ b/roles/server_native-journalctl-health-check/meta/main.yml @@ -1,2 +1,2 @@ dependencies: - - server_native-systemd-email + - independent_systemd-email