import json import socket import subprocess REMOTES = {"bee1", "bee2", "bee3"} CUR_PROJECT = "default" BAK_PROJECT = "backs" STORAGE = "default" def run_and_load(cmd_list): std = subprocess.run( cmd_list, capture_output=True, ).stdout.decode() return json.loads(std) def backup_custom_volumes(nodes): vols = run_and_load( [ "lxc", "storage", "volume", "list", STORAGE, "-f", "json", "--project", CUR_PROJECT, ] ) vols_names = [ x["name"] for x in vols if x["type"] == "custom" and not "/" in x["name"] ] for name in vols_names: print(f"Snapshot volume {name}") std = subprocess.run( [ "lxc", "storage", "volume", "snapshot", STORAGE, name, "--project", CUR_PROJECT, ], ) for node in nodes: print(f"export volume {name} on {node}") std = subprocess.run( [ "lxc", "storage", "volume", "cp", f"{STORAGE}/{name}", f"{node}:{STORAGE}/{name}", "--target-project", BAK_PROJECT, "--refresh", ], ) def backup_containers(nodes): containers = run_and_load( [ "lxc", "list", "-f", "json", "--project", CUR_PROJECT, ] ) cont_names = [x["name"] for x in containers] for name in cont_names: print(f"Snapshot {name}") std = subprocess.run( [ "lxc", "snapshot", name, "--project", CUR_PROJECT, ], ) for node in nodes: print(f"export {name} on {node}") std = subprocess.run( [ "lxc", "cp", name, f"{node}:{name}", "--target-project", BAK_PROJECT, "--stateless", "--refresh", ], ) if __name__ == "__main__": curr_host = socket.gethostname() other_nodes = REMOTES.difference([curr_host]) backup_containers(other_nodes)