v_restore_user 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #!/bin/bash
  2. # info: restore user
  3. # options: user backup
  4. #
  5. # The function for resotring user from backup.
  6. #----------------------------------------------------------#
  7. # Variable&Function #
  8. #----------------------------------------------------------#
  9. # Argument defenition
  10. user=$1
  11. backup=$2
  12. # Importing variables
  13. source $VESTA/conf/vars.conf
  14. source $V_CONF/vesta.conf
  15. source $V_FUNC/shared.func
  16. source $V_FUNC/domain.func
  17. source $V_FUNC/db.func
  18. # Defining ftp command function
  19. ftpc() {
  20. ftp -n $HOST $PORT <<EOF
  21. quote USER $USERNAME
  22. quote PASS $PASSWORD
  23. binary
  24. cd $BPATH
  25. $1
  26. quit
  27. EOF
  28. }
  29. init_ftp_variables() {
  30. # Checking config
  31. source $V_CONF/ftp.backup.conf
  32. if [ -z "$HOST" ] || [ -z "$USERNAME" ] || [ -z "$PASSWORD" ] ||\
  33. [ -z "$BPATH" ]; then
  34. echo "Error: Parsing error"
  35. log_event 'debug' "$E_PARSING $V_EVENT"
  36. exit $E_PARSING
  37. fi
  38. }
  39. check_ftp_connection(){
  40. # Checking ftp permission
  41. ftmpdir=$(mktemp -u -p $BPATH)
  42. command="mkdir $ftmpdir
  43. ls $ftmpdir
  44. rm $ftmpdir"
  45. if [ ! -z "$(ftpc "$command")" ] ; then
  46. echo "Error: FTP error"
  47. log_event 'debug' "$E_FTP $V_EVENT"
  48. exit $E_FTP
  49. fi
  50. }
  51. #----------------------------------------------------------#
  52. # Verifications #
  53. #----------------------------------------------------------#
  54. # Get current time
  55. start_time=$(date '+%s')
  56. echo "$(date "+%F %T") System restore for user $user"
  57. echo
  58. # Checking arg number
  59. check_args '2' "$#" 'user backup'
  60. # Checking argument format
  61. format_validation 'user' 'backup'
  62. # Checking backup system is enabled
  63. is_system_enabled 'backup'
  64. # Checking load averages
  65. la=$(cat /proc/loadavg |cut -f 1 -d ' '|cut -f 1 -d '.')
  66. i=0
  67. while [ "$la" -ge "$V_BACKUP_LA_LIMIT" ]; do
  68. echo "$(date "+%F %T") Load Average $la"
  69. echo
  70. sleep 60
  71. if [ "$i" -ge "15" ]; then
  72. echo "Error: LA is too high"
  73. log_event 'debug' "$E_LA $V_EVENT"
  74. exit $E_LA
  75. fi
  76. (( ++i))
  77. done
  78. # Checking local backup existance
  79. if [ ! -e "$V_BACKUP/$user.$backup.tar" ]; then
  80. if [ ! -z "$(echo $BACKUP_SYSTEM | grep -w ftp)" ]; then
  81. init_ftp_variables
  82. check_ftp_connection
  83. if [ ! -z "$(ftpc ls |awk '{print $9}' |grep $user.$backup.)" ]; then
  84. cd $V_BACKUP
  85. echo "$(date "+%F %T") Downloading ftp backup"
  86. ftpc "get $user.$backup.tar" >> /dev/null 2>/dev/null
  87. echo "$(date "+%F %T") Downloaded $user.$backup.tar"
  88. fi
  89. fi
  90. fi
  91. if [ ! -e "$V_BACKUP/$user.$backup.tar" ]; then
  92. echo "Error: $V_BACKUP/$user.$backup.tar backup not found"
  93. log_event 'debug' "$E_NOTEXIST $V_EVENT"
  94. exit $E_NOTEXIST
  95. fi
  96. # Checking arguments
  97. if [ -z "$3" ]; then
  98. # Define full backup variables
  99. VESTA='yes'
  100. PAM='yes'
  101. WEB='yes'
  102. DNS='yes'
  103. DB='yes'
  104. MAIL='yes'
  105. SSL='yes'
  106. CRON='yes'
  107. else
  108. args=("$@")
  109. for (( i=2; i<${#@}; i++)); do
  110. key=$(echo ${args[$i]} | cut -f 1 -d :| tr '[:lower:]' '[:upper:]')
  111. opt=$(echo ${args[$i]} | cut -f 2 -d :)
  112. if [ -z "$(echo ${args[$i]} |grep :)" ]; then
  113. eval $key='yes'
  114. else
  115. eval $key='opt'
  116. eval ${key}_OPT=$opt
  117. fi
  118. done
  119. fi
  120. #----------------------------------------------------------#
  121. # Action #
  122. #----------------------------------------------------------#
  123. # Creating temporary directory
  124. tmpdir=$(mktemp -p $V_BACKUP -d)
  125. echo "TMPDIR is $tmpdir"
  126. cd $tmpdir
  127. echo "$(date "+%F %T") Extracting files from backup"
  128. tar -xf $V_BACKUP/$user.$backup.tar
  129. echo "$(date "+%F %T") Backup has been unpacked"
  130. # Checking Vesta
  131. #----------------------------------------------------------#
  132. # Vesta #
  133. #----------------------------------------------------------#
  134. # Logging
  135. log_event 'system' "$V_EVENT"
  136. exit