Last active
October 1, 2024 09:16
-
-
Save arevindh/42f7f3239014af482209f3230bbcda3c to your computer and use it in GitHub Desktop.
Move docker volume to new vm . (Shutdown the stack / container first )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Function to check if the required commands are available | |
function check_dependencies() { | |
local dependencies=("docker" "scp" "ssh" "tar") | |
for cmd in "${dependencies[@]}"; do | |
if ! command -v $cmd &> /dev/null; then | |
echo "$cmd is required but not installed. Please install it and try again." | |
exit 1 | |
fi | |
done | |
} | |
# Function to display available Docker volumes and let user pick one using 'select' | |
function select_docker_volume() { | |
echo "Available Docker volumes:" | |
volumes=($(docker volume ls --format '{{.Name}}')) | |
if [ ${#volumes[@]} -eq 0 ]; then | |
echo "No Docker volumes found." | |
exit 1 | |
fi | |
select volume_name in "${volumes[@]}"; do | |
if [[ -n "$volume_name" ]]; then | |
echo "You selected: $volume_name" | |
break | |
else | |
echo "Invalid selection. Please try again." | |
fi | |
done | |
} | |
# Function to create a tarball of the volume | |
function backup_volume() { | |
echo "Backing up the volume..." | |
docker run --rm -v "$volume_name:/volume" -v "$(pwd):/backup" busybox tar czf "/backup/${volume_name}.tar.gz" -C /volume . | |
echo "Backup complete: $(pwd)/${volume_name}.tar.gz" | |
} | |
# Function to transfer the tarball to the destination VM | |
function transfer_backup() { | |
read -p "Enter the destination VM (user@hostname): " destination_vm | |
read -p "Enter the destination path on the VM: " destination_path | |
echo "Transferring the backup to $destination_vm..." | |
scp "${volume_name}.tar.gz" "${destination_vm}:${destination_path}" | |
if [ $? -ne 0 ]; then | |
echo "Transfer failed." | |
exit 1 | |
fi | |
echo "Transfer complete." | |
} | |
# Function to restore the volume on the destination VM | |
function restore_volume_on_destination() { | |
read -p "Do you want to restore the volume on the destination VM now? (y/n): " restore_confirm | |
if [[ "$restore_confirm" == "y" || "$restore_confirm" == "Y" ]]; then | |
echo "Restoring the volume on the destination VM..." | |
ssh "$destination_vm" << EOF | |
docker volume create "$volume_name" | |
docker run --rm -v "$volume_name:/volume" -v "$destination_path:/backup" busybox tar xzf "/backup/${volume_name}.tar.gz" -C /volume | |
echo "Volume restored on the destination VM." | |
EOF | |
else | |
echo "Backup transferred. You can manually restore it later." | |
fi | |
} | |
# Main script | |
check_dependencies | |
select_docker_volume | |
backup_volume | |
transfer_backup | |
restore_volume_on_destination |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to Use the Script:
Save the script to a file called move_docker_volume.sh.
nano move_docker_volume.sh
Make the script executable by running:
chmod +x move_docker_volume.sh
Run the script:
./move_docker_volume.sh
What the Script Does:
Example of Volume Selection:
When you run the script, the terminal will show the list of available Docker volumes, and you can pick one by entering its corresponding number:
This script provides an easy-to-use, interactive way of selecting Docker volumes via the terminal and automating the migration of the Docker volume between VMs.
Note:
You might need to make volume
external: true
since the volume is created outside of docker compose on new server.