Last active
February 19, 2024 17:20
-
-
Save XReyRobert/aaec1a69eb38f54d869c6b5447babb20 to your computer and use it in GitHub Desktop.
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 | |
# Check if three arguments are passed to the script | |
if [ "$#" -ne 3 ]; then | |
echo "Usage: $0 <REMOTE_HOST> <VOLUME_NAME> <ARCHIVE_PATH>" | |
exit 1 | |
fi | |
REMOTE_HOST=$1 | |
VOLUME_NAME=$2 | |
LOCAL_ARCHIVE_PATH=$3 | |
REMOTE_ARCHIVE_PATH="/tmp/$(basename ${LOCAL_ARCHIVE_PATH})" | |
# Step 1: Upload the archive to the remote host using SCP | |
echo "Uploading ${LOCAL_ARCHIVE_PATH} to ${REMOTE_HOST}:${REMOTE_ARCHIVE_PATH}..." | |
scp "${LOCAL_ARCHIVE_PATH}" "${REMOTE_HOST}:${REMOTE_ARCHIVE_PATH}" | |
# Step 2: Import the archive into the specified volume on the remote host using SSH and podman volume import | |
echo "Importing ${REMOTE_ARCHIVE_PATH} into volume '${VOLUME_NAME}' on ${REMOTE_HOST}..." | |
ssh "${REMOTE_HOST}" "podman volume import ${VOLUME_NAME} ${REMOTE_ARCHIVE_PATH}" | |
if [ $? -eq 0 ]; then | |
echo "Import successful: ${VOLUME_NAME}" | |
else | |
echo "An error occurred during the import." | |
exit 1 | |
fi | |
# Optional: Clean up the archive on the remote host after import | |
echo "Cleaning up remote archive..." | |
ssh "${REMOTE_HOST}" "rm -f ${REMOTE_ARCHIVE_PATH}" | |
echo "Script completed successfully." |
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 | |
# Check if three arguments are passed to the script | |
if [ "$#" -ne 3 ]; then | |
echo "Usage: $0 <REMOTE_HOST> <VOLUME_NAME> <ARCHIVE_PATH>" | |
exit 1 | |
fi | |
REMOTE_HOST=$1 | |
VOLUME_NAME=$2 | |
LOCAL_ARCHIVE_PATH=$3 | |
REMOTE_ARCHIVE_PATH="/tmp/$(basename "${LOCAL_ARCHIVE_PATH}")" | |
# Step 1: Upload the archive to the remote host using SCP | |
echo "Uploading ${LOCAL_ARCHIVE_PATH} to ${REMOTE_HOST}:${REMOTE_ARCHIVE_PATH}..." | |
scp "${LOCAL_ARCHIVE_PATH}" "${REMOTE_HOST}:${REMOTE_ARCHIVE_PATH}" | |
# Step 2: Check if the volume already exists and confirm deletion | |
echo "Checking if volume '${VOLUME_NAME}' exists on ${REMOTE_HOST}..." | |
if ssh "${REMOTE_HOST}" "podman volume inspect ${VOLUME_NAME}" &> /dev/null; then | |
read -p "Volume '${VOLUME_NAME}' already exists. Delete and recreate it? [y/N]: " confirm | |
if [[ ${confirm} =~ ^[Yy]$ ]]; then | |
echo "Deleting existing volume '${VOLUME_NAME}'..." | |
ssh "${REMOTE_HOST}" "podman volume rm ${VOLUME_NAME}" | |
echo "Creating new volume '${VOLUME_NAME}'..." | |
ssh "${REMOTE_HOST}" "podman volume create ${VOLUME_NAME}" | |
else | |
echo "Operation cancelled by the user." | |
exit 1 | |
fi | |
else | |
echo "Volume '${VOLUME_NAME}' does not exist. Creating it..." | |
ssh "${REMOTE_HOST}" "podman volume create ${VOLUME_NAME}" | |
fi | |
# Step 3: Import the archive into the specified volume on the remote host | |
echo "Importing ${REMOTE_ARCHIVE_PATH} into volume '${VOLUME_NAME}' on ${REMOTE_HOST}..." | |
ssh "${REMOTE_HOST}" "podman volume import ${VOLUME_NAME} ${REMOTE_ARCHIVE_PATH}" | |
if [ $? -eq 0 ]; then | |
echo "Import successful: ${VOLUME_NAME}" | |
else | |
echo "An error occurred during the import." | |
exit 1 | |
fi | |
# Optional: Clean up the archive on the remote host after import | |
echo "Cleaning up remote archive..." | |
ssh "${REMOTE_HOST}" "rm -f ${REMOTE_ARCHIVE_PATH}" | |
echo "Script completed successfully." |
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 | |
# Check for minimum number of arguments | |
if [ "$#" -lt 3 ]; then | |
echo "Usage: $0 <SOURCE_HOST> <DESTINATION_HOST> <VOLUME_NAME>..." | |
exit 1 | |
fi | |
SOURCE_HOST=$1 | |
DESTINATION_HOST=$2 | |
VOLUME_NAMES=${@:3} # Capture all volume names passed as arguments | |
for VOLUME_NAME in $VOLUME_NAMES; do | |
TIMESTAMP=$(date +%Y%m%d-%H%M%S) | |
ARCHIVE_NAME="${VOLUME_NAME}-${TIMESTAMP}.tar" | |
REMOTE_ARCHIVE_PATH="/tmp/${ARCHIVE_NAME}" | |
# Export volume from source | |
echo "Exporting volume '${VOLUME_NAME}' from ${SOURCE_HOST}..." | |
ssh $SOURCE_HOST "podman volume export $VOLUME_NAME > $REMOTE_ARCHIVE_PATH" && echo "Export successful." | |
# SCP the archive to the destination host | |
echo "Transferring archive to ${DESTINATION_HOST}..." | |
scp $SOURCE_HOST:$REMOTE_ARCHIVE_PATH $DESTINATION_HOST:$REMOTE_ARCHIVE_PATH && echo "Transfer successful." | |
# Import volume on destination | |
echo "Importing volume '${VOLUME_NAME}' on ${DESTINATION_HOST}..." | |
ssh $DESTINATION_HOST "podman volume create $VOLUME_NAME 2> /dev/null || echo 'Volume already exists, using existing one.'; podman volume import $VOLUME_NAME $REMOTE_ARCHIVE_PATH" && echo "Import successful." | |
# Cleanup archives | |
echo "Cleaning up archives..." | |
ssh $SOURCE_HOST "rm -f $REMOTE_ARCHIVE_PATH" | |
ssh $DESTINATION_HOST "rm -f $REMOTE_ARCHIVE_PATH" | |
echo "Volume '${VOLUME_NAME}' migration completed." | |
done | |
echo "Migration process completed for all volumes." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment