Move Proxmox Container to Different Storage

Move Proxmox Container to Different Storage

Get Social!

2015-03-05 00_18_04-Proxmox Virtual Environment storageA 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

 


Related posts:


11 Comments

Martin Cleaver

6-Mar-2015 at 6:33 pm

Hi 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 pm

Hi 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 pm

    No, you’re correct – I’ve updated the script to work from any location.

k0nsl

19-May-2015 at 8:56 am

I tried it on one of my CTs and received the following in the end:

[email protected]:~# ./migrate -s storage01 -c 100
Moving 100 to storage01...
Container(s) not found
INFO: starting new backup job: vzdump 100 --dumpdir /tmp
INFO: Starting Backup of VM 100 (openvz)
INFO: CTID 100 exist mounted down
INFO: status = stopped
INFO: backup mode: stop
INFO: ionice priority: 7
INFO: creating archive '/tmp/vzdump-openvz-100-2015_05_18-21_00_49.tar'
INFO: Total bytes written: 98926970880 (93GiB, ?/s)
INFO: tar: -: Wrote only 6144 of 10240 bytes
INFO: tar: Error is not recoverable: exiting now
ERROR: Backup of VM 100 failed - command '(cd /var/lib/vz/private/100;find . '(' -regex '^\.$' ')' -o '(' -type 's' -prune ')' -o -print0|sed 's/\\/\\\\/g'|tar cpf - --totals --sparse --numeric-owner --no-recursion --one-file-system --null -T -) >/tmp/vzdump-openvz-100-2015_05_18-21_00_49.dat' failed: exit code 2
INFO: Backup job finished with errors
job errors
[email protected]:~#


    k0nsl

    19-May-2015 at 4:28 pm

    Nevermind, 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 pm

      I’ve updated the comment near the TMP line to warn people about the size.

Damien

16-Sep-2015 at 4:25 pm

Thanks you make me save at least 2hrs today !

Patrick

23-Nov-2015 at 4:04 pm

Hi 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 pm

    Thanks for the update – I’ll check it out.

Alex

5-May-2016 at 11:04 am

Great script!!!

Thanks Guys. :D

Heinzz

5-Jun-2019 at 4:39 pm

Hi 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. = )

Leave a Reply to Heinzz Cancel reply

Visit our advertisers

Search

Quick Poll

How many Proxmox servers do you work with?

Visit our advertisers