diff --git a/app/backup.py b/app/backup.py index 0e4d6d5..7683bc3 100644 --- a/app/backup.py +++ b/app/backup.py @@ -137,6 +137,7 @@ def save_snr_bcp(host: str, name: str) -> None: connection = connect_to_device( vendor="snr", host=host, + name=name, ) if connection is None: return @@ -160,6 +161,7 @@ def save_cisco_sb_bcp(host: str, name: str) -> None: connection = connect_to_device( vendor="cisco_sb", host=host, + name=name, ) if connection is None: return @@ -183,6 +185,7 @@ def save_cisco_bcp(host: str, name: str) -> None: connection = connect_to_device( vendor="cisco", host=host, + name=name, ) if connection is None: return diff --git a/app/config.py b/app/config.py index 1c4c7b6..28e234b 100644 --- a/app/config.py +++ b/app/config.py @@ -84,6 +84,7 @@ class ConfigGit: protocol: str = config["git"].get("protocol") username: str = os.getenv("GIT_USERNAME") token: str = os.getenv("GIT_TOKEN") + remove_local: bool = config["git"].getboolean("remove_local") @dataclass class ConfigMail: diff --git a/app/config/config-template.ini b/app/config/config-template.ini index 7f649dd..2680231 100644 --- a/app/config/config-template.ini +++ b/app/config/config-template.ini @@ -27,6 +27,8 @@ protocol: https remote: gitlab.example.com.ru/secure/backup-network-device.git # ветка branch: main +# Удаление локального репозитория после push +remove_local: False [bcp] # Файл со списком устройств. Должен лежать в папке config diff --git a/app/main.py b/app/main.py index 3ddb90d..fa203ee 100644 --- a/app/main.py +++ b/app/main.py @@ -8,7 +8,7 @@ from backup import ( save_cisco_bcp, ) from messages import send_mail -from repo import check_bcp_repo, push_to_remote +from repo import check_bcp_repo, push_to_remote, delete_all_files import schedule import time import os @@ -48,6 +48,8 @@ def main(): save_cisco_bcp(host=current_device["ip"], name=current_device["name"]) if cfg.git.push: push_to_remote() + if cfg.git.remove_local: + delete_all_files() if cfg.mail.send: if os.path.isfile(log_file): with open(log_file, 'r', encoding='utf-8') as file: diff --git a/app/repo.py b/app/repo.py index 0ae1136..86e4c1d 100644 --- a/app/repo.py +++ b/app/repo.py @@ -1,8 +1,10 @@ -from git import Repo, InvalidGitRepositoryError, Git +from git import Repo, InvalidGitRepositoryError, Git, rmtree from config import cfg import logging as log from datetime import datetime - +import os +import shutil +import stat def check_remote_repo(repo: Repo): log.info("check remote repo ") @@ -77,3 +79,27 @@ def push_to_remote(): remote = repo.remote(name=cfg.git.branch) remote.push(refspec=f"{cfg.git.branch}:{cfg.git.branch}") log.info("push to remote repo %r", cfg.git.branch) + + +def delete_all_files(path: str = cfg.bcp.dir) -> None: + + if os.path.exists(path) and os.path.isdir(path): + for filename in os.listdir(path): + file_path = os.path.join(path, filename) + if filename == ".git": + log.info('start remove read only for .git') + for root, dirs, files in os.walk(file_path): + os.chmod(root, stat.S_IWRITE) + for file in files: + d_file_path = os.path.join(root, file) + os.chmod(d_file_path, stat.S_IWRITE) + log.info('end remove read only for .git') + try: + if os.path.isfile(file_path) or os.path.islink(file_path): + os.remove(file_path) + log.info("Successfully deleting: %s", file_path) + elif os.path.isdir(file_path): + shutil.rmtree(file_path) + log.info("Successfully deleting: %s", file_path) + except Exception as e: + log.warning("Error deleting %s: %s", file_path, e) \ No newline at end of file