2022-01-23 13:01:49 +01:00
|
|
|
#!/bin/python
|
|
|
|
# Backups volumes of running containers
|
|
|
|
#
|
2022-03-28 16:37:59 +02:00
|
|
|
import subprocess, os, pathlib, 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-01-23 15:52:31 +01:00
|
|
|
raise Exception("Error 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...')
|
|
|
|
print('start volume backups...')
|
2022-03-28 16:37:59 +02:00
|
|
|
backup_time = datetime.now().strftime("%Y%m%d%H%M%S")
|
|
|
|
backups_folder = '/Backups/'
|
|
|
|
dirname = os.path.dirname(__file__)
|
|
|
|
repository_name = os.path.basename(dirname)
|
|
|
|
print('load connection data...')
|
|
|
|
databases = pandas.read_csv(dirname + "/databases.csv", sep=";")
|
|
|
|
machine_id = bash("sha256sum /etc/machine-id")[0][0:64]
|
|
|
|
backup_repository_folder = backups_folder + machine_id + "/" + repository_name + "/"
|
|
|
|
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)
|
|
|
|
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]
|
|
|
|
source_path = "/var/lib/docker/volumes/" + volume_name + "/_data"
|
|
|
|
log_path = backup_repository_folder + "log.txt"
|
|
|
|
destination_path = backup_repository_folder + "versions/"+ backup_time + "/" + volume_name
|
|
|
|
versions_dir_path = backup_repository_folder + "versions/"
|
|
|
|
databases_entries = databases.loc[databases['container'] == container]
|
|
|
|
backup_versions = os.listdir(versions_dir_path)
|
|
|
|
backup_versions.sort(reverse=True)
|
|
|
|
last_version = backup_versions[0]
|
|
|
|
last_version_dir_path = versions_dir_path + last_version + "/" + volume_name
|
|
|
|
current_version_dir_path = versions_dir_path + backup_time + "/" + volume_name
|
2022-03-17 20:29:27 +01:00
|
|
|
if len(databases_entries) == 1:
|
|
|
|
print("Backup database...")
|
2022-03-28 16:37:59 +02:00
|
|
|
sql_cp_source_path = destination_path + "/sql"
|
|
|
|
sql_cp_destination_path = current_version_dir_path + "/sql"
|
|
|
|
sql_destination_dir_file_path = sql_cp_destination_path + "/backup.sql"
|
|
|
|
pathlib.Path(sql_cp_destination_path).mkdir(parents=True, exist_ok=True)
|
2022-03-28 16:39:37 +02:00
|
|
|
database_entry = databases_entries.iloc[0]
|
2022-03-17 20:29:27 +01:00
|
|
|
database_backup_command="docker exec "+ database_entry["container"] + " /usr/bin/mysqldump -u "+ database_entry["username"] + " -p"+ database_entry["password"] + " "+ database_entry["database"] + " > " + sql_destination_dir_file_path
|
|
|
|
print_bash(database_backup_command)
|
|
|
|
else:
|
|
|
|
print("Backup files...")
|
2022-03-28 16:39:37 +02:00
|
|
|
files_rsync_destination_path = destination_path + "/files"
|
2022-03-28 16:37:59 +02:00
|
|
|
pathlib.Path(files_rsync_destination_path).mkdir(parents=True, exist_ok=True)
|
2022-03-17 21:10:43 +01:00
|
|
|
print("Backup data during container is running...")
|
2022-03-28 16:39:37 +02:00
|
|
|
rsync_command = "rsync -abP --delete --delete-excluded --log-file=" + log_path +" --backup-dir=" + files_version_dir_path +" '"+ source_path +"/' " + files_rsync_destination_path
|
2022-03-17 21:10:43 +01:00
|
|
|
print_bash(rsync_command)
|
2022-03-28 16:37:59 +02:00
|
|
|
print("stop containers...")
|
2022-03-17 21:10:43 +01:00
|
|
|
print("Backup data after container is stopped...")
|
2022-03-17 20:29:27 +01:00
|
|
|
print_bash("docker stop " + list_to_string(containers))
|
2022-03-17 21:10:43 +01:00
|
|
|
print_bash(rsync_command)
|
2022-03-17 20:29:27 +01:00
|
|
|
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.')
|