diff --git a/proxmox/backup_gitea_gce.py b/proxmox/backup_gitea_gce.py new file mode 100755 index 0000000..67723e5 --- /dev/null +++ b/proxmox/backup_gitea_gce.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python + +import os +import re +import subprocess +import tempfile + +# Using SSH from another machine, tell proxmox to create a snapshot of the +# gitea container, copy that snapshot to this machine, upload it to the +# google cloud storage archival bucket, and then delete the backup from +# proxmox. Why delete? Because I am a schmuck and have a too small SSD on +# my proxmox box to store many backups. +# +# We are only adding files to the bucket, and you will have to manually +# in the future go in and delete older backups. + +# The VMID (in terms of proxmox) for the Gitea container +GITEA_VMID=104 + +# IP address of our proxmox container +PROXMOX_IP="10.0.1.200" + +# Google cloud bucket we are storing our backup into. +GCE_BUCKET="gs://c9ad486f2e268b556ce305c1231b4e2d" + +# We are backing the container image to Google Cloud Platform, specifically +# a cloud storage bucket with a storage class of Archive. Pricing is as of +# writing this guide, assuming 5 GB per snapshot. +# +# Assuming ~5 GB per Snapshot; +# Storage (Archive Class): $0.0012/GB/Month => $0.006/Month +# Storage (Archive + Minimum Duration): 12 Months * $0.006 => $0.72 +# Network Egress: $0.12/GB => $0.60 +# Retrival: $0.05/GB => $0.25 + +# First we tell proxmox to create a backup using snapshot mode of our +# gitea container, compressing it with zstd. +ps = subprocess.run(\ + f'ssh root@{PROXMOX_IP} vzdump {GITEA_VMID} --mode snapshot --compress zstd --zstd 0 --dumpdir /tmp', \ + shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, check=True, text=True) + +# Extract the path to the generated backup image. +backup_filepath = re.findall("creating vzdump archive \'(.*)\'", ps.stdout)[0] + +# Send it to us so we can upload it locally without having to play around with credential handling +# for gsutil. +with tempfile.TemporaryDirectory() as local_tmpdir: + print(f'Using {local_tmpdir} as temporary directory to store local copy of {backup_filepath} from proxmox...') + + # Copy it to this machine. + subprocess.call(f'rsync -avP root@{PROXMOX_IP}:\'{backup_filepath}\' {local_tmpdir}/', shell=True) + + # And upload it to our bucket. + subprocess.call(f'gsutil cp {local_tmpdir}/* {GCE_BUCKET}', shell=True) \ No newline at end of file