Move Proxmox Container to Different Storage
Category : How-to
A task often required when new storage is added or removed, and containers grow over time is to move a container onto another storage device.
The Proxmox Web GUI does not give us the ability to migrate a container from one storage device to another directly. To move a container onto different storage we have to take a backup of the container and restore it to the same ID with a different storage device specified. This can be time laborious when working with several containers.
The below script allows you to move an OpenVZ container from one storage device to another. The process requires that the container be stopped, which the script will handle.
Save the below script into a file called migrate.
vi migrate
#!/bin/bash # # Filename : migrate # Description : Migrate Proxmox OpenVZ container from one storage to another # Author : James Coyle # # Version: # -Date -Author -Description # 20-11-2013 James Coyle Initial # # # Variables TMP=/tmp #Location to use to create the backup for transferring to new storage. This needs to be big enough to store the backup archive for the container. # Do not edit usage() { echo "Usage: $0" echo " [-c Required: Container ID to migrate <int>] " echo " [-s Required: Target storage ID <string>]" echo " [-d Optional: Delete the backup file after CT restoration <boolean>]" echo "" echo "Example: $0 -c 100 -s nasarray" echo "" exit 1; } while getopts "c:s:d" o; do case "${o}" in c) CT=${OPTARG} ;; s) TARGET_STORAGE=${OPTARG} ;; d) DELETE=true ;; *) usage ;; esac done shift $((OPTIND-1)) # Check mandatory fields if [ -z "${CT}" ] || [ -z "${TARGET_STORAGE}" ]; then usage fi RUNNING=false set -e set -o pipefail echo "Moving $CT to $TARGET_STORAGE..." if vzlist | fgrep -w -q " $CT " then RUNNING=true fi if $RUNNING then vzctl stop $CT fi vzdump --dumpdir $TMP $CT ARCHIVE=$(ls -t $TMP/vzdump-openvz-$CT-*.tar | head -n 1) vzrestore $ARCHIVE $CT -force -storage $TARGET_STORAGE if $RUNNING then vzctl start $CT fi if $DELETE then LOG=$(ls -t $TMP/vzdump-openvz-$CT-*.log | head -n 1) echo "Deleting $LOG and $ARCHIVE" rm -rf $ARCHIVE $TMP/$LOG fi
Set execution permissions on the script:
chmod + x migrate
The script has several parameters which are detailed below:
- -d is specified if you would like the script to delete the temporary backup after the process has completed. Leave this out if you would like the backup tar file to be kept, just in case anything goes wrong.
- -s is required to specify the name of the target storage. You can find this from the Proxmox Web GUI.
- -c is required for the container ID to migrate.
In addition, the script contains the variable TMP. This will be the location of the backup tar created as part of the migration process and must contain enough space to store the content of the container being migrated. You can change this to suit your environment.
Example command:
./migrate -d -s newstorage -c 101
11 Comments
Martin Cleaver
6-Mar-2015 at 6:33 pmHi James,
I see you have a github account at https://github.com/JAC2703
Have you thought about keeping your proxmox utilities there?
Also, I built a bunch of utilities in a framework for proxmox as scripts in https://github.com/mrjcleaver/proxmox-setup
e.g. https://github.com/mrjcleaver/proxmox-setup/tree/master/lib/proxmox-setup
Regards, Martin
Aggelos Karalias
12-Mar-2015 at 6:19 pmHi James,
thanks for the script it works as expected. I had to slightly change the
$(ls -t vzdump-openvz-$CT-*.log | head -n 1)
part to$(cd $TMP && ls -t vzdump-openvz-$CT-*.log | head -n 1)
in both occurencies, because ls could not locate the file when I was running the script from a different directory that $TMP.Am I missing something?
Regards,
Aggelos
james.coyle
6-Apr-2015 at 2:19 pmNo, you’re correct – I’ve updated the script to work from any location.
k0nsl
19-May-2015 at 8:56 amI tried it on one of my CTs and received the following in the end:
k0nsl
19-May-2015 at 4:28 pmNevermind, I didn’t have enough space allocated for /tmp. I changed the location to another drive, and presto! Works like a charm.
james.coyle
27-May-2015 at 8:37 pmI’ve updated the comment near the TMP line to warn people about the size.
Damien
16-Sep-2015 at 4:25 pmThanks you make me save at least 2hrs today !
Patrick
23-Nov-2015 at 4:04 pmHi James,
Made some changes to support PVE 4. Thanks!
#!/bin/bash
#
# Filename : migrate
# Description : Migrate Proxmox OpenVZ container from one storage to another
# Author : James Coyle
#
# Version:
# -Date -Author -Description
# 20-11-2013 James Coyle Initial
# 23-11-2015 Patrick Smits Changes to support PVE 4
#
# Variables
TMP=/pve/backup/temp #Location to use to create the backup for transferring to new storage. This needs to be big enough to store the backup archive for the container
# Do not edit
usage() {
echo "Usage: $0"
echo " [-c Required: Container ID to migrate ] "
echo " [-s Required: Target storage ID ]"
echo " [-d Optional: Delete the backup file after CT restoration ]"
echo ""
echo "Example: $0 -c 100 -s nasarray"
echo ""
exit 1;
}
while getopts "c:s:d" o; do
case "${o}" in
c)
CT=${OPTARG}
;;
s)
TARGET_STORAGE=${OPTARG}
;;
d)
DELETE=true
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
# Check mandatory fields
if [ -z "${CT}" ] || [ -z "${TARGET_STORAGE}" ]; then
usage
fi
RUNNING=false
set -e
set -o pipefail
echo "Moving $CT to $TARGET_STORAGE..."
if pct list | grep running | fgrep "$CT"
then
RUNNING=true
fi
if $RUNNING
then
pct stop $CT
fi
vzdump --dumpdir $TMP $CT
ARCHIVE=$(ls -t $TMP/vzdump-lxc-$CT-*.tar | head -n 1)
pct restore $CT $ARCHIVE -force -storage $TARGET_STORAGE
if $RUNNING
then
pct start $CT
fi
if $DELETE
then
LOG=$(ls -t $TMP/vzdump-lxc-$CT-*.log | head -n 1)
echo "Deleting $LOG and $ARCHIVE"
rm -rf $ARCHIVE $TMP/$LOG
fi
james.coyle
23-Nov-2015 at 4:49 pmThanks for the update – I’ll check it out.
Alex
5-May-2016 at 11:04 amGreat script!!!
Thanks Guys. :D
Heinzz
5-Jun-2019 at 4:39 pmHi James,
A little bit of Necro-posting but thanks for your script, saved me a few hours today as well. = )
I have made a few changes to get it up-to-date with Proxmox 5.x, i see that you are not really updating your GitHub repo, but if you are interested, i’ll be happy to share. = )