Advertisement
Guest User

Untitled

a guest
May 28th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.22 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. # Shell script to restore OpenVZ dump image to current system
  3. # #####################Requirment############################
  4. #   1. OpenVZ is installed and running                      #
  5. #   2. vzdump and rsync is installed and running            #
  6. #############################################################
  7. # Author: Hung Nguyen Van
  8. # Synapsys Co. LTD
  9. # 01-06-2010
  10.  
  11. VZDUMP="/usr/sbin/vzrestore"
  12. VZ_IMAGE="$1"
  13. VE_ID="$2"
  14. SCRIPTNAME="$0"
  15. BASENAME=`basename "$SCRIPTNAME"`
  16. RAM=`free -m|grep Mem|awk '{print $2}'`          #Total server ram
  17. SWAP=`free -m|grep Swap|awk '{print $2}'`        #Total server swap
  18. FREE_SPACE=`df -h /var/lib/vz|grep dev|awk '{print $4}'`
  19. MEG=256
  20. test_answer() {
  21.     if [ $1 -gt $2 ] ; then
  22.         exit 1
  23.     fi
  24.     if [ $1 -eq $2 ] ; then
  25.         echo "you've set your value equal to Maximum value!"
  26.         exit 1
  27.     else   
  28.         exit 0
  29.     fi
  30. }
  31.  
  32. restore() {
  33.     if [ ! -e $VZ_IMAGE ] || [ ! $VE_ID ]; then
  34.         echo "Usage: "$BASENAME" IMAGE_PATH VEID"
  35.         echo ""
  36.         exit 1
  37.     else
  38.         vzrestore $VZ_IMAGE $VE_ID
  39.         vz_config
  40.         exit 0
  41.     fi
  42.         }
  43.        
  44. ##########Test############
  45.  
  46. ###################
  47. vz_config() {
  48.     disk_config () {   
  49.         # Set disk space for your VE
  50.         echo "you have "$FREE_SPACE" total!"
  51.         echo ""
  52.         echo "Do not set diskspace greater than "$FREE_SPACE""
  53.         echo ""
  54.         read -p "Maximum space for your Virtual Machine? (ex 20G) " MAX_SPACE
  55.         echo ""
  56.         read -p "Minimum space for your Virtual Machine? (ex 15G) " MIN_SPACE
  57.         echo ""
  58.         test_answer $MAX_SPACE $FREE_SPACE
  59.         if [ "$?" -ne "0" ] ; then
  60.             disk_config
  61.         else
  62.             vzctl set $VE_ID --diskspace $MIN_SPACE:$MAX_SPACE --save
  63.         fi
  64.         }
  65.     # Set Kernel Memory
  66.     TOTAL_MEM=`expr $RAM + $SWAP|bc`
  67.     echo "You have "$TOTAL_MEM"MB memory can be set for your Virtual Machine!"
  68.     echo
  69.     read -p "Maximum KMEMSIZES do you want to set? (number only! ex: 256) " MAX_KMEM
  70.     echo
  71.     test_answer $MAX_KMEM $TOTAL_MEM
  72.     read -p "Minimum KMEMSIZES do you want to set? (number only! ex: 128) " MIM_KMEM
  73.     echo
  74.     test_answer $MIN_KMEM $TOTAL_MEM
  75.     MAX_KMEM=`expr $MAX_KMEM*$MEG|bc`
  76.     MIN_KMEM=`expr $MIN_KMEM*$MEG|bc`
  77.     vzctl set $VE_ID --kmemsize $(($MAX_KMEM)):$(($MIN_KMEM)) --save
  78.    
  79.     #Set Virtual Memory
  80.     read -p "Maximum OOMGUARPAGES do you want to set? (number only! ex: 1024) " MAX_OMMEM
  81.     echo ""
  82.     test_answer $MAX_OMMEM $TOTAL_MEM
  83.     read -p "Minimum OOMGUARPAGES do you want to set? (number only! ex: 1024) " MIN_OMMEM
  84.     echo ""
  85.     test_answer $MIN_OMMEM $TOTAL_MEM
  86.     MAX_OMMEM=`expr $MAX_OMMEM*$MEG|bc`
  87.     MIN_OMMEM=`expr $MIN_OMMEM*$MEG|bc`
  88.     vzctl set $VE_ID --kmemsize $(($MAX_KMEM)):$(($MIN_KMEM)) --save
  89.    
  90.    
  91. #   if [ $MAX_OMMEM -gt $TOTAL_MEMORY or $MIN_OMMEM -gt $TOTAL_MEMORY ]; then
  92. #       echo "Do not set OOMGUARPAGES greater than TOTAL memory of your server!"
  93.        
  94.     }
  95.    
  96. check_dump () {
  97.     if [ -f $VZDUMP ] ; then
  98.         restore
  99.     else
  100.         echo "You dont have vzdump installed"
  101.         echo ""
  102.         read -p "Do you want to install vzdump? (yes/no): " ANSWER
  103.         if [ $ANSWER = "yes" ]; then
  104.             apt-get -y install vzdump rsync
  105.             restore
  106.         else
  107.             exit 1
  108.         fi
  109.     fi 
  110.         }
  111.  
  112. test_root () {
  113.     if [ "`id -ur`" != '0' ]; then
  114.         echo 'Error: you must be root!'
  115.         echo
  116.         exit 1
  117.     else check_dump
  118.     fi
  119.         }
  120.  
  121. if [ ! -e /var/lib/vz ] ; then
  122.     echo "did you install openvz?"
  123.     echo
  124.     exit 1
  125. else
  126.     test_root
  127. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement