From b83e481d017cd74061d52efd0a2f0213ba73721b Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Wed, 13 Dec 2023 08:55:02 +0100 Subject: [PATCH] implemented exception handling for rsync 24 --- README.md | 1 + backup-docker-to-local.py | 28 ++++++++++++++++++++-------- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 2322b94..13a53fe 100644 --- a/README.md +++ b/README.md @@ -64,3 +64,4 @@ This project is licensed under the GNU Affero General Public License v3.0. The f - https://zwischenzugs.com/2016/08/29/bash-to-python-converter/ - https://en.wikipedia.org/wiki/Incremental_backup#Incremental - https://unix.stackexchange.com/questions/567837/linux-backup-utility-for-incremental-backups +- https://chat.openai.com/share/6d10f143-3f7c-4feb-8ae9-5644c3433a65 \ No newline at end of file diff --git a/backup-docker-to-local.py b/backup-docker-to-local.py index a986f9b..d93e4af 100644 --- a/backup-docker-to-local.py +++ b/backup-docker-to-local.py @@ -8,20 +8,29 @@ import pathlib import pandas from datetime import datetime +class RsyncCode24Exception(Exception): + """Exception for rsync error code 24.""" + """rsync warning: some files vanished before they could be transferred""" + pass def bash(command): print(command) process = subprocess.Popen([command], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) out, err = process.communicate() stdout = out.splitlines() - output = [] - for line in stdout: - output.append(line.decode("utf-8")) - if process.wait() > bool(0): - print(command, out, err) - raise Exception("Exitcode is greater then 0") - return output + stderr = err.decode("utf-8") + output = [line.decode("utf-8") for line in stdout] + exitcode = process.wait() + if exitcode != 0: + print(f"Error in command: {command}\nOutput: {out}\nError: {err}\nExit code: {exitcode}") + + if "rsync" in command and exitcode == 24: + raise RsyncCode24Exception(f"rsync error code 24 encountered: {stderr}") + + raise Exception("Exit code is greater than 0") + + return output def print_bash(command): output = bash(command) @@ -95,7 +104,10 @@ for volume_name in volume_names: link_dest_parameter="" source_dir = "/var/lib/docker/volumes/" + volume_name + "/_data/" rsync_command = "rsync -abP --delete --delete-excluded " + link_dest_parameter + source_dir + " " + files_rsync_destination_path - print_bash(rsync_command) + try: + print_bash(rsync_command) + except RsyncCode24Exception: + print("Ignoring rsync error code 24, proceeding with the next command.") print("stop containers...") print("Backup data after container is stopped...") print_bash("docker stop " + list_to_string(containers))