def __init__(self, container_name): self.container_name = container_name self.container = list( filter(lambda c: c.name == self.container_name, docker_env().containers.list()))[0] self.ldap_password = list( filter(lambda x: x.split("=")[0] == 'LDAP_ADMIN_PASSWORD', self.container.attrs.get("Config").get("Env")))[0].split( "=")[1]
def get_networks(): networks = list() for network in docker_env().networks.list(): if network: config = network.attrs.get('IPAM').get('Config') subnet = config[0].get('Subnet') if len(config) else None if subnet: networks.append([ network.attrs.get('Name'), subnet ]) return networks
def main(config: Config, tasks: list): # check if the backup directory exists for task in tasks: if task == 'database': for mariadb in config.mariadb: container = list( filter(lambda c: c.name == mariadb.container_name, docker_env().containers.list())) if not container: Printer.print( f'Database Container {mariadb.container_name} does not exist. Skipping!', 2) continue MariaDB(path=config.backup_dir, host=mariadb.host, port=mariadb.port, username=mariadb.username, password=mariadb.password, databases=mariadb.databases, skip_existing=mariadb.skip_existing, container=container[0]).backup() for mongodb in config.mongodb: container = list( filter(lambda c: c.name == mongodb.container_name, docker_env().containers.list())) if not container: Printer.print( f'Database Container {mongodb.container_name} does not exist. Skipping!', 2) continue MongoDB(path=config.backup_dir, host=mongodb.host, port=mongodb.port, username=mongodb.username, password=mongodb.password, databases=mongodb.databases, skip_existing=mongodb.skip_existing, container=container[0]).backup() for postgres in config.postgres: container = list( filter(lambda c: c.name == postgres.container_name, docker_env().containers.list())) if not container: Printer.print( f'Database Container {postgres.container_name} does not exist. Skipping!', 2) continue PostgreSQL(path=config.backup_dir, host=postgres.host, port=postgres.port, username=postgres.username, password=postgres.password, databases=postgres.databases, skip_existing=postgres.skip_existing, container=container[0]).backup() elif task == 'files' and config.files: for file in config.files: File(src=file, path=config.backup_dir).backup() elif task == 'gitlab' and config.gitlab: container = list( filter(lambda c: c.name == config.gitlab.container_name, docker_env().containers.list())) if not container: Printer.print( f'GitLab Container {config.gitlab.container_name} does not exist. Skipping!', 2) continue GitLab(container[0]).backup() # Generate Checksums checksums = Checksums(path=config.backup_dir) checksums.generate_all() # Reporting dr = DatabaseResult() dr.sortby = 'Container' dr.print() fr = FileResult() fr.print()
from docker import from_env as docker_env from helper import Printer, DatabaseResult, FileResult, MariaDB, MongoDB, PostgreSQL, File, Checksums Printer.verbose = True # Docker Databases MariaDB(username="******", password="******", path='/home/user/Downloads', databases=['mysql', 'spring'], container=list( filter(lambda c: c.name == "user_mariadb_1", docker_env().containers.list()))[0]).backup() MariaDB(path='/home/user/Downloads', databases=["mysql", "teamspeak", "nextcloud"], container=list( filter(lambda c: c.name == "user_mariadb_insecure_1", docker_env().containers.list()))[0]).backup() MongoDB(username="******", password="******", path='/home/user/Downloads', databases=['admin'], container=list( filter(lambda c: c.name == "user_mongodb_1", docker_env().containers.list()))[0]).backup() MongoDB(path='/home/user/Downloads', databases=['admin'], container=list(
def main(config: Config): # check if the backup directory exists if not config.backup_dir.endswith("/"): config.backup_dir += "/" config.backup_dir += str(datetime.utcnow().strftime("%Y-%m-%d")) # %H-%M if not os.path.exists(config.backup_dir): print_verbose(f"Creating backup directory: ({config.backup_dir})") os.makedirs(config.backup_dir) for job in jobs: if job == "database": try: container = list( filter(lambda c: c.name == config.database_container_name, docker_env().containers.list()))[0] except IndexError: print_verbose("Warning: Database Container not found!") continue data = list() for database in config.database_list: data.append( docker_db_backup(container, config.database_username, config.database_password, database, config.backup_dir)) if data: # create table table = PrettyTable() table.title = "Database Backup Status" table.field_names = ["Database", "Size", "Status"] for row in data: table.add_row(row) table.align["Database"] = "l" table.align["Size"] = "r" table.sortby = "Database" print(table) else: print("Skipped database backup - nothing to do!") if job == "files": data = list() for path in config.file_path_list: data.append(file_backup(path, config.backup_dir)) if data: # create table table = PrettyTable() table.title = "File Backup Status" table.field_names = [ "Path", "Original Size", "Compressed Size", "Status" ] for row in data: table.add_row(row) table.align["Path"] = "l" table.align["Original Size"] = "r" table.align["Compressed Size"] = "r" print(table) else: print("Skipped file backup - nothing to do!") if job == "gitlab": print("GitLab repository backup has been started!") try: container = list( filter(lambda c: c.name == config.gitlab_container_name, docker_env().containers.list()))[0] except IndexError: print_verbose("Warning: Gitlab Container not found!") continue res = container.exec_run("gitlab-rake gitlab:backup:create") if res.exit_code == 0: print( f"{Fore.GREEN}GitLab repository backup complete!{Fore.RESET}" ) else: print( f"{Fore.RED}GitLab repository backup failed.{Fore.RESET}") print_verbose(f"Status Code: {res.status_code}") print_verbose(f"Stdout: {res.output}") # create checksum's files_to_skip = [f"{method}sum.txt" for method in config_object.checksums] if check_check_sums(config_object.checksums): for method in config_object.checksums: with open(f"{config_object.backup_dir}/{method}sum.txt", "a") as f: for filename in os.listdir(config_object.backup_dir): if filename in files_to_skip: continue val = getattr(hashlib, method)(open( f"{config_object.backup_dir}/{filename}", "rb").read()).hexdigest() f.write(f"{val}\t{filename}\n") else: print("Config Error: Invalid checksum methods, skipping...") if int(time() - START) > 5: print(convert_time(int(time() - START)))