diff --git a/files/backups/backup-all.py b/files/backups/backup-all.py index 0b600d8..52caf28 100644 --- a/files/backups/backup-all.py +++ b/files/backups/backup-all.py @@ -14,7 +14,7 @@ from dataclasses import dataclass from pathlib import Path from typing import List, Optional import requests -import configparser +import tomllib # Configure logging @@ -28,17 +28,38 @@ logging.basicConfig( ) logger = logging.getLogger(__name__) -config = configparser.ConfigParser() -config.read("/etc/backup/config.ini") +try: + with open("/etc/backup/config.toml", "rb") as config_file: + config = tomllib.load(config_file) +except OSError as e: + logger.error(f"Failed to read config file: {e}") + raise -RESTIC_REPOSITORY = config.get("restic", "RESTIC_REPOSITORY") -RESTIC_PASSWORD = config.get("restic", "RESTIC_PASSWORD") -AWS_ACCESS_KEY_ID = config.get("restic", "AWS_ACCESS_KEY_ID") -AWS_SECRET_ACCESS_KEY = config.get("restic", "AWS_SECRET_ACCESS_KEY") -AWS_DEFAULT_REGION = config.get("restic", "AWS_DEFAULT_REGION") -TELEGRAM_BOT_TOKEN = config.get("telegram", "TELEGRAM_BOT_TOKEN") -TELEGRAM_CHAT_ID = config.get("telegram", "TELEGRAM_CHAT_ID") -NOTIFICATIONS_NAME = config.get("telegram", "NOTIFICATIONS_NAME") +storage_cfg = config.get("storage", {}).get("yandex", {}) +RESTIC_REPOSITORY = storage_cfg.get("restic_repository") +RESTIC_PASSWORD = storage_cfg.get("restic_password") +AWS_ACCESS_KEY_ID = storage_cfg.get("aws_access_key_id") +AWS_SECRET_ACCESS_KEY = storage_cfg.get("aws_secret_access_key") +AWS_DEFAULT_REGION = storage_cfg.get("aws_default_region") + +notifications_cfg = config.get("notifications", {}).get("telegram", {}) +TELEGRAM_BOT_TOKEN = notifications_cfg.get("telegram_bot_token") +TELEGRAM_CHAT_ID = notifications_cfg.get("telegram_chat_id") +NOTIFICATIONS_NAME = notifications_cfg.get("notifications_name") + +if not all( + [ + RESTIC_REPOSITORY, + RESTIC_PASSWORD, + AWS_ACCESS_KEY_ID, + AWS_SECRET_ACCESS_KEY, + AWS_DEFAULT_REGION, + TELEGRAM_BOT_TOKEN, + TELEGRAM_CHAT_ID, + NOTIFICATIONS_NAME, + ] +): + raise ValueError("Missing required configuration values in config.toml") # File name to store directories and files to back up BACKUP_TARGETS_FILE = "backup-targets" diff --git a/files/backups/config.template.toml b/files/backups/config.template.toml new file mode 100644 index 0000000..b8ba4d5 --- /dev/null +++ b/files/backups/config.template.toml @@ -0,0 +1,17 @@ +roots = [ + "{{ application_dir }}" +] + +[storage.yandex] +type = "restic" +restic_repository = "{{ restic_repository }}" +restic_password = "{{ restic_password }}" +aws_access_key_id = "{{ restic_s3_access_key }}" +aws_secret_access_key = "{{ restic_s3_access_secret }}" +aws_default_region = "{{ restic_s3_region }}" + +[notifications.telegram] +type = "telegram" +telegram_bot_token = "{{ notifications_tg_bot_token }}" +telegram_chat_id = "{{ notifications_tg_chat_id }}" +notifications_name = "{{ notifications_name }}"