mirror of
https://github.com/kevinveenbirkenbach/docker-volume-backup.git
synced 2024-11-01 07:43:10 +01:00
refactored. untested.
This commit is contained in:
parent
b83e481d01
commit
a1c33c1747
@ -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."""
|
||||||
|
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)
|
||||||
|
|
||||||
def print_bash(command):
|
def backup_volume(volume_name, version_dir):
|
||||||
output = bash(command)
|
"""Backup files of a volume."""
|
||||||
print(list_to_string(output))
|
files_rsync_destination_path = os.path.join(version_dir, volume_name, "files")
|
||||||
return output
|
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}"
|
||||||
def list_to_string(list):
|
execute_shell_command(rsync_command)
|
||||||
return str(' '.join(list))
|
|
||||||
|
|
||||||
|
|
||||||
print('start backup routine...')
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
print('Start backup routine...')
|
||||||
dirname = os.path.dirname(__file__)
|
dirname = os.path.dirname(__file__)
|
||||||
repository_name = os.path.basename(dirname)
|
repository_name = os.path.basename(dirname)
|
||||||
# identifier of this backups
|
machine_id = get_machine_id()
|
||||||
machine_id = bash("sha256sum /etc/machine-id")[0][0:64]
|
|
||||||
# Folder in which all Backups are stored
|
|
||||||
backups_dir = '/Backups/'
|
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")
|
backup_time = datetime.now().strftime("%Y%m%d%H%M%S")
|
||||||
# Folder containing the current version
|
version_dir = create_backup_directories(backups_dir, machine_id, repository_name, backup_time)
|
||||||
version_dir = versions_dir + backup_time + "/"
|
|
||||||
|
|
||||||
# Create folder to store version in
|
print('Start volume backups...')
|
||||||
pathlib.Path(version_dir).mkdir(parents=True, exist_ok=True)
|
databases = pandas.read_csv(os.path.join(dirname, "databases.csv"), sep=";")
|
||||||
|
volume_names = execute_shell_command("docker volume ls --format '{{.Name}}'")
|
||||||
|
|
||||||
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:
|
for volume_name in volume_names:
|
||||||
print('start backup routine for volume: ' + volume_name)
|
print(f'Start backup routine for volume: {volume_name}')
|
||||||
containers = bash("docker ps --filter volume=\"" + volume_name + "\" --format '{{.Names}}'")
|
containers = execute_shell_command(f"docker ps --filter volume=\"{volume_name}\" --format '{{.Names}}'")
|
||||||
if len(containers) == 0:
|
if not containers:
|
||||||
print('skipped due to no running containers using this volume.')
|
print('Skipped due to no running containers using this volume.')
|
||||||
else:
|
continue
|
||||||
container = containers[0]
|
|
||||||
# Folder to which the volumes are copied
|
for container in containers:
|
||||||
volume_destination_dir = version_dir + volume_name
|
if container != 'akaunting':
|
||||||
# Database name
|
backup_database(container, databases, version_dir)
|
||||||
database_name = re.split("(_|-)(database|db)", container)[0]
|
backup_volume(volume_name, version_dir)
|
||||||
# Entries with database login data concerning this container
|
|
||||||
databases_entries = databases.loc[databases['database'] == database_name]
|
print('Finished volume backups.')
|
||||||
# Exception for akaunting due to fast implementation
|
print('Restart docker service...')
|
||||||
if len(databases_entries) == 1 and container != 'akaunting':
|
execute_shell_command("systemctl restart docker")
|
||||||
print("Backup database...")
|
print('Finished backup routine.')
|
||||||
mysqldump_destination_dir = volume_destination_dir + "/sql"
|
|
||||||
mysqldump_destination_file = mysqldump_destination_dir + "/backup.sql"
|
if __name__ == "__main__":
|
||||||
pathlib.Path(mysqldump_destination_dir).mkdir(parents=True, exist_ok=True)
|
main()
|
||||||
database_entry = databases_entries.iloc[0]
|
|
||||||
database_backup_command = "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)
|
|
||||||
print("Backup files...")
|
|
||||||
files_rsync_destination_path = volume_destination_dir + "/files"
|
|
||||||
pathlib.Path(files_rsync_destination_path).mkdir(parents=True, exist_ok=True)
|
|
||||||
versions = os.listdir(versions_dir)
|
|
||||||
versions.sort(reverse=True)
|
|
||||||
if len(versions) > 1:
|
|
||||||
last_version = versions[1]
|
|
||||||
last_version_files_dir = versions_dir + last_version + "/" + volume_name + "/files"
|
|
||||||
if os.path.isdir(last_version_files_dir):
|
|
||||||
link_dest_parameter="--link-dest='" + last_version_files_dir + "' "
|
|
||||||
else:
|
|
||||||
print("No previous version exists in path "+ last_version_files_dir + ".")
|
|
||||||
link_dest_parameter=""
|
|
||||||
else:
|
|
||||||
print("No previous version exists in path "+ last_version_files_dir + ".")
|
|
||||||
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
|
|
||||||
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))
|
|
||||||
print_bash(rsync_command)
|
|
||||||
print("start containers...")
|
|
||||||
print_bash("docker start " + list_to_string(containers))
|
|
||||||
print("end backup routine for volume:" + volume_name)
|
|
||||||
print('finished volume backups.')
|
|
||||||
print('restart docker service...')
|
|
||||||
print_bash("systemctl restart docker")
|
|
||||||
print('finished backup routine.')
|
|
||||||
|
Loading…
Reference in New Issue
Block a user