Skip to content

Instantly share code, notes, and snippets.

@arevindh
Last active October 1, 2024 09:16
Show Gist options
  • Save arevindh/42f7f3239014af482209f3230bbcda3c to your computer and use it in GitHub Desktop.
Save arevindh/42f7f3239014af482209f3230bbcda3c to your computer and use it in GitHub Desktop.
Move docker volume to new vm . (Shutdown the stack / container first )
#!/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
@arevindh
Copy link
Author

arevindh commented Oct 1, 2024

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:

  1. Checks Dependencies: The script checks if required commands like docker, scp, ssh, and tar are available on your system.
  2. Volume Selection: It lists the available Docker volumes and allows you to pick one.
  3. Backup: It creates a compressed backup (tar.gz) of the selected Docker volume.
  4. Transfer: The script transfers the backup file to the destination VM using scp.
  5. Restoration: You can optionally restore the volume on the destination VM immediately after transfer using SSH.

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:

Available Docker volumes:
1) volume1
2) volume2
3) volume3
Select the number of the volume to move: 2
You selected: volume2

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment