Refactored

This commit is contained in:
Kevin Veen-Birkenbach 2022-01-23 20:55:09 +01:00
parent 75c171ce09
commit 1ff8fd4a0a

View File

@ -47,31 +47,31 @@ for volume_name in volume_names:
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
files_destination_path=destination_path + "/files"
sql_destination_path=destination_path + "/sql"
log_path=backup_repository_folder + "log.txt"
backup_dir_path=backup_repository_folder + "diffs/"+ backup_time + "/" + volume_name
files_backup_dir_path=backup_dir_path + "/files"
sql_backup_dir_path=backup_dir_path + "/sql"
print("Create backup folder structure for " + volume_name);
pathlib.Path(sql_destination_path).mkdir(parents=True, exist_ok=True)
pathlib.Path(files_backup_dir_path).mkdir(parents=True, exist_ok=True)
pathlib.Path(files_destination_path).mkdir(parents=True, exist_ok=True)
pathlib.Path(sql_backup_dir_path).mkdir(parents=True, exist_ok=True)
databases_entries=databases.loc[databases['container'] == container];
if len(databases_entries) == 1:
database_entry=databases_entries.iloc[0];
print("Backup database...")
sql_destination_path=destination_path + "/sql"
sql_backup_dir_path=backup_dir_path + "/sql"
sql_destination_dir_file_path=sql_destination_path+"/backup.sql"
pathlib.Path(sql_destination_path).mkdir(parents=True, exist_ok=True)
pathlib.Path(sql_backup_dir_path).mkdir(parents=True, exist_ok=True)
database_entry=databases_entries.iloc[0];
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)
print_bash("cp -v " + sql_destination_dir_file_path + " " + sql_backup_dir_path)
else:
print("stop containers:");
print("Backup files...")
files_destination_path=destination_path + "/files"
files_backup_dir_path=backup_dir_path + "/files"
pathlib.Path(files_backup_dir_path).mkdir(parents=True, exist_ok=True)
pathlib.Path(files_destination_path).mkdir(parents=True, exist_ok=True)
print("stop containers...");
print_bash("docker stop " + list_to_string(containers))
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=" + files_backup_dir_path +" '"+ source_path +"/' " + files_destination_path +"\"")
print("start containers:")
print("start containers...")
print_bash("docker start " + list_to_string(containers))
print("end backup routine for volume:" + volume_name)
print('finished volume backups.')