Restore all Proxmox OpenVZ Containers From The command Line

Restore all Proxmox OpenVZ Containers From The command Line

Get Social!

proxmox logo gradI use Proxmox to host a development environment using OpenVZ containers. I take frequent backups of all OpenVZ containers in the event I need to roll back any development work.

The below script restores all OpenVZ containers which are available in the backup folder, but not available in the Proxmox GUI. Using this script, you can remove the containers in Proxmox which you would like to restore and run the script to restore the latest backup.

The script iterates through all of your backup files and only restores the latest based on the date in the file name.

You will need to set the BACKUP_PATH variable to the location of your backup folder with no trailing slash, and BACKUP_EXT with the extension used for your chosen backup format.

If you save this script in the /bin then you can call the script from the terminal without having to move to the scripts directory. Create the file and paste the below script into it.

vi /bin/restore_all
#!/bin/bash
#
# Filename : restore_all
# Description : Restores all missing OpenVZ containers in Proxmox to the latest version available in the dump folder.
# Author : James Coyle
#
# Version:
# -Date       -Author      -Description
# 01-11-2013  James Coyle  Initial
#
#

BACKUP_PATH=/var/lib/vz/dump
BACKUP_EXT=tar.lzo

# Do not change
SEARCH_PATH=$BACKUP_PATH/vzdump-openvz-*.$BACKUP_EXT

# Check dir exists
if [ ! -d $BACKUP_PATH ]; then
  echo "The directory $BACKUP_PATH does not exist"
  exit 99
fi

