mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2024-11-25 06:01:04 +01:00
Optimized storage script
This commit is contained in:
parent
ec331862ad
commit
26f9cbd49c
@ -8,39 +8,67 @@ def run_command(command):
|
|||||||
|
|
||||||
# Path on the SSD where data will be moved
|
# Path on the SSD where data will be moved
|
||||||
ssd_path = "/path/to/ssd"
|
ssd_path = "/path/to/ssd"
|
||||||
|
hdd_path = "/path/to/hdd"
|
||||||
|
|
||||||
# List all Docker volumes
|
# List all Docker volumes
|
||||||
volumes = run_command("docker volume ls -q").splitlines()
|
volumes = run_command("docker volume ls -q").splitlines()
|
||||||
|
|
||||||
for volume in volumes:
|
def stop_containers(containers):
|
||||||
# List all containers using this volume
|
"""Stop a list of containers."""
|
||||||
containers = run_command(f"docker ps -q --filter volume={volume}").splitlines()
|
container_list = ' '.join(containers)
|
||||||
|
print(f"Stopping containers {container_list}...")
|
||||||
|
execute_shell_command(f"docker stop {container_list}")
|
||||||
|
|
||||||
for container in containers:
|
def start_containers(containers):
|
||||||
# Get the image of the container
|
"""Start a list of containers."""
|
||||||
image = run_command(f"docker inspect --format='{{{{.Config.Image}}}}' {container}")
|
container_list = ' '.join(containers)
|
||||||
|
print(f"Start containers {container_list}...")
|
||||||
|
execute_shell_command(f"docker start {container_list}")
|
||||||
|
|
||||||
# Check if the image contains "postgres" or "mariadb"
|
def is_database(image):
|
||||||
if "postgres" in image or "mariadb" in image:
|
return "postgres" in image or "mariadb" in image
|
||||||
print(f"Container {container} with database image {image} found, using volume {volume}.")
|
|
||||||
|
|
||||||
# Stop the container
|
def is_symbolic_link(file_path):
|
||||||
run_command(f"docker stop {container}")
|
return os.path.islink(file_path)
|
||||||
|
|
||||||
# Get the mount point of the volume
|
def get_volume_path(volume):
|
||||||
volume_path = run_command(f"docker volume inspect --format '{{{{ .Mountpoint }}}}' {volume}")
|
return run_command(f"docker volume inspect --format '{{{{ .Mountpoint }}}}' {volume}")
|
||||||
|
|
||||||
# Create a new directory on the SSD
|
def get_image(volume):
|
||||||
ssd_volume_path = os.path.join(ssd_path, volume)
|
return run_command(f"docker inspect --format='{{{{.Config.Image}}}}' {container}")
|
||||||
os.makedirs(ssd_volume_path, exist_ok=True)
|
|
||||||
|
def pause_and_move(storage_path,volume):
|
||||||
|
stop_containers(containers)
|
||||||
|
# Create a new directory on the Storage
|
||||||
|
storage_volume_path = os.path.join(storage_path, volume)
|
||||||
|
os.makedirs(storage_volume_path, exist_ok=False)
|
||||||
|
|
||||||
# Move the data
|
# Move the data
|
||||||
for item in os.listdir(volume_path):
|
for item in os.listdir(volume_path):
|
||||||
shutil.move(os.path.join(volume_path, item), ssd_volume_path)
|
shutil.move(os.path.join(volume_path, item), storage_volume_path)
|
||||||
|
|
||||||
# Create a symbolic link
|
# Create a symbolic link
|
||||||
os.symlink(ssd_volume_path, volume_path)
|
os.symlink(storage_volume_path, volume_path)
|
||||||
|
|
||||||
|
start_containers(containers)
|
||||||
|
|
||||||
|
for volume in volumes:
|
||||||
|
# List all containers using this volume
|
||||||
|
containers = run_command(f"docker ps -q --filter volume={volume}").splitlines()
|
||||||
|
volume_path = get_volume_path(volume)
|
||||||
|
if is_symbolic_link(volume_path):
|
||||||
|
print(f"Skipped Volume {volume}. The storage path {volume_path} is a symbolic link.")
|
||||||
|
|
||||||
|
for container in containers:
|
||||||
|
# Get the image of the container
|
||||||
|
image = get_image(container)
|
||||||
|
|
||||||
|
# Check if the image contains "postgres" or "mariadb"
|
||||||
|
if is_database(image):
|
||||||
|
print(f"Container {container} with database image {image} found, using volume {volume}.")
|
||||||
|
pause_and_move(ssd_path,volume)
|
||||||
else:
|
else:
|
||||||
print(f"Container {container} with file image {image} found, using volume {volume}.")
|
print(f"Container {container} with file image {image} found, using volume {volume}.")
|
||||||
|
pause_and_move(hdd_path,volume)
|
||||||
|
|
||||||
print("Operation completed.")
|
print("Operation completed.")
|
||||||
|
Loading…
Reference in New Issue
Block a user