Add minio backup client.

This commit is contained in:
Alex Manning 2023-10-22 22:21:25 +01:00
parent a4c5e72c0b
commit 289271dcf9
3 changed files with 89 additions and 0 deletions

11
minio-backup/Dockerfile Normal file
View file

@ -0,0 +1,11 @@
ARG BUILD_FROM
FROM ${BUILD_FROM}
ENV LANG C.UTF-8
COPY run.py /
RUN chmod a+x /run.py
RUN pip3 install --upgrade minio
CMD [ "/run.py" ]

37
minio-backup/config.yaml Normal file
View file

@ -0,0 +1,37 @@
name: "Minio Backup"
version: "0.0.1"
slug: minio-backup
description: >-
"Backup the backup folder using minio."
arch:
- armhf
- armv7
- aarch64
- amd64
- i386
map:
- backup:rw
build_from:
armhf: ghcr.io/home-assistant/armhf-base-python
armv7: ghcr.io/home-assistant/armv7-base-python
aarch64: ghcr.io/home-assistant/aarch64-base-python
amd64: ghcr.io/home-assistant/amd64-base-python
i386: ghcr.io/home-assistant/i386-base-python
init: false
startup: "once"
advanced: true
hassio_api: true
hassio_role: "backup"
options:
minio_url: ""
minio_access_key: ""
minio_secret_key: ""
minio_bucket: ""
schema:
minio_url: "str"
minio_access_key: "str"
minio_secret_key: "str"
minio_bucket: "str"

41
minio-backup/run.py Normal file
View file

@ -0,0 +1,41 @@
#!/usr/bin/python3
import json
import pathlib
import minio
def main():
# Load the user configuration.
with open("/data/options.json", "r") as f:
config = json.load(f)
client = minio.Minio(
config["minio_url"],
access_key=config["minio_access_key"],
secret_key=config["minio_secret_key"],
)
found = client.bucket_exists(config["minio_bucket"])
if not found:
client.make_bucket(config["minio_bucket"])
else:
print(f"Bucket {config['minio_bucket']} already exists")
backup_folder = pathlib.Path("/backup")
objects = [x.object_name for x in client.list_objects(config["minio_bucket"])]
to_upload = [x for x in backup_folder.iterdir() if x.name not in objects]
for file in to_upload:
client.fput_object(
config["minio_bucket"],
file.name,
str(file.resolve()),
)
print(f"Uploaded {file.name}")
if __name__ == "__main__":
main()