add remove local backups
This commit is contained in:
		
							parent
							
								
									c9fbad8604
								
							
						
					
					
						commit
						e1a28abc61
					
				| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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:
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -27,6 +27,8 @@ protocol: https
 | 
			
		|||
remote: gitlab.example.com.ru/secure/backup-network-device.git
 | 
			
		||||
# ветка
 | 
			
		||||
branch: main
 | 
			
		||||
# Удаление локального репозитория после push
 | 
			
		||||
remove_local: False
 | 
			
		||||
 | 
			
		||||
[bcp]
 | 
			
		||||
# Файл со списком устройств. Должен лежать в папке config
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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:
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										30
									
								
								app/repo.py
								
								
								
								
							
							
						
						
									
										30
									
								
								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)
 | 
			
		||||
		Loading…
	
		Reference in New Issue