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]
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 }}

View File

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

View File

@ -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)

View File

@ -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