Update restic script from services to volumes

This commit is contained in:
Wojciech Kozlowski 2022-10-26 22:09:50 +02:00
parent 3e5ee2a90e
commit f296ed9ea5
4 changed files with 20 additions and 18 deletions

View File

@ -1,5 +1,5 @@
[Unit] [Unit]
Description=Backup service data snapshots using restic Description=Backup volume snapshots using restic
Documentation=man:restic(8) Documentation=man:restic(8)
[Service] [Service]
@ -8,4 +8,4 @@ Environment=TZ=UTC
Environment=RESTIC_CACHE_DIR=/var/cache/restic Environment=RESTIC_CACHE_DIR=/var/cache/restic
Environment=RESTIC_PASSWORD_FILE=/etc/restic.password Environment=RESTIC_PASSWORD_FILE=/etc/restic.password
EnvironmentFile=/etc/scaleway.keys 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 }}

View File

@ -1,5 +1,5 @@
[Unit] [Unit]
Description=Daily restic backup Description=Daily restic volume backup
Documentation=man:restic(8) Documentation=man:restic(8)
[Timer] [Timer]

View File

@ -5,17 +5,18 @@ import os
import subprocess import subprocess
def get_service_datasets(root_dataset): def get_volume_datasets(root_dataset):
zfs_list = subprocess.getoutput( zfs_list = subprocess.getoutput(
f"zfs list -H -r {root_dataset} -o name,mountpoint" f"zfs list -H -r {root_dataset} -o name,mountpoint"
) )
zfs_list_lines = zfs_list.split('\n') zfs_list_lines = zfs_list.split('\n')
zfs_list_lines_items = map(lambda l: l.split(), zfs_list_lines) zfs_list_lines_items = map(lambda l: l.split(), zfs_list_lines)
return { return {
os.path.basename(dataset): { os.path.relpath(dataset, root_dataset).replace("/", "---"): {
"dataset": dataset, "dataset": dataset,
"mountpoint": mountpoint, "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): def get_last_daily_snapshot(dataset):
@ -36,19 +37,20 @@ if __name__ == "__main__":
help="S3 bucket endpoint for the backups") help="S3 bucket endpoint for the backups")
args = parser.parse_args() args = parser.parse_args()
service_datasets = get_service_datasets(args.root_dataset) volume_datasets = get_volume_datasets(args.root_dataset)
for service, properties in service_datasets.items(): for volume, properties in volume_datasets.items():
properties["snapshot"] = get_last_daily_snapshot(properties["dataset"]) 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"] mountpoint = properties["mountpoint"]
snapshot = properties["snapshot"] snapshot = properties["snapshot"]
backup_path = os.path.normpath(os.path.join("/", "mnt", os.path.relpath(mountpoint, "/"))) backup_path = os.path.normpath(os.path.join("/", "mnt", os.path.relpath(mountpoint, "/")))
bucket_name = f"the-nine-worlds---{volume}"
restic_cmd_base = "restic " \ 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" "--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", subprocess.run(f"zfs clone -o mountpoint={backup_path} {snapshot} rpool/restic",
shell=True, check=True) shell=True, check=True)

View File

@ -1,20 +1,20 @@
- name: Install the restic backup script - name: Install the restic backup script
copy: copy:
src: ./filesystem/{{ ansible_hostname }}/usr/local/sbin/restic-service-data src: ./filesystem/{{ ansible_hostname }}/usr/local/sbin/restic-volume-data
dest: /usr/local/sbin/restic-service-data dest: /usr/local/sbin/restic-volume-data
mode: 0755 mode: 0755
- name: Install the restic backup service file - name: Install the restic backup service file
template: template:
src: ./filesystem/{{ ansible_hostname }}/etc/systemd/system/restic-service-data.service.j2 src: ./filesystem/{{ ansible_hostname }}/etc/systemd/system/restic-volume-data.service.j2
dest: /etc/systemd/system/restic-service-data.service dest: /etc/systemd/system/restic-volume-data.service
mode: 0644 mode: 0644
register: systemd_restic_service_data_service_file register: systemd_restic_service_data_service_file
- name: Install the restic backup timer file - name: Install the restic backup timer file
copy: copy:
src: ./filesystem/{{ ansible_hostname }}/etc/systemd/system/restic-service-data.timer src: ./filesystem/{{ ansible_hostname }}/etc/systemd/system/restic-volume-data.timer
dest: /etc/systemd/system/restic-service-data.timer dest: /etc/systemd/system/restic-volume-data.timer
mode: 0644 mode: 0644
register: systemd_restic_service_data_timer_file register: systemd_restic_service_data_timer_file
@ -27,6 +27,6 @@
- name: Enable restic backup - name: Enable restic backup
systemd: systemd:
name: restic-service-data.timer name: restic-volume-data.timer
enabled: yes enabled: yes
state: started state: started