Finished python version

This commit is contained in:
Kevin Veen-Birkenbach 2022-01-23 15:52:31 +01:00
parent 7bf07d7fb5
commit 454826b7e9

View File

@ -1,17 +1,20 @@
#!/bin/python #!/bin/python
# Backups volumes of running containers # Backups volumes of running containers
# #
import subprocess, os, sys import subprocess, os, sys, pathlib
from datetime import datetime from datetime import datetime
from pprint import pprint
def bash(command): def bash(command):
out, err=subprocess.Popen([command],stdout=subprocess.PIPE, shell=True).communicate() process=subprocess.Popen([command],stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
stdout=out.read().splitlines() out, err=process.communicate()
stdout=out.splitlines()
output=[] output=[]
for line in stdout: for line in stdout:
output.append(line.decode("utf-8")) output.append(line.decode("utf-8"))
if err > 0: if process.wait() > bool(0):
raise Exception("Error is greater then 0", output, command) print(command,out,err);
raise Exception("Error is greater then 0")
return output return output
def print_bash(command): def print_bash(command):
@ -33,30 +36,31 @@ 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('start backup routine for volume: ' + volume_name);
containers=bash("docker ps --filter volume=\""+ volume_name +"\" --format '{{.Names}}'") containers=bash("docker ps --filter volume=\""+ volume_name +"\" --format '{{.Names}}'")
container=containers[0]
if len(containers) == 0: if len(containers) == 0:
print('skipped due to no running containers using this volume.'); print('skipped due to no running containers using this volume.');
else: else:
container=containers[0]
print("stop containers:"); print("stop containers:");
print_bash("docker stop " + list_to_string(containers)) print_bash("docker stop " + list_to_string(containers))
for source_path in bash("docker inspect --format \"{{ range .Mounts }}{{ if eq .Type \\\"volume\\\"}}{{ if eq .Name \\\"" + volume_name +"\\\"}}{{ println .Destination }}{{ end }}{{ end }}{{ end }}\" \""+ container +"\""): source_path_command="docker inspect --format \"{{ range .Mounts }}{{ if eq .Type \\\"volume\\\"}}{{ if eq .Name \\\"" + volume_name +"\\\"}}{{ println .Destination }}{{ end }}{{ end }}{{ end }}\" \""+ container +"\""
source_path_command_result_filtered=list(filter(None, bash(source_path_command)))
for source_path in source_path_command_result_filtered:
destination_path=backup_repository_folder+"latest/"+ volume_name destination_path=backup_repository_folder+"latest/"+ volume_name
raw_destination_path="destination_path/raw" raw_destination_path=destination_path + "/raw"
prepared_destination_path=destination_path + "/prepared" prepared_destination_path=destination_path + "/prepared"
log_path=backup_repository_folder + "log.txt" log_path=backup_repository_folder + "log.txt"
backup_dir_path=backup_repository_folder + "diffs/"+ backup_time + "/" + volume_name backup_dir_path=backup_repository_folder + "diffs/"+ backup_time + "/" + volume_name
raw_backup_dir_path=backup_dir_path + "/raw" raw_backup_dir_path=backup_dir_path + "/raw"
prepared_backup_dir_path=backup_dir_path + "/prepared" prepared_backup_dir_path=backup_dir_path + "/prepared"
if path.exists(destination_path): if os.path.exists(destination_path):
print("backup volume: " + volume_name); print("backup volume: " + volume_name);
else: else:
print("first backup volume" + volume_name); print("first backup volume: " + volume_name);
os.mkdir(raw_destination_path) pathlib.Path(raw_destination_path).mkdir(parents=True, exist_ok=True)
os.mkdir(raw_backup_dir_path) pathlib.Path(raw_backup_dir_path).mkdir(parents=True, exist_ok=True)
os.mkdir(prepared_destination_path) pathlib.Path(prepared_destination_path).mkdir(parents=True, exist_ok=True)
os.mkdir(prepared_backup_dir_path) pathlib.Path(prepared_backup_dir_path).mkdir(parents=True, exist_ok=True)
print_bash("rsync -abP --delete --delete-excluded --log-file=" + log_path +" --backup-dir=" + raw_backup_dir_path +" '"+ source_path +"/' " + raw_destination_path) print_bash("docker run --rm --volumes-from " + container + " -v "+backups_folder+":"+ backups_folder +" \"kevinveenbirkenbach/alpine-rsync\" sh -c \"rsync -abP --delete --delete-excluded --log-file=" + log_path +" --backup-dir=" + raw_backup_dir_path +" '"+ source_path +"/' " + raw_destination_path +"\"")
print_bash("docker run --rm --volumes-from " + container + " -v "+backups_folder+":"+ backups_folder +" \"kevinveenbirkenbach/alpine-rsync\" sh -c ")
print("start containers:") print("start containers:")
print_bash("docker start " + list_to_string(containers)) print_bash("docker start " + list_to_string(containers))
print("end backup routine for volume:" + volume_name) print("end backup routine for volume:" + volume_name)