backup_setup.sh 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #!/bin/bash
  2. # Author: yeho <lj2007331 AT gmail.com>
  3. # BLOG: https://blog.linuxeye.com
  4. #
  5. # Notes: OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+
  6. #
  7. # Project home page:
  8. # https://oneinstack.com
  9. # https://github.com/lj2007331/oneinstack
  10. export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  11. clear
  12. printf "
  13. #######################################################################
  14. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  15. # Setup the backup parameters #
  16. # For more information please visit https://oneinstack.com #
  17. #######################################################################
  18. "
  19. . ./options.conf
  20. . ./include/color.sh
  21. . ./include/check_dir.sh
  22. . ./include/python.sh
  23. # Check if user is root
  24. [ $(id -u) != "0" ] && { echo "${CFAILURE}Error: You must be root to run this script${CEND}"; exit 1; }
  25. while :; do echo
  26. echo 'Please select your backup destination:'
  27. echo -e "\t${CMSG}1${CEND}. Only Localhost"
  28. echo -e "\t${CMSG}2${CEND}. Only Remote host"
  29. echo -e "\t${CMSG}3${CEND}. Only Qcloud COS"
  30. echo -e "\t${CMSG}4${CEND}. Localhost and Remote host"
  31. echo -e "\t${CMSG}5${CEND}. Localhost and Qcloud COS"
  32. echo -e "\t${CMSG}6${CEND}. Remote host and Qcloud COS"
  33. read -p "Please input a number:(Default 1 press Enter) " DESC_BK
  34. [ -z "$DESC_BK" ] && DESC_BK=1
  35. if [[ ! $DESC_BK =~ ^[1-6]$ ]];then
  36. echo "${CWARNING}input error! Please only input number 1,2,3,4,5,6${CEND}"
  37. else
  38. break
  39. fi
  40. done
  41. [ "$DESC_BK" == '1' ] && sed -i 's@^backup_destination=.*@backup_destination=local@' ./options.conf
  42. [ "$DESC_BK" == '2' ] && sed -i 's@^backup_destination=.*@backup_destination=remote@' ./options.conf
  43. [ "$DESC_BK" == '3' ] && sed -i 's@^backup_destination=.*@backup_destination=cos@' ./options.conf
  44. [ "$DESC_BK" == '4' ] && sed -i 's@^backup_destination=.*@backup_destination=local,remote@' ./options.conf
  45. [ "$DESC_BK" == '5' ] && sed -i 's@^backup_destination=.*@backup_destination=local,cos@' ./options.conf
  46. [ "$DESC_BK" == '6' ] && sed -i 's@^backup_destination=.*@backup_destination=Remote,cos@' ./options.conf
  47. while :; do echo
  48. echo 'Please select your backup content:'
  49. echo -e "\t${CMSG}1${CEND}. Only Database"
  50. echo -e "\t${CMSG}2${CEND}. Only Website"
  51. echo -e "\t${CMSG}3${CEND}. Database and Website"
  52. read -p "Please input a number:(Default 1 press Enter) " CONTENT_BK
  53. [ -z "$CONTENT_BK" ] && CONTENT_BK=1
  54. if [[ ! $CONTENT_BK =~ ^[1-3]$ ]];then
  55. echo "${CWARNING}input error! Please only input number 1,2,3${CEND}"
  56. else
  57. break
  58. fi
  59. done
  60. [ "$CONTENT_BK" == '1' ] && sed -i 's@^backup_content=.*@backup_content=db@' ./options.conf
  61. [ "$CONTENT_BK" == '2' ] && sed -i 's@^backup_content=.*@backup_content=web@' ./options.conf
  62. [ "$CONTENT_BK" == '3' ] && sed -i 's@^backup_content=.*@backup_content=db,web@' ./options.conf
  63. if [ "$DESC_BK" != '3' ];then
  64. while :; do echo
  65. echo "Please enter the directory for save the backup file: "
  66. read -p "(Default directory: $backup_dir): " NEW_backup_dir
  67. [ -z "$NEW_backup_dir" ] && NEW_backup_dir="$backup_dir"
  68. if [ -z "`echo $NEW_backup_dir| grep '^/'`" ]; then
  69. echo "${CWARNING}input error! ${CEND}"
  70. else
  71. break
  72. fi
  73. done
  74. sed -i "s@^backup_dir=.*@backup_dir=$NEW_backup_dir@" ./options.conf
  75. fi
  76. while :; do echo
  77. echo "Pleas enter a valid backup number of days: "
  78. read -p "(Default days: 5): " expired_days
  79. [ -z "$expired_days" ] && expired_days=5
  80. [ -n "`echo $expired_days | sed -n "/^[0-9]\+$/p"`" ] && break || echo "${CWARNING}input error! Please only enter numbers! ${CEND}"
  81. done
  82. sed -i "s@^expired_days=.*@expired_days=$expired_days@" ./options.conf
  83. if [ "$CONTENT_BK" != '2' ];then
  84. databases=`$db_install_dir/bin/mysql -uroot -p$dbrootpwd -e "show databases\G" | grep Database | awk '{print $2}' | grep -Evw "(performance_schema|information_schema|mysql|sys)"`
  85. while :; do echo
  86. echo "Please enter one or more name for database, separate multiple database names with commas: "
  87. read -p "(Default database: `echo $databases | tr ' ' ','`) " db_name
  88. db_name=`echo $db_name | tr -d ' '`
  89. [ -z "$db_name" ] && db_name="`echo $databases | tr ' ' ','`"
  90. D_tmp=0
  91. for D in `echo $db_name | tr ',' ' '`
  92. do
  93. [ -z "`echo $databases | grep -w $D`" ] && { echo "${CWARNING}$D was not exist! ${CEND}" ; D_tmp=1; }
  94. done
  95. [ "$D_tmp" != '1' ] && break
  96. done
  97. sed -i "s@^db_name=.*@db_name=$db_name@" ./options.conf
  98. fi
  99. if [ "$CONTENT_BK" != '1' ];then
  100. websites=`ls $wwwroot_dir | grep -vw default`
  101. while :; do echo
  102. echo "Please enter one or more name for website, separate multiple website names with commas: "
  103. read -p "(Default website: `echo $websites | tr ' ' ','`) " website_name
  104. website_name=`echo $website_name | tr -d ' '`
  105. [ -z "$website_name" ] && website_name="`echo $websites | tr ' ' ','`"
  106. W_tmp=0
  107. for W in `echo $website_name | tr ',' ' '`
  108. do
  109. [ ! -e "$wwwroot_dir/$W" ] && { echo "${CWARNING}$wwwroot_dir/$W not exist! ${CEND}" ; W_tmp=1; }
  110. done
  111. [ "$W_tmp" != '1' ] && break
  112. done
  113. sed -i "s@^website_name=.*@website_name=$website_name@" ./options.conf
  114. fi
  115. echo
  116. echo "You have to backup the content:"
  117. [ "$CONTENT_BK" != '2' ] && echo "Database: ${CMSG}$db_name${CEND}"
  118. [ "$CONTENT_BK" != '1' ] && echo "Website: ${CMSG}$website_name${CEND}"
  119. if [[ "$DESC_BK" =~ ^[2,4,6]$ ]];then
  120. > tools/iplist.txt
  121. while :; do echo
  122. read -p "Please enter the remote host ip: " remote_ip
  123. [ -z "$remote_ip" -o "$remote_ip" == '127.0.0.1' ] && continue
  124. echo
  125. read -p "Please enter the remote host port(Default: 22) : " remote_port
  126. [ -z "$remote_port" ] && remote_port=22
  127. echo
  128. read -p "Please enter the remote host user(Default: root) : " remote_user
  129. [ -z "$remote_user" ] && remote_user=root
  130. echo
  131. read -p "Please enter the remote host password: " remote_password
  132. IPcode=$(echo "ibase=16;$(echo "$remote_ip" | xxd -ps -u)"|bc|tr -d '\\'|tr -d '\n')
  133. Portcode=$(echo "ibase=16;$(echo "$remote_port" | xxd -ps -u)"|bc|tr -d '\\'|tr -d '\n')
  134. PWcode=$(echo "ibase=16;$(echo "$remote_password" | xxd -ps -u)"|bc|tr -d '\\'|tr -d '\n')
  135. [ -e "~/.ssh/known_hosts" ] && grep $remote_ip ~/.ssh/known_hosts | sed -i "/$remote_ip/d" ~/.ssh/known_hosts
  136. ./tools/mssh.exp ${IPcode}P $remote_user ${PWcode}P ${Portcode}P true 10
  137. if [ $? -eq 0 ];then
  138. [ -z "`grep $remote_ip tools/iplist.txt`" ] && echo "$remote_ip $remote_port $remote_user $remote_password" >> tools/iplist.txt || echo "${CWARNING}$remote_ip has been added! ${CEND}"
  139. while :; do
  140. read -p "Do you want to add more host ? [y/n]: " more_host_yn
  141. if [ "$more_host_yn" != 'y' -a "$more_host_yn" != 'n' ];then
  142. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  143. else
  144. break
  145. fi
  146. done
  147. [ "$more_host_yn" == 'n' ] && break
  148. fi
  149. done
  150. fi
  151. if [[ "$DESC_BK" =~ ^[3,5,6]$ ]];then
  152. [ ! -e "${python_install_dir}/bin/python" ] && Install_Python
  153. [ ! -e "${python_install_dir}/lib/python2.7/site-packages/requests" ] && ${python_install_dir}/bin/pip install requests
  154. while :; do echo
  155. echo 'Please select your backup datacenter:'
  156. echo -e "\t ${CMSG}1${CEND}. 华南(广州) ${CMSG}2${CEND}. 华北(天津)"
  157. echo -e "\t ${CMSG}3${CEND}. 华东(上海) ${CMSG}4${CEND}. 新加坡"
  158. read -p "Please input a number:(Default 1 press Enter) " Location
  159. [ -z "$Location" ] && Location=1
  160. if [ ${Location} -ge 1 >/dev/null 2>&1 -a ${Location} -le 4 >/dev/null 2>&1 ]; then
  161. break
  162. else
  163. echo "${CWARNING}input error! Please only input number 1~4${CEND}"
  164. fi
  165. done
  166. [ "$Location" == '1' ] && region=gz
  167. [ "$Location" == '2' ] && region=tj
  168. [ "$Location" == '3' ] && region=sh
  169. [ "$Location" == '4' ] && region=sgp
  170. [ -e "/root/.coscredentials" ] && rm -rf /root/.coscredentials
  171. while :; do echo
  172. read -p "Please enter the Qcloud COS appid: " appid
  173. [ -z "$appid" ] && continue
  174. echo
  175. read -p "Please enter the Qcloud COS secret id: " secret_id
  176. [ -z "$secret_id" ] && continue
  177. echo
  178. read -p "Please enter the Qcloud COS secret key: " secret_key
  179. [ -z "$secret_key" ] && continue
  180. echo
  181. read -p "Please enter the Qcloud COS bucket: " bucket
  182. [ -z "$bucket" ] && continue
  183. echo
  184. $python_install_dir/bin/python ./tools/coscmd config --appid=$appid --id=$secret_id --key=$secret_key --region=$region --bucket=$bucket >/dev/null 2>&1
  185. if [ "`$python_install_dir/bin/python ./tools/coscmd ls /`" == 'True' ];then
  186. echo "${CMSG}appid/secret_id/secret_key/region/bucket OK${CEND}"
  187. echo
  188. break
  189. else
  190. echo "${CWARNING}input error! appid/secret_id/secret_key/region/bucket invalid${CEND}"
  191. fi
  192. done
  193. fi