mirror of
https://github.com/kevinveenbirkenbach/docker-volume-backup.git
synced 2024-11-25 01:31:03 +01:00
Draft translating to python
This commit is contained in:
parent
2eeb4961d0
commit
7bf07d7fb5
@ -1,68 +1,66 @@
|
|||||||
#!/bin/python
|
#!/bin/python
|
||||||
# Backups volumes of running containers
|
# Backups volumes of running containers
|
||||||
#
|
#
|
||||||
import subprocess
|
import subprocess, os, sys
|
||||||
import os, sys
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
def bash(command):
|
def bash(command):
|
||||||
stdout=subprocess.Popen([command],stdout=subprocess.PIPE, shell=True).stdout.read()
|
out, err=subprocess.Popen([command],stdout=subprocess.PIPE, shell=True).communicate()
|
||||||
if isinstance(stdout, list):
|
stdout=out.read().splitlines()
|
||||||
return subprocess.Popen([command],stdout=subprocess.PIPE, shell=True).stdout.read()
|
output=[]
|
||||||
else:
|
for line in stdout:
|
||||||
return subprocess.Popen([command],stdout=subprocess.PIPE, shell=True).stdout.read()
|
output.append(line.decode("utf-8"))
|
||||||
|
if err > 0:
|
||||||
|
raise Exception("Error is greater then 0", output, command)
|
||||||
|
return output
|
||||||
|
|
||||||
|
def print_bash(command):
|
||||||
|
output=bash(command)
|
||||||
|
print(list_to_string(output))
|
||||||
|
return output
|
||||||
|
|
||||||
def list_to_string(list):
|
def list_to_string(list):
|
||||||
list_string=""
|
return str(' '.join(list));
|
||||||
for element in list:
|
|
||||||
list_string=list_string + " " + (element.decode("utf-8"))
|
|
||||||
return list_string;
|
|
||||||
|
|
||||||
print('start backup routine...')
|
print('start backup routine...')
|
||||||
print('start volume backups...')
|
print('start volume backups...')
|
||||||
backup_time=datetime.now().strftime("%Y%m%d%H%M%S")
|
backup_time=datetime.now().strftime("%Y%m%d%H%M%S")
|
||||||
backups_folder='/Backups/'
|
backups_folder='/Backups/'
|
||||||
repository_name=os.path.basename(os.path.dirname(__file__))
|
repository_name=os.path.basename(os.path.dirname(__file__))
|
||||||
machine_id=bash("sha256sum /etc/machine-id").decode("utf-8")[0:64]
|
machine_id=bash("sha256sum /etc/machine-id")[0][0:64]
|
||||||
backup_repository_folder=backups_folder + machine_id + "/" + repository_name + "/"
|
backup_repository_folder=backups_folder + machine_id + "/" + repository_name + "/"
|
||||||
volume_names=bash("docker volume ls --format '{{.Name}}'").splitlines()
|
volume_names=bash("docker volume ls --format '{{.Name}}'")
|
||||||
for volume_name in volume_names:
|
for volume_name in volume_names:
|
||||||
volume_name=volume_name.decode("utf-8")
|
|
||||||
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}}'").splitlines()
|
containers=bash("docker ps --filter volume=\""+ volume_name +"\" --format '{{.Names}}'")
|
||||||
container=containers[0].decode("utf-8")
|
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:
|
||||||
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 $(docker inspect --format "{{ range .Mounts }}{{ if eq .Type \"volume\"}}{{ if eq .Name \"$volume_name\"}}{{ println .Destination }}{{ end }}{{ end }}{{ end }}" "$container");
|
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 +"\""):
|
||||||
# do
|
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" || exit 1
|
if path.exists(destination_path):
|
||||||
# if [ -d "$destination_path" ]
|
print("backup volume: " + volume_name);
|
||||||
# then
|
else:
|
||||||
# print('backup volume: $volume_name');
|
print("first backup volume" + volume_name);
|
||||||
# else
|
os.mkdir(raw_destination_path)
|
||||||
# print('first backup volume: $volume_name');
|
os.mkdir(raw_backup_dir_path)
|
||||||
# mkdir -vp "$raw_destination_path" &&
|
os.mkdir(prepared_destination_path)
|
||||||
# mkdir -vp "$raw_backup_dir_path" &&
|
os.mkdir(prepared_backup_dir_path)
|
||||||
# mkdir -vp "$prepared_destination_path" &&
|
print_bash("rsync -abP --delete --delete-excluded --log-file=" + log_path +" --backup-dir=" + raw_backup_dir_path +" '"+ source_path +"/' " + raw_destination_path)
|
||||||
# mkdir -vp "$prepared_backup_dir_path" || exit 1
|
print_bash("docker run --rm --volumes-from " + container + " -v "+backups_folder+":"+ backups_folder +" \"kevinveenbirkenbach/alpine-rsync\" sh -c ")
|
||||||
# fi
|
print("start containers:")
|
||||||
# docker run --rm --volumes-from "$container" -v "$backups_folder:$backups_folder" "kevinveenbirkenbach/alpine-rsync" sh -c "
|
print_bash("docker start " + list_to_string(containers))
|
||||||
# rsync -abP --delete --delete-excluded --log-file=$log_path --backup-dir=$raw_backup_dir_path '$source_path/' $raw_destination_path" &&
|
print("end backup routine for volume:" + volume_name)
|
||||||
print(bash("docker start " + list_to_string(containers)))
|
|
||||||
# done
|
|
||||||
# fi
|
|
||||||
# print('end backup routine for volume: $volume_name');
|
|
||||||
# done
|
|
||||||
print('finished volume backups.')
|
print('finished volume backups.')
|
||||||
print('restart docker service...')
|
print('restart docker service...')
|
||||||
# systemctl restart docker || exit 1
|
print_bash("systemctl restart docker")
|
||||||
print('finished backup routine.')
|
print('finished backup routine.')
|
||||||
|
Loading…
Reference in New Issue
Block a user