Add logging.

This commit is contained in:
Alex Manning 2023-10-24 18:28:45 +01:00
parent 4473fe96d5
commit 2bd31b8736
2 changed files with 27 additions and 3 deletions

View file

@ -1,5 +1,5 @@
name: "Minio Backup" name: "Minio Backup"
version: "0.0.4" version: "0.0.5"
slug: minio-backup slug: minio-backup
description: >- description: >-
"Backup the backup folder using minio." "Backup the backup folder using minio."

View file

@ -1,12 +1,25 @@
#!/usr/local/bin/python #!/usr/local/bin/python
import json import json
import logging
import pathlib import pathlib
import sys
import minio import minio
def main(): def main():
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
handler.setFormatter(formatter)
logger.addHandler(handler)
# Load the user configuration. # Load the user configuration.
with open("/data/options.json", "r") as f: with open("/data/options.json", "r") as f:
config = json.load(f) config = json.load(f)
@ -16,17 +29,26 @@ def main():
access_key=config["minio_access_key"], access_key=config["minio_access_key"],
secret_key=config["minio_secret_key"], secret_key=config["minio_secret_key"],
) )
logger.info("Created minio client")
found = client.bucket_exists(config["minio_bucket"]) found = client.bucket_exists(config["minio_bucket"])
if not found: if not found:
logger.warn("Creating bucket %s", config["minio_bucket"])
client.make_bucket(config["minio_bucket"]) client.make_bucket(config["minio_bucket"])
else: else:
print(f"Bucket {config['minio_bucket']} already exists") logger.info("Bucket %s already exists.", config["minio_bucket"])
backup_folder = pathlib.Path("/backup") backup_folder = pathlib.Path("/backup")
backup_files = backup_folder.iterdir()
logger.info("Found the folllowing files in /backup: %s", backup_files)
objects = [x.object_name for x in client.list_objects(config["minio_bucket"])] objects = [x.object_name for x in client.list_objects(config["minio_bucket"])]
logger.info("Found the following files in s3: %s", objects)
to_upload = [x for x in backup_folder.iterdir() if x.name not in objects] to_upload = [x for x in backup_folder.iterdir() if x.name not in objects]
logger.warn(
"The following files do not already exist and will be backed up: %s", to_upload
)
for file in to_upload: for file in to_upload:
client.fput_object( client.fput_object(
@ -34,7 +56,9 @@ def main():
file.name, file.name,
str(file.resolve()), str(file.resolve()),
) )
print(f"Uploaded {file.name}") logger.warn("Uploaded %s", file.name)
logger.info("Done")
if __name__ == "__main__": if __name__ == "__main__":