mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2025-08-29 15:06:26 +02:00
Renamed to backup and cleanup roles
This commit is contained in:
21
roles/backup-data-to-usb/README.md
Normal file
21
roles/backup-data-to-usb/README.md
Normal file
@@ -0,0 +1,21 @@
|
||||
# backup-data-to-usb
|
||||
|
||||
This Ansible role automates the process of performing backups to a swappable USB device.
|
||||
|
||||
## Features
|
||||
|
||||
- Automatically starts the backup process when mounted to a specific destination.
|
||||
- Supports customization of the backup source path and destination.
|
||||
- Provides a systemd service to run the backup script.
|
||||
|
||||
## Author
|
||||
|
||||
This role was created and is maintained by Kevin Veen-Birkenbach.
|
||||
|
||||
## License
|
||||
|
||||
This code is released under the AGPL v3 license. Please refer to the [LICENSE](LICENSE) file for more details.
|
||||
|
||||
## Credits
|
||||
|
||||
This software was created with the assistance of [OpenAI ChatGPT](https://chat.openai.com/share/a75ca771-d8a4-4b75-9912-c515ba371ae4).
|
63
roles/backup-data-to-usb/files/backup-data-to-usb.python
Normal file
63
roles/backup-data-to-usb/files/backup-data-to-usb.python
Normal file
@@ -0,0 +1,63 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import sys
|
||||
import subprocess
|
||||
import shutil
|
||||
import os
|
||||
import glob
|
||||
import datetime
|
||||
|
||||
def main():
|
||||
source_path = sys.argv[1]
|
||||
print(f"source path: {source_path}")
|
||||
|
||||
backup_to_usb_destination_path = sys.argv[2]
|
||||
print(f"backup to usb destination path: {backup_to_usb_destination_path}")
|
||||
|
||||
if not os.path.isdir(backup_to_usb_destination_path):
|
||||
print(f"Directory {backup_to_usb_destination_path} does not exist")
|
||||
sys.exit(1)
|
||||
|
||||
machine_id = subprocess.run(["sha256sum", "/etc/machine-id"], capture_output=True, text=True).stdout.strip()[:64]
|
||||
print(f"machine id: {machine_id}")
|
||||
|
||||
versions_path = os.path.join(backup_to_usb_destination_path, f"{machine_id}/backup-data-to-usb/")
|
||||
print(f"versions path: {versions_path}")
|
||||
|
||||
if not os.path.isdir(versions_path):
|
||||
print(f"Creating {versions_path}...")
|
||||
os.makedirs(versions_path, exist_ok=True)
|
||||
|
||||
previous_version_path = max(glob.glob(f"{versions_path}*"), key=os.path.getmtime, default=None)
|
||||
print(f"previous versions path: {previous_version_path}")
|
||||
|
||||
current_version_path = os.path.join(versions_path, datetime.datetime.now().strftime("%Y%m%d%H%M%S"))
|
||||
print(f"current versions path: {current_version_path}")
|
||||
|
||||
print("Creating backup destination folder...")
|
||||
os.makedirs(current_version_path, exist_ok=True)
|
||||
|
||||
print("Starting synchronization...")
|
||||
try:
|
||||
rsync_command = [
|
||||
"rsync", "-abP", "--delete", "--delete-excluded"
|
||||
]
|
||||
if previous_version_path is not None:
|
||||
rsync_command.append("--link-dest=" + previous_version_path)
|
||||
rsync_command.extend([source_path, current_version_path])
|
||||
rsync_output = subprocess.check_output(rsync_command, stderr=subprocess.STDOUT, text=True)
|
||||
|
||||
print(rsync_output)
|
||||
print("Synchronization finished")
|
||||
sys.exit(0)
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(e.output)
|
||||
if "rsync warning: some files vanished before they could be transferred" in e.output:
|
||||
print("Synchronization finished with rsync warning")
|
||||
sys.exit(0)
|
||||
else:
|
||||
print("Synchronization failed")
|
||||
sys.exit(1)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
6
roles/backup-data-to-usb/handlers/main.yml
Normal file
6
roles/backup-data-to-usb/handlers/main.yml
Normal file
@@ -0,0 +1,6 @@
|
||||
- name: "reload backup-data-to-usb.service"
|
||||
systemd:
|
||||
name: backup-data-to-usb.service
|
||||
state: reloaded
|
||||
enabled: yes
|
||||
daemon_reload: yes
|
3
roles/backup-data-to-usb/meta/main.yml
Normal file
3
roles/backup-data-to-usb/meta/main.yml
Normal file
@@ -0,0 +1,3 @@
|
||||
---
|
||||
dependencies:
|
||||
- role: cleanup-backups-service
|
16
roles/backup-data-to-usb/tasks/main.yml
Normal file
16
roles/backup-data-to-usb/tasks/main.yml
Normal file
@@ -0,0 +1,16 @@
|
||||
- name: Copy backup script to the scripts directory
|
||||
copy:
|
||||
src: backup-data-to-usb.python
|
||||
dest: "{{ backup_to_usb_script_path }}"
|
||||
owner: root
|
||||
group: root
|
||||
mode: '0755'
|
||||
|
||||
- name: Copy systemd service to systemd directory
|
||||
template:
|
||||
src: backup-data-to-usb.service.j2
|
||||
dest: /etc/systemd/system/backup-data-to-usb.service
|
||||
owner: root
|
||||
group: root
|
||||
mode: '0644'
|
||||
notify: reload backup-data-to-usb.service
|
@@ -0,0 +1,11 @@
|
||||
[Unit]
|
||||
Description=Backup to USB when mounted to {{ backup_to_usb_mount }}
|
||||
Wants={{systemctl_mount_service_name}}
|
||||
OnFailure=systemd-notifier@%n.service
|
||||
|
||||
[Service]
|
||||
ExecStart=/bin/python {{ backup_to_usb_script_path }} {{backup_to_usb_source}} {{backup_to_usb_destination}}
|
||||
ExecStartPost=/bin/systemctl start backups-cleanup.service
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
4
roles/backup-data-to-usb/vars/main.yml
Normal file
4
roles/backup-data-to-usb/vars/main.yml
Normal file
@@ -0,0 +1,4 @@
|
||||
backup_to_usb_script_path: "/usr/local/sbin/backup-data-to-usb.python"
|
||||
backup_to_usb_destination: "{{backup_to_usb_mount}}{{backup_to_usb_destination_subdirectory}}"
|
||||
backups_folder_path: "{{backup_to_usb_destination}}"
|
||||
systemctl_mount_service_name: "{{ backup_to_usb_mount | trim('/') | replace('/', '-') }}.mount"
|
Reference in New Issue
Block a user