docker-volume-backup/backup-docker-to-local.py

88 lines
3.7 KiB
Python
Raw Normal View History

2022-01-23 13:01:49 +01:00
#!/bin/python
# Backups volumes of running containers
2023-12-25 20:31:56 +01:00
2022-03-29 19:20:35 +02:00
import subprocess
import os
2022-04-04 11:22:26 +02:00
import re
2022-03-29 19:20:35 +02:00
import pathlib
import pandas
2022-01-23 13:01:49 +01:00
from datetime import datetime
2023-12-25 20:31:56 +01:00
class BackupException(Exception):
"""Generic exception for backup errors."""
pass
2022-03-28 16:37:59 +02:00
2023-12-25 20:31:56 +01:00
def execute_shell_command(command):
"""Execute a shell command and return its output."""
2022-03-28 16:37:59 +02:00
print(command)
process = subprocess.Popen([command], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
out, err = process.communicate()
2023-12-25 20:31:56 +01:00
if process.returncode != 0:
raise BackupException(f"Error in command: {command}\nOutput: {out}\nError: {err}\nExit code: {process.returncode}")
return [line.decode("utf-8") for line in out.splitlines()]
2022-01-23 14:04:38 +01:00
2023-12-25 20:31:56 +01:00
def get_machine_id():
"""Get the machine identifier."""
return execute_shell_command("sha256sum /etc/machine-id")[0][0:64]
2023-12-25 20:31:56 +01:00
def create_backup_directories(base_dir, machine_id, repository_name, backup_time):
"""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
2022-03-28 16:37:59 +02:00
2023-12-25 20:31:56 +01:00
def get_database_name(container):
"""Extract the database name from the container name."""
return re.split("(_|-)(database|db)", container)[0]
2022-01-23 13:01:49 +01:00
2023-12-25 20:31:56 +01:00
def backup_database(container, databases, version_dir):
"""Backup database if applicable."""
database_name = get_database_name(container)
database_entry = databases.loc[databases['database'] == database_name].iloc[0]
mysqldump_destination_dir = os.path.join(version_dir, "sql")
pathlib.Path(mysqldump_destination_dir).mkdir(parents=True, exist_ok=True)
mysqldump_destination_file = os.path.join(mysqldump_destination_dir, "backup.sql")
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}"
execute_shell_command(database_backup_command)
2022-03-28 16:37:59 +02:00
2023-12-25 20:31:56 +01:00
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)
source_dir = f"/var/lib/docker/volumes/{volume_name}/_data/"
rsync_command = f"rsync -abP --delete --delete-excluded {source_dir} {files_rsync_destination_path}"
execute_shell_command(rsync_command)
2022-03-28 16:37:59 +02:00
2023-12-25 20:31:56 +01:00
def main():
print('Start backup routine...')
dirname = os.path.dirname(__file__)
repository_name = os.path.basename(dirname)
machine_id = get_machine_id()
backups_dir = '/Backups/'
backup_time = datetime.now().strftime("%Y%m%d%H%M%S")
version_dir = create_backup_directories(backups_dir, machine_id, repository_name, backup_time)
2022-01-23 13:01:49 +01:00
2023-12-25 20:31:56 +01:00
print('Start volume backups...')
databases = pandas.read_csv(os.path.join(dirname, "databases.csv"), sep=";")
volume_names = execute_shell_command("docker volume ls --format '{{.Name}}'")
for volume_name in volume_names:
print(f'Start backup routine for volume: {volume_name}')
containers = execute_shell_command(f"docker ps --filter volume=\"{volume_name}\" --format '{{.Names}}'")
if not containers:
print('Skipped due to no running containers using this volume.')
continue
2022-03-29 19:20:35 +02:00
2023-12-25 20:31:56 +01:00
for container in containers:
if container != 'akaunting':
backup_database(container, databases, version_dir)
backup_volume(volume_name, version_dir)
2023-12-25 20:31:56 +01:00
print('Finished volume backups.')
print('Restart docker service...')
execute_shell_command("systemctl restart docker")
print('Finished backup routine.')
2023-12-25 20:31:56 +01:00
if __name__ == "__main__":
main()