# Get unique VMIDs
for F in $SEARCH_PATH
do
  FILENAME=${F##*/}
  FILE_DATE=${FILENAME:18:19}
  FILE_DATE=${FILE_DATE//[_\-]/}
  VMID=${FILENAME:14:3}
  if [ -n $FILE_VIMS[$VMID] ]; then
    FILE_VIMS[$VMID]=$F
  fi

  TEST_FILENAME=${FILE_VIMS[$VMID]##*/}
  TEST_FILE_DATE=${TEST_FILENAME:18:19}
  TEST_FILE_DATE=${TEST_FILE_DATE//[_\-]/}
  if [ "$FILE_DATE" -gt "$TEST_FILE_DATE" ]; then
    FILE_VIMS[$VMID]=F
  fi
done

# Restore VM
for I in ${!FILE_VIMS[*]}
do
  echo "Restoring $I with ${FILE_VIMS[$I]}..."
  vzrestore ${FILE_VIMS[$I]} $I
done

Make the script executable using chmod.

chmod +x /bin/restore_all

Use the below command to run the script and restore all containers which are missing from backup.

restore_all

Limit backup I/O bandwidth

Category : How-to

Get Social!

proxmox_logoWhen backing up in Proxmox, especially on the lower end of the server market, the backup process can cause your system to almost grind to a halt. Luckily, there is a config change which we can make to limit the bandwidth of the backup process to make sure there is sufficient bandwidth for everything else.

The setting cannot be changed using the web gui, you must log into your Proxmox server using SSH. Once you have logged in, open the config file:

vi /etc/vzdump.conf

Find the line which contains bwlimit, remove the hash and append the value of KB/s which you would like the backup limiting to. The below example limits the backup process to 40000 KB/s.

bwlimit 40000

To calculate how many KB/s you need will take trial and error to see what affect speeds have on your system. Something which is helpful however, is converting MBs (Megabytes) into KB/s (Kilobytes). The formula is simple – just multiply the MB value by 1024 as there are 1024 KB in a MB.

MB/s * 1024 = KB/s

If you want a transfer speed of 10 MB/s you would use a bwlimit value of 10240 as the below example shows.

10 MB/s * 1024 = 10240 KB/s

Backup ESXi 5.x running virtual machine

Category : How-to

Get Social!

ESXi does not provide an option to backup a running virtual machine, without paying VMware a substantial amount of money. You can get round this by turning off the guest and copying the VMDK however this requires downtime for the guest. You cannot copy the VMDK while the guest is running because the VMDK virtual disk cannot be copied while it is in use. As this is likely to be the boot disk for the virtual machine, stopping disk activity without turning the instance off is impossible. The below simple script creates a snapshot of the guest which forces the guest to write all new changes to a new file instead of writing them to the virtual disk. This frees the disk to be copied. Once the copy has completed, the snapshot is removed writing all changes since the backup started back to the virtual disk.

This script contains little error checking and requires a few conditions to be met in order to complete successfully.
• No device maps for physical disks must exist in the instance
• ‘Independent’ disks must not exist in the instance
• The parameters of the script must be completed correctly.

There are 2 lines in the script which must be configured. Open the script using a text editor and change the below variables to match your preferences.
#Config
BASE_PATH=”PATH_TO_VIRTUAL_MACHINES”
BACKUP_ROOT=”PATH_TO_SAVE_BACKUP”

Example
#Config
BASE_PATH=”/vmfs/volumes/datastore1/Machines/”
BACKUP_ROOT=”/vmfs/volumes/datastore1/Backup/”

Copy the script to the below location on your ESXi server. The file name of the script should be VMbackupBash E.G. /usr/bin/VMbackupBash

To run the script, type
VMbackupBash [MACHINE_FOLDER_TO_BACKUP]
The machine name must be the name of the folder in BASE_PATH which contains the instance to backup.

#!/bin/bash
#Backup VMware instance

echo ""
echo "-------------------------------"
echo "- VM Backup Script -"
echo "- James Coyle -"
echo "-------------------------------"
echo ""

#Config
#root path to where your running instances are stored.
BASE_PATH="/vmfs/volumes/datastore1/Machines/"

#root path to where you would like to save the backup
BACKUP_ROOT="/vmfs/volumes/datastore1/Backup/"

#Do not edit
if [ "$1" ]; then
MACHINE=$1

else
#MACHINE=$1
echo "Enter machine name: "
read MACHINE

fi

BACKUP_APPEND=$(date +"%Y%m%d-%H%M%S")
MACHINE_PATH="$BASE_PATH$MACHINE/"
BACKUP_PATH="$BACKUP_ROOT$MACHINE$BACKUP_APPEND/"

if [ -d $MACHINE_PATH ] && [ -d $BACKUP_ROOT ]; then

cd $MACHINE_PATH

VIMID=`vim-cmd vmsvc/getallvms | egrep -o "[0-9]+[ ]+$MACHINE" | cut -d" " -f1`

if [ -z VIMID ]; then
echo "Could not get VM ID."
vim-cmd vmsvc/getallvms

echo ""
echo "Enter vimid of $MACHINE"
read VIMID
echo ""
echo ""

fi

#Get existing files
FILE_ARRAY=`find $MACHINE_PATH -type f`

I=0
for T in $FILE_ARRAY[@]
do
#echo "$I[$T]"
I=$((I+1))
done

echo "- Backup info"
echo "-------------------------------"
echo " Machine path: $MACHINE_PATH"
echo " Backup path: $BACKUP_PATH"
echo " Using VM ID: $VIMID."
echo " Machine name: $MACHINE."
echo " Files in Machine folder: $I."
echo ""

echo "- Starting backup"
echo "-------------------------------"
mkdir $BACKUP_PATH

echo " Creating snapshot..."
vim-cmd vmsvc/snapshot.create $VIMID tempbackupsnapshot
echo " Snapshot complete."
IFS='
'
for A in $FILE_ARRAY
do
if [ -n "`echo \"$A\" | egrep '.vmdk|.nvram|.vmx'`" ] ; then
FILESIZE=$(stat -c%s "$A")
echo " Copying file: $A - Size: $FILESIZE bytes"
cp "$A" "$BACKUP_PATH"

else
echo " Skipping file: $A"

fi

done

#Remove snapshot
echo " Removing snapshot..."
vim-cmd vmsvc/snapshot.removeall $VIMID
echo " Snapshot removed."

echo " Starting compression..."
tar -czpf "$BACKUP_ROOT/$MACHINE-$BACKUP_APPEND.tgz" "$BACKUP_PATH"
echo " Compression complete."

echo " Removing backup temp..."
rm -rf "$BACKUP_PATH"
echo " Removed temp files."

echo ""
echo "-------------------------------"
echo " Backup Complete!"
echo "-------------------------------"

else
echo ""
echo ""
echo "-------------------------------"
echo " Error"
echo "-------------------------------"
echo " Folder $MACHINE_PATH or $BACKUP_ROOT does not exists"
#exit 1
fi

Visit our advertisers

Quick Poll

Do you use GlusterFS in your workplace?

Visit our advertisers