Restore a single Proxmox OpenVZ Container From The command Line
Category : How-to
I mostly use Proxmox from the command line, or terminal, and I have created a few scripts to perform common and repetitive tasks.
The below script will restore a single OpenVZ container to the latest backup file available in the dump directory. The scripts takes a parameter for the container VMID to restore from backup. If the container exists, it will be stopped and removed before restoring the latest backup file available in the backup directory.
The script iterates through all of your backup files and only restores the latest based on the date in the file name, and not the date of the file creation or modified.
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_one
#!/bin/bash # # Filename : restore_one # Description : Restores a single OpenVZ Proxmox container to the latest backup file # 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-$1-*.$BACKUP_EXT function display-useage { echo "Useage $0 [vmid to restore]" echo "Example: $0 999" } # Check dir exists if [ ! -d $BACKUP_PATH ]; then echo "The directory $BACKUP_PATH does not exist" exit 99 fi # Check if argument is present if [ -z "$1" ] then echo "Argument not present." display-useage exit 99 fi # Check if vmid is available, on or off VMON=$(vzlist | grep -P "[ ]+$1[ ]+") VMOFF=$(vzlist --stopped | grep -P "[ ]+$1[ ]+") if [ -n "$VMON" ]; then echo "Requesting stop of container." vzctl stop $1 echo "Requesting deletion of container." vzctl delete $1 elif [ -n "$VMOFF" ]; then echo "Container is stopped." echo "Requesting deletion of container." vzctl delete $1 else echo "Container is not live." fi # Get unique VMIDs for F in $SEARCH_PATH do FILENAME=${F##*/} FILE_DATE=${FILENAME:18:19} FILE_DATE=${FILE_DATE//[_\-]/} if [ -z "$BACKUP_FILE" ]; then BACKUP_FILE=$F fi TEST_FILENAME=${BACKUP_FILE##*/} TEST_FILE_DATE=${TEST_FILENAME:18:19} TEST_FILE_DATE=${TEST_FILE_DATE//[_\-]/} if [ "$FILE_DATE" -gt "$TEST_FILE_DATE" ]; then BACKUP_FILE=$F fi done if [ -n $BACKUP_FILE ]; then # Restore VM echo "Restoring $1 with $BACKUP_FILE..." vzrestore $BACKUP_FILE $1 else echo "No backup file for VMID $1 exists." fi
Make the script executable using chmod.
chmod +x /bin/restore_one
Use the below command, and substitute [VMID] with the container VMID to restore, to run the script.
restore_one [VMID]
See my other script on restoring multiple OpenVZ containers in Proxmox.