diff --git a/playbooks/filesystem/yggdrasil/etc/systemd/system/restic-service-data.service.j2 b/playbooks/filesystem/yggdrasil/etc/systemd/system/restic-volume-data.service.j2 similarity index 51% rename from playbooks/filesystem/yggdrasil/etc/systemd/system/restic-service-data.service.j2 rename to playbooks/filesystem/yggdrasil/etc/systemd/system/restic-volume-data.service.j2 index 9f8b30a..6f5b4c9 100644 --- a/playbooks/filesystem/yggdrasil/etc/systemd/system/restic-service-data.service.j2 +++ b/playbooks/filesystem/yggdrasil/etc/systemd/system/restic-volume-data.service.j2 @@ -1,5 +1,5 @@ [Unit] -Description=Backup service data snapshots using restic +Description=Backup volume snapshots using restic Documentation=man:restic(8) [Service] @@ -8,4 +8,4 @@ Environment=TZ=UTC Environment=RESTIC_CACHE_DIR=/var/cache/restic Environment=RESTIC_PASSWORD_FILE=/etc/restic.password EnvironmentFile=/etc/scaleway.keys -ExecStart=/usr/local/sbin/restic-service-data --root-dataset rpool/var/lib/{{ ansible_hostname }}/data --bucket-endpoint {{ scw_bucket_endpoint }} +ExecStart=/usr/local/sbin/restic-volume-data --root-dataset rpool/var/lib/{{ ansible_hostname }}/data --bucket-endpoint {{ scw_bucket_endpoint }} diff --git a/playbooks/filesystem/yggdrasil/etc/systemd/system/restic-service-data.timer b/playbooks/filesystem/yggdrasil/etc/systemd/system/restic-volume-data.timer similarity index 75% rename from playbooks/filesystem/yggdrasil/etc/systemd/system/restic-service-data.timer rename to playbooks/filesystem/yggdrasil/etc/systemd/system/restic-volume-data.timer index 56cf7d1..167f117 100644 --- a/playbooks/filesystem/yggdrasil/etc/systemd/system/restic-service-data.timer +++ b/playbooks/filesystem/yggdrasil/etc/systemd/system/restic-volume-data.timer @@ -1,5 +1,5 @@ [Unit] -Description=Daily restic backup +Description=Daily restic volume backup Documentation=man:restic(8) [Timer] diff --git a/playbooks/filesystem/yggdrasil/usr/local/sbin/restic-service-data b/playbooks/filesystem/yggdrasil/usr/local/sbin/restic-volume-data similarity index 77% rename from playbooks/filesystem/yggdrasil/usr/local/sbin/restic-service-data rename to playbooks/filesystem/yggdrasil/usr/local/sbin/restic-volume-data index 3a84805..aa38dc7 100644 --- a/playbooks/filesystem/yggdrasil/usr/local/sbin/restic-service-data +++ b/playbooks/filesystem/yggdrasil/usr/local/sbin/restic-volume-data @@ -5,17 +5,18 @@ import os import subprocess -def get_service_datasets(root_dataset): +def get_volume_datasets(root_dataset): zfs_list = subprocess.getoutput( f"zfs list -H -r {root_dataset} -o name,mountpoint" ) zfs_list_lines = zfs_list.split('\n') zfs_list_lines_items = map(lambda l: l.split(), zfs_list_lines) return { - os.path.basename(dataset): { + os.path.relpath(dataset, root_dataset).replace("/", "---"): { "dataset": dataset, "mountpoint": mountpoint, - } for dataset, mountpoint in zfs_list_lines_items if dataset != root_dataset + } for dataset, mountpoint in zfs_list_lines_items if ((dataset != root_dataset) and + os.path.ismount(mountpoint)) } def get_last_daily_snapshot(dataset): @@ -36,19 +37,20 @@ if __name__ == "__main__": help="S3 bucket endpoint for the backups") args = parser.parse_args() - service_datasets = get_service_datasets(args.root_dataset) - for service, properties in service_datasets.items(): + volume_datasets = get_volume_datasets(args.root_dataset) + for volume, properties in volume_datasets.items(): properties["snapshot"] = get_last_daily_snapshot(properties["dataset"]) - for service, properties in service_datasets.items(): + for volume, properties in volume_datasets.items(): mountpoint = properties["mountpoint"] snapshot = properties["snapshot"] backup_path = os.path.normpath(os.path.join("/", "mnt", os.path.relpath(mountpoint, "/"))) + bucket_name = f"the-nine-worlds---{volume}" restic_cmd_base = "restic " \ - f"--repo s3:https://{args.bucket_endpoint}/the-nine-worlds---{service} " \ + f"--repo s3:https://{args.bucket_endpoint}/{bucket_name} " \ "--option s3.storage-class=ONEZONE_IA" - print(f"Backing up {service} : {snapshot}", flush=True) + print(f"Backing up {bucket_name} : {snapshot}", flush=True) subprocess.run(f"zfs clone -o mountpoint={backup_path} {snapshot} rpool/restic", shell=True, check=True) diff --git a/playbooks/tasks/backups/02-restic-enable.yml b/playbooks/tasks/backups/02-restic-enable.yml index 0cac96c..6d98c31 100644 --- a/playbooks/tasks/backups/02-restic-enable.yml +++ b/playbooks/tasks/backups/02-restic-enable.yml @@ -1,20 +1,20 @@ - name: Install the restic backup script copy: - src: ./filesystem/{{ ansible_hostname }}/usr/local/sbin/restic-service-data - dest: /usr/local/sbin/restic-service-data + src: ./filesystem/{{ ansible_hostname }}/usr/local/sbin/restic-volume-data + dest: /usr/local/sbin/restic-volume-data mode: 0755 - name: Install the restic backup service file template: - src: ./filesystem/{{ ansible_hostname }}/etc/systemd/system/restic-service-data.service.j2 - dest: /etc/systemd/system/restic-service-data.service + src: ./filesystem/{{ ansible_hostname }}/etc/systemd/system/restic-volume-data.service.j2 + dest: /etc/systemd/system/restic-volume-data.service mode: 0644 register: systemd_restic_service_data_service_file - name: Install the restic backup timer file copy: - src: ./filesystem/{{ ansible_hostname }}/etc/systemd/system/restic-service-data.timer - dest: /etc/systemd/system/restic-service-data.timer + src: ./filesystem/{{ ansible_hostname }}/etc/systemd/system/restic-volume-data.timer + dest: /etc/systemd/system/restic-volume-data.timer mode: 0644 register: systemd_restic_service_data_timer_file @@ -27,6 +27,6 @@ - name: Enable restic backup systemd: - name: restic-service-data.timer + name: restic-volume-data.timer enabled: yes state: started