Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save davibusanello/aa2127dbda8b80e9c075a5b04d404490 to your computer and use it in GitHub Desktop.
Save davibusanello/aa2127dbda8b80e9c075a5b04d404490 to your computer and use it in GitHub Desktop.

Revisions

  1. @etoosamoe etoosamoe created this gist Apr 6, 2023.
    110 changes: 110 additions & 0 deletions docker-volume-transfer.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,110 @@
    # Goal

    You have docker volume on one host and want to move that volume to another host. For example, you want to transfer PostgreSQL container with it's volume to another host.

    # Solution

    Actually, there is no in-a-box solution from Docker, so we need to:
    - attach our volume to another container
    - copy and archive all files
    - copy archive to another host
    - create volume

    # Script

    To be honest, original script was posted on [Medium](https://medium.com/@ioannis.gkikasth/how-to-migrate-a-docker-volume-from-one-machine-to-another-7445ff172957), by [@funkyfisch](https://github.com/funkyfisch). Original gist - https://gist.github.com/funkyfisch/0372475f82639cbba0fbc55df7eff648#file-migrate-docker-volume-sh.

    ```
    #!/bin/bash
    # Environment check
    SOURCE_HOST_ADDRESS=localhost
    SOURCE_HOST_USER=$USER
    TARGET_HOST_ADDRESS=192.168.126.168
    TARGET_HOST_USER=targetuser
    # If migrating from localhost, set this to localhost
    if [[ ! $SOURCE_HOST_ADDRESS ]]
    then
    echo "Please set the current host address in SOURCE_HOST_ADDRESS"
    exit 1
    fi
    # If migrating from localhost, set this to $USER
    if [[ ! $SOURCE_HOST_USER ]]
    then
    echo "Please set the current host user in SOURCE_HOST"
    exit 1
    fi
    # If migrating to localhost, set this to localhost
    if [[ ! $TARGET_HOST_ADDRESS ]]
    then
    echo "Please set the new host address in TARGET_HOST_ADDRESS"
    exit 1
    fi
    # If migrating to localhost, set this to $USER
    if [[ ! $TARGET_HOST_USER ]]
    then
    echo "Please set the new host user in TARGET_HOST_USER"
    exit 1
    fi
    # Argument check
    if [[ ! $1 ]]
    then
    echo "Please supply a docker volume name, as displayed by the command 'docker volume ls'"
    exit 1
    fi
    volume_name=$1
    # Export the volume from the current instance
    echo "Exporting volume $volume_name on $SOURCE_HOST_ADDRESS"
    ssh "$SOURCE_HOST_USER"@"$SOURCE_HOST_ADDRESS" "\
    mkdir -p \$HOME/docker-volume-backup
    docker run \
    --rm \
    -v $volume_name:/volume-backup-source \
    -v \$HOME/docker-volume-backup:/volume-backup-target \
    busybox \
    sh -c 'cd /volume-backup-source && tar cf /volume-backup-target/backup.tar .' \
    "
    # Transfer the exported volume to the new address
    echo "Transferring exported volume $volume_name from $SOURCE_HOST_ADDRESS to $TARGET_HOST_ADDRESS"
    ssh "$TARGET_HOST_USER"@"$TARGET_HOST_ADDRESS" "mkdir -p \$HOME/docker-volume-backup"
    scp -3 "$SOURCE_HOST_USER"@"$SOURCE_HOST_ADDRESS":./docker-volume-backup/backup.tar \
    "$TARGET_HOST_USER"@"$TARGET_HOST_ADDRESS":./docker-volume-backup/backup.tar
    # Restore the backup
    echo "Creating volume $volume_name on $TARGET_HOST_ADDRESS"
    ssh "$TARGET_HOST_USER"@"$TARGET_HOST_ADDRESS" "\
    docker volume create $volume_name \
    && docker run \
    --rm \
    -v $volume_name:/volume-backup-target \
    -v \$HOME/docker-volume-backup/:/volume-backup-source \
    busybox \
    sh -c 'cd /volume-backup-target && tar xf /volume-backup-source/backup.tar .' \
    "
    # Clean up residual files
    echo "Cleaning up unnecessary files"
    ssh "$SOURCE_HOST_USER"@"$SOURCE_HOST_ADDRESS" "rm -rf \$HOME/docker-volume-backup"
    ssh "$TARGET_HOST_USER"@"$TARGET_HOST_ADDRESS" "rm -rf \$HOME/docker-volume-backup"
    echo "Successfully migrated docker volume $volume_name from $SOURCE_HOST_ADDRESS to $TARGET_HOST_ADDRESS"
    ```

    # Run

    `./script.sh sentry-postgres`