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

110 lines
4.5 KiB
Python
Raw Normal View History

2022-01-23 13:01:49 +01:00
#!/bin/python
# Backups volumes of running containers
#
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
2022-03-28 16:37:59 +02:00
2022-01-23 13:01:49 +01:00
def bash(command):
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()
stdout = out.splitlines()
output = []
2022-01-23 14:04:38 +01:00
for line in stdout:
output.append(line.decode("utf-8"))
2022-01-23 15:52:31 +01:00
if process.wait() > bool(0):
2022-03-28 16:37:59 +02:00
print(command, out, err)
2022-12-23 15:48:10 +01:00
raise Exception("Exitcode is greater then 0")
2022-01-23 14:04:38 +01:00
return output
2022-03-28 16:37:59 +02:00
2022-01-23 14:04:38 +01:00
def print_bash(command):
2022-03-28 16:37:59 +02:00
output = bash(command)
2022-01-23 14:04:38 +01:00
print(list_to_string(output))
return output
2022-01-23 13:01:49 +01:00
2022-03-28 16:37:59 +02:00
2022-01-23 13:01:49 +01:00
def list_to_string(list):
2022-03-28 16:37:59 +02:00
return str(' '.join(list))
2022-01-23 13:01:49 +01:00
print('start backup routine...')
2022-03-29 19:20:35 +02:00
2022-03-28 16:37:59 +02:00
dirname = os.path.dirname(__file__)
repository_name = os.path.basename(dirname)
2022-03-29 19:20:35 +02:00
# 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 + "/"
2022-03-29 19:20:35 +02:00
# 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 + "/"
2022-03-29 19:20:35 +02:00
# Create folder to store version in
pathlib.Path(version_dir).mkdir(parents=True, exist_ok=True)
2022-03-29 19:20:35 +02:00
print('start volume backups...')
2022-03-28 16:37:59 +02:00
print('load connection data...')
databases = pandas.read_csv(dirname + "/databases.csv", sep=";")
volume_names = bash("docker volume ls --format '{{.Name}}'")
2022-01-23 13:01:49 +01:00
for volume_name in volume_names:
2022-03-28 16:37:59 +02:00
print('start backup routine for volume: ' + volume_name)
2022-03-28 17:08:54 +02:00
containers = bash("docker ps --filter volume=\"" + volume_name + "\" --format '{{.Names}}'")
2022-01-23 13:01:49 +01:00
if len(containers) == 0:
2022-03-28 16:37:59 +02:00
print('skipped due to no running containers using this volume.')
2022-01-23 13:01:49 +01:00
else:
2022-03-28 16:37:59 +02:00
container = containers[0]
2022-03-29 19:20:35 +02:00
# Folder to which the volumes are copied
volume_destination_dir = version_dir + volume_name
2022-04-04 11:22:26 +02:00
# Database name
2022-04-04 11:34:42 +02:00
database_name = re.split("(_|-)(database|db)", container)[0]
2022-04-04 11:22:26 +02:00
# Entries with database login data concerning this container
databases_entries = databases.loc[databases['database'] == database_name]
2022-04-04 11:51:22 +02:00
# Exception for akaunting due to fast implementation
if len(databases_entries) == 1 and container != 'akaunting':
print("Backup database...")
2022-03-29 19:20:35 +02:00
mysqldump_destination_dir = volume_destination_dir + "/sql"
2022-03-28 17:46:32 +02:00
mysqldump_destination_file = mysqldump_destination_dir + "/backup.sql"
pathlib.Path(mysqldump_destination_dir).mkdir(parents=True, exist_ok=True)
2022-03-28 16:39:37 +02:00
database_entry = databases_entries.iloc[0]
2023-06-25 22:15:00 +02:00
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)
2022-08-31 20:48:40 +02:00
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 + "' "
2022-03-28 17:08:54 +02:00
else:
2022-03-29 08:45:00 +02:00
print("No previous version exists in path "+ last_version_files_dir + ".")
2022-03-28 17:08:54 +02:00
link_dest_parameter=""
2022-08-31 20:48:40 +02:00
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
print_bash(rsync_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))
2022-01-23 14:04:38 +01:00
print("end backup routine for volume:" + volume_name)
2022-01-23 13:01:49 +01:00
print('finished volume backups.')
print('restart docker service...')
2022-01-23 14:04:38 +01:00
print_bash("systemctl restart docker")
2022-01-23 13:01:49 +01:00
print('finished backup routine.')