refactored. untested.

This commit is contained in:
Kevin Veen-Birkenbach 2023-12-25 20:31:56 +01:00
parent b83e481d01
commit a1c33c1747

View File

@ -1,6 +1,6 @@
#!/bin/python #!/bin/python
# Backups volumes of running containers # Backups volumes of running containers
#
import subprocess import subprocess
import os import os
import re import re
@ -8,114 +8,80 @@ import pathlib
import pandas import pandas
from datetime import datetime from datetime import datetime
class RsyncCode24Exception(Exception): class BackupException(Exception):
"""Exception for rsync error code 24.""" """Generic exception for backup errors."""
"""rsync warning: some files vanished before they could be transferred"""
pass pass
def bash(command): def execute_shell_command(command):
"""Execute a shell command and return its output."""
print(command) print(command)
process = subprocess.Popen([command], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) process = subprocess.Popen([command], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
out, err = process.communicate() out, err = process.communicate()
stdout = out.splitlines() if process.returncode != 0:
stderr = err.decode("utf-8") raise BackupException(f"Error in command: {command}\nOutput: {out}\nError: {err}\nExit code: {process.returncode}")
output = [line.decode("utf-8") for line in stdout] return [line.decode("utf-8") for line in out.splitlines()]
exitcode = process.wait() def get_machine_id():
if exitcode != 0: """Get the machine identifier."""
print(f"Error in command: {command}\nOutput: {out}\nError: {err}\nExit code: {exitcode}") return execute_shell_command("sha256sum /etc/machine-id")[0][0:64]
if "rsync" in command and exitcode == 24: def create_backup_directories(base_dir, machine_id, repository_name, backup_time):
raise RsyncCode24Exception(f"rsync error code 24 encountered: {stderr}") """Create necessary directories for backup."""
version_dir = os.path.join(base_dir, machine_id, repository_name, backup_time)
pathlib.Path(version_dir).mkdir(parents=True, exist_ok=True)
return version_dir
raise Exception("Exit code is greater than 0") def get_database_name(container):
"""Extract the database name from the container name."""
return re.split("(_|-)(database|db)", container)[0]
return output def backup_database(container, databases, version_dir):
"""Backup database if applicable."""
def print_bash(command): database_name = get_database_name(container)
output = bash(command) database_entry = databases.loc[databases['database'] == database_name].iloc[0]
print(list_to_string(output)) mysqldump_destination_dir = os.path.join(version_dir, "sql")
return output
def list_to_string(list):
return str(' '.join(list))
print('start backup routine...')
dirname = os.path.dirname(__file__)
repository_name = os.path.basename(dirname)
# identifier of this backups
machine_id = bash("sha256sum /etc/machine-id")[0][0:64]
# Folder in which all Backups are stored
backups_dir = '/Backups/'
# Folder in which the versions off docker volume backups are stored
versions_dir = backups_dir + machine_id + "/" + repository_name + "/"
# Time when the backup started
backup_time = datetime.now().strftime("%Y%m%d%H%M%S")
# Folder containing the current version
version_dir = versions_dir + backup_time + "/"
# Create folder to store version in
pathlib.Path(version_dir).mkdir(parents=True, exist_ok=True)
print('start volume backups...')
print('load connection data...')
databases = pandas.read_csv(dirname + "/databases.csv", sep=";")
volume_names = bash("docker volume ls --format '{{.Name}}'")
for volume_name in volume_names:
print('start backup routine for volume: ' + volume_name)
containers = bash("docker ps --filter volume=\"" + volume_name + "\" --format '{{.Names}}'")
if len(containers) == 0:
print('skipped due to no running containers using this volume.')
else:
container = containers[0]
# Folder to which the volumes are copied
volume_destination_dir = version_dir + volume_name
# Database name
database_name = re.split("(_|-)(database|db)", container)[0]
# Entries with database login data concerning this container
databases_entries = databases.loc[databases['database'] == database_name]
# Exception for akaunting due to fast implementation
if len(databases_entries) == 1 and container != 'akaunting':
print("Backup database...")
mysqldump_destination_dir = volume_destination_dir + "/sql"
mysqldump_destination_file = mysqldump_destination_dir + "/backup.sql"
pathlib.Path(mysqldump_destination_dir).mkdir(parents=True, exist_ok=True) pathlib.Path(mysqldump_destination_dir).mkdir(parents=True, exist_ok=True)
database_entry = databases_entries.iloc[0] mysqldump_destination_file = os.path.join(mysqldump_destination_dir, "backup.sql")
database_backup_command = "docker exec " + container + " /usr/bin/mariadb-dump -u " + database_entry["username"] + " -p" + database_entry["password"] + " " + database_entry["database"] + " > " + mysqldump_destination_file database_backup_command = f"docker exec {container} /usr/bin/mariadb-dump -u {database_entry['username']} -p{database_entry['password']} {database_entry['database']} > {mysqldump_destination_file}"
print_bash(database_backup_command) execute_shell_command(database_backup_command)
print("Backup files...")
files_rsync_destination_path = volume_destination_dir + "/files" def backup_volume(volume_name, version_dir):
"""Backup files of a volume."""
files_rsync_destination_path = os.path.join(version_dir, volume_name, "files")
pathlib.Path(files_rsync_destination_path).mkdir(parents=True, exist_ok=True) pathlib.Path(files_rsync_destination_path).mkdir(parents=True, exist_ok=True)
versions = os.listdir(versions_dir) source_dir = f"/var/lib/docker/volumes/{volume_name}/_data/"
versions.sort(reverse=True) rsync_command = f"rsync -abP --delete --delete-excluded {source_dir} {files_rsync_destination_path}"
if len(versions) > 1: execute_shell_command(rsync_command)
last_version = versions[1]
last_version_files_dir = versions_dir + last_version + "/" + volume_name + "/files" def main():
if os.path.isdir(last_version_files_dir): print('Start backup routine...')
link_dest_parameter="--link-dest='" + last_version_files_dir + "' " dirname = os.path.dirname(__file__)
else: repository_name = os.path.basename(dirname)
print("No previous version exists in path "+ last_version_files_dir + ".") machine_id = get_machine_id()
link_dest_parameter="" backups_dir = '/Backups/'
else: backup_time = datetime.now().strftime("%Y%m%d%H%M%S")
print("No previous version exists in path "+ last_version_files_dir + ".") version_dir = create_backup_directories(backups_dir, machine_id, repository_name, backup_time)
link_dest_parameter=""
source_dir = "/var/lib/docker/volumes/" + volume_name + "/_data/" print('Start volume backups...')
rsync_command = "rsync -abP --delete --delete-excluded " + link_dest_parameter + source_dir + " " + files_rsync_destination_path databases = pandas.read_csv(os.path.join(dirname, "databases.csv"), sep=";")
try: volume_names = execute_shell_command("docker volume ls --format '{{.Name}}'")
print_bash(rsync_command)
except RsyncCode24Exception: for volume_name in volume_names:
print("Ignoring rsync error code 24, proceeding with the next command.") print(f'Start backup routine for volume: {volume_name}')
print("stop containers...") containers = execute_shell_command(f"docker ps --filter volume=\"{volume_name}\" --format '{{.Names}}'")
print("Backup data after container is stopped...") if not containers:
print_bash("docker stop " + list_to_string(containers)) print('Skipped due to no running containers using this volume.')
print_bash(rsync_command) continue
print("start containers...")
print_bash("docker start " + list_to_string(containers)) for container in containers:
print("end backup routine for volume:" + volume_name) if container != 'akaunting':
print('finished volume backups.') backup_database(container, databases, version_dir)
print('restart docker service...') backup_volume(volume_name, version_dir)
print_bash("systemctl restart docker")
print('finished backup routine.') print('Finished volume backups.')
print('Restart docker service...')
execute_shell_command("systemctl restart docker")
print('Finished backup routine.')
if __name__ == "__main__":
main()