def check_file_exists(path, container: DockerContainer) -> bool: cmd = "test -f {}".format(path) try: DockerUtils.run_cmd(cmd=cmd, container=container) return True except: return False
def check_dir_exist(path, container: DockerContainer) -> bool: cmd = "ls -A {}/".format(path) try: DockerUtils.run_cmd(cmd=cmd, container=container) return True except: return False
def check_dir_empty(path, container: DockerContainer) -> bool: cmd = "ls -A {} | [ $(wc -c) -gt 0 ]".format(path) try: DockerUtils.run_cmd(cmd=cmd, container=container) return False except: return True
def __backup_database(self, backup_dir: str): cmd_mkdir = "mkdir -p {}".format(backup_dir) DockerUtils.run_cmd(cmd=cmd_mkdir, container=DockerContainer.LOCAL) cmd = "mysqldump --single-transaction --column-statistics=0 -h {server} -u {user} -p{pwd} {db} > {path}/backup.sql".format( server=self._db_server, user=self._db_user, pwd=self._db_password, db=self._database, path=Path(backup_dir)) DockerUtils.run_cmd(cmd=cmd, container=DockerContainer.LOCAL)
def __backup_data(self, backup_dir: str, incremental=False): if incremental and not check_dir_exist(self._latest_dir, container=DockerContainer.BACKUP): logger.error("Can no perform incremental backup. Missing folder {}".format(self._latest_dir)) incremental = False if not incremental: cmd = "rsync -az {source}/ {target}/data".format(source=self._data_dir, target=Path(backup_dir)) else: cmd = "rsync -az {source}/ --link-dest {latest}/data {target}/data".format(source=self._data_dir, latest=self._latest_dir, target=Path(backup_dir)) DockerUtils.run_cmd(cmd=cmd, container=DockerContainer.BACKUP) cmd_link = "rm -rf {latest} && ln -s {backup_dir} {latest}".format(backup_dir=backup_dir, latest=self._latest_dir) DockerUtils.run_cmd(cmd=cmd_link, container=DockerContainer.BACKUP)
def __restore_database(self, backup_dir: str): # Drop cmd_drop = "mysql -h {server} -u {user} -p{pwd} -e \"DROP DATABASE {db}\"".format( server=self._db_server, user=self._db_user, pwd=self._db_password, db=self._database) DockerUtils.run_cmd(cmd=cmd_drop, container=DockerContainer.LOCAL) # Create cmd_create = "mysql -h {server} -u {user} -p{pwd} -e \"CREATE DATABASE {db}\"".format( server=self._db_server, user=self._db_user, pwd=self._db_password, db=self._database) DockerUtils.run_cmd(cmd=cmd_create, container=DockerContainer.LOCAL) # Restore cmd_restore = "mysql -h {server} -u {user} -p{pwd} {db} < {path}/backup.sql".format( server=self._db_server, user=self._db_user, pwd=self._db_password, db=self._database, path=Path(backup_dir)) DockerUtils.run_cmd(cmd=cmd_restore, container=DockerContainer.LOCAL)
def fix_latest_link(backup_path: str, latest_dir: str, container: DockerContainer) -> bool: cmd = "ls -la {}".format(backup_path) try: result = DockerUtils.run_cmd(cmd=cmd, container=container) result_list = result.split(b'\n') regexp = " [0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}-[0-9]{2}-[0-9]{2}" valid_backups = [] for entry in result_list: regres = re.search(regexp, str(entry)) if regres: valid_backups.append(regres.group(0)[1:]) if valid_backups: valid_backups.sort() latest_backup = os.path.join(backup_path, valid_backups[-1]) cmd_link = "rm -rf {latest} && ln -s {backup_dir} {latest}".format(backup_dir=latest_backup, latest=latest_dir) DockerUtils.run_cmd(cmd=cmd_link, container=DockerContainer.BACKUP) return True except: return False
def __deactivate_maintenance(self): DockerUtils.run_cmd(cmd="docker start keycloak_keycloak", container=DockerContainer.LOCAL)
def get_folder_size_human(path, container: DockerContainer): """disk usage in human readable format (e.g. '2,1GB')""" cmd_folder_size = "du -sh {}".format(path) result = DockerUtils.run_cmd(cmd=cmd_folder_size, container=container) return result.split()[0].decode('utf-8')
def get_folder_size_in_bytes(path, container: DockerContainer): """disk usage in bytes""" cmd_folder_size = "du -s --bytes {}".format(path) result = DockerUtils.run_cmd(cmd=cmd_folder_size, container=container) return result.split()[0].decode('utf-8')
def __deactivate_maintenance(self): DockerUtils.run_cmd(cmd="docker start wiki_app", container=DockerContainer.BACKUP)
def __restore_data(self, backup_dir: str): cmd = "rsync -az --delete {}/data/ {}".format(Path(backup_dir), self._data_dir) DockerUtils.run_cmd(cmd=cmd, container=DockerContainer.BACKUP)