Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- # Shell script to restore OpenVZ dump image to current system
- # #####################Requirment############################
- # 1. OpenVZ is installed and running #
- # 2. vzdump and rsync is installed and running #
- #############################################################
- # Author: Hung Nguyen Van
- # Synapsys Co. LTD
- # 01-06-2010
- VZDUMP="/usr/sbin/vzrestore"
- VZ_IMAGE="$1"
- VE_ID="$2"
- SCRIPTNAME="$0"
- BASENAME=`basename "$SCRIPTNAME"`
- RAM=`free -m|grep Mem|awk '{print $2}'` #Total server ram
- SWAP=`free -m|grep Swap|awk '{print $2}'` #Total server swap
- FREE_SPACE=`df -h /var/lib/vz|grep dev|awk '{print $4}'`
- MEG=256
- test_answer() {
- if [ $1 -gt $2 ] ; then
- exit 1
- fi
- if [ $1 -eq $2 ] ; then
- echo "you've set your value equal to Maximum value!"
- exit 1
- else
- exit 0
- fi
- }
- restore() {
- if [ ! -e $VZ_IMAGE ] || [ ! $VE_ID ]; then
- echo "Usage: "$BASENAME" IMAGE_PATH VEID"
- echo ""
- exit 1
- else
- vzrestore $VZ_IMAGE $VE_ID
- vz_config
- exit 0
- fi
- }
- ##########Test############
- ###################
- vz_config() {
- disk_config () {
- # Set disk space for your VE
- echo "you have "$FREE_SPACE" total!"
- echo ""
- echo "Do not set diskspace greater than "$FREE_SPACE""
- echo ""
- read -p "Maximum space for your Virtual Machine? (ex 20G) " MAX_SPACE
- echo ""
- read -p "Minimum space for your Virtual Machine? (ex 15G) " MIN_SPACE
- echo ""
- test_answer $MAX_SPACE $FREE_SPACE
- if [ "$?" -ne "0" ] ; then
- disk_config
- else
- vzctl set $VE_ID --diskspace $MIN_SPACE:$MAX_SPACE --save
- fi
- }
- # Set Kernel Memory
- TOTAL_MEM=`expr $RAM + $SWAP|bc`
- echo "You have "$TOTAL_MEM"MB memory can be set for your Virtual Machine!"
- echo
- read -p "Maximum KMEMSIZES do you want to set? (number only! ex: 256) " MAX_KMEM
- echo
- test_answer $MAX_KMEM $TOTAL_MEM
- read -p "Minimum KMEMSIZES do you want to set? (number only! ex: 128) " MIM_KMEM
- echo
- test_answer $MIN_KMEM $TOTAL_MEM
- MAX_KMEM=`expr $MAX_KMEM*$MEG|bc`
- MIN_KMEM=`expr $MIN_KMEM*$MEG|bc`
- vzctl set $VE_ID --kmemsize $(($MAX_KMEM)):$(($MIN_KMEM)) --save
- #Set Virtual Memory
- read -p "Maximum OOMGUARPAGES do you want to set? (number only! ex: 1024) " MAX_OMMEM
- echo ""
- test_answer $MAX_OMMEM $TOTAL_MEM
- read -p "Minimum OOMGUARPAGES do you want to set? (number only! ex: 1024) " MIN_OMMEM
- echo ""
- test_answer $MIN_OMMEM $TOTAL_MEM
- MAX_OMMEM=`expr $MAX_OMMEM*$MEG|bc`
- MIN_OMMEM=`expr $MIN_OMMEM*$MEG|bc`
- vzctl set $VE_ID --kmemsize $(($MAX_KMEM)):$(($MIN_KMEM)) --save
- # if [ $MAX_OMMEM -gt $TOTAL_MEMORY or $MIN_OMMEM -gt $TOTAL_MEMORY ]; then
- # echo "Do not set OOMGUARPAGES greater than TOTAL memory of your server!"
- }
- check_dump () {
- if [ -f $VZDUMP ] ; then
- restore
- else
- echo "You dont have vzdump installed"
- echo ""
- read -p "Do you want to install vzdump? (yes/no): " ANSWER
- if [ $ANSWER = "yes" ]; then
- apt-get -y install vzdump rsync
- restore
- else
- exit 1
- fi
- fi
- }
- test_root () {
- if [ "`id -ur`" != '0' ]; then
- echo 'Error: you must be root!'
- echo
- exit 1
- else check_dump
- fi
- }
- if [ ! -e /var/lib/vz ] ; then
- echo "did you install openvz?"
- echo
- exit 1
- else
- test_root
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement