backup_setup.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #!/bin/bash
  2. # Author: yeho <lj2007331 AT gmail.com>
  3. # BLOG: https://blog.linuxeye.cn
  4. #
  5. # Notes: OneinStack for CentOS/RadHat 6+ Debian 7+ 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 6+ Debian 7+ and Ubuntu 12+ #
  15. # Setup the backup parameters #
  16. # For more information please visit https://oneinstack.com #
  17. #######################################################################
  18. "
  19. # Check if user is root
  20. [ $(id -u) != "0" ] && { echo "${CFAILURE}Error: You must be root to run this script${CEND}"; exit 1; }
  21. oneinstack_dir=$(dirname "`readlink -f $0`")
  22. pushd ${oneinstack_dir} > /dev/null
  23. . ./options.conf
  24. . ./versions.txt
  25. . ./include/color.sh
  26. . ./include/check_os.sh
  27. . ./include/check_dir.sh
  28. . ./include/download.sh
  29. . ./include/python.sh
  30. while :; do echo
  31. echo 'Please select your backup destination:'
  32. echo -e "\t${CMSG}1${CEND}. Localhost"
  33. echo -e "\t${CMSG}2${CEND}. Remote host"
  34. echo -e "\t${CMSG}3${CEND}. Qcloud COS"
  35. echo -e "\t${CMSG}4${CEND}. UPYUN(又拍云)"
  36. read -p "Please input a number:(Default 1 press Enter) " desc_bk
  37. [ -z "$desc_bk" ] && desc_bk=1
  38. ary=(1 2 3 4 12 13 14 23 24 34 123 124 234 1234)
  39. if [[ "${ary[@]}" =~ "$desc_bk" ]]; then
  40. break
  41. else
  42. echo "${CWARNING}input error! Please only input number 1,2,12,23,234 and so on${CEND}"
  43. fi
  44. done
  45. sed -i 's@^backup_destination=.*@backup_destination=@' ./options.conf
  46. [ `echo $desc_bk | grep -e 1` ] && sed -i 's@^backup_destination=.*@backup_destination=local@' ./options.conf
  47. [ `echo $desc_bk | grep -e 2` ] && sed -i 's@^backup_destination=.*@&,remote@' ./options.conf
  48. [ `echo $desc_bk | grep -e 3` ] && sed -i 's@^backup_destination=.*@&,cos@' ./options.conf
  49. [ `echo $desc_bk | grep -e 4` ] && sed -i 's@^backup_destination=.*@&,upyun@' ./options.conf
  50. sed -i 's@^backup_destination=,@backup_destination=@' ./options.conf
  51. while :; do echo
  52. echo 'Please select your backup content:'
  53. echo -e "\t${CMSG}1${CEND}. Only Database"
  54. echo -e "\t${CMSG}2${CEND}. Only Website"
  55. echo -e "\t${CMSG}3${CEND}. Database and Website"
  56. read -p "Please input a number:(Default 1 press Enter) " content_bk
  57. [ -z "$content_bk" ] && content_bk=1
  58. if [[ ! $content_bk =~ ^[1-3]$ ]]; then
  59. echo "${CWARNING}input error! Please only input number 1~3${CEND}"
  60. else
  61. break
  62. fi
  63. done
  64. [ "$content_bk" == '1' ] && sed -i 's@^backup_content=.*@backup_content=db@' ./options.conf
  65. [ "$content_bk" == '2' ] && sed -i 's@^backup_content=.*@backup_content=web@' ./options.conf
  66. [ "$content_bk" == '3' ] && sed -i 's@^backup_content=.*@backup_content=db,web@' ./options.conf
  67. if [[ $desc_bk =~ ^[1,2]$ ]]; then
  68. while :; do echo
  69. echo "Please enter the directory for save the backup file: "
  70. read -p "(Default directory: ${backup_dir}): " new_backup_dir
  71. [ -z "${new_backup_dir}" ] && new_backup_dir="${backup_dir}"
  72. if [ -z "`echo ${new_backup_dir}| grep '^/'`" ]; then
  73. echo "${CWARNING}input error! ${CEND}"
  74. else
  75. break
  76. fi
  77. done
  78. sed -i "s@^backup_dir=.*@backup_dir=${new_backup_dir}@" ./options.conf
  79. fi
  80. while :; do echo
  81. echo "Pleas enter a valid backup number of days: "
  82. read -p "(Default days: 5): " expired_days
  83. [ -z "$expired_days" ] && expired_days=5
  84. [ -n "`echo $expired_days | sed -n "/^[0-9]\+$/p"`" ] && break || echo "${CWARNING}input error! Please only enter numbers! ${CEND}"
  85. done
  86. sed -i "s@^expired_days=.*@expired_days=$expired_days@" ./options.conf
  87. if [ "$content_bk" != '2' ]; then
  88. 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)"`
  89. while :; do echo
  90. echo "Please enter one or more name for database, separate multiple database names with commas: "
  91. read -p "(Default database: `echo $databases | tr ' ' ','`) " db_name
  92. db_name=`echo ${db_name} | tr -d ' '`
  93. [ -z "${db_name}" ] && db_name="`echo $databases | tr ' ' ','`"
  94. D_tmp=0
  95. for D in `echo ${db_name} | tr ',' ' '`
  96. do
  97. [ -z "`echo $databases | grep -w $D`" ] && { echo "${CWARNING}$D was not exist! ${CEND}" ; D_tmp=1; }
  98. done
  99. [ "$D_tmp" != '1' ] && break
  100. done
  101. sed -i "s@^db_name=.*@db_name=${db_name}@" ./options.conf
  102. fi
  103. if [ "$content_bk" != '1' ]; then
  104. websites=`ls ${wwwroot_dir} | grep -vw default`
  105. while :; do echo
  106. echo "Please enter one or more name for website, separate multiple website names with commas: "
  107. read -p "(Default website: `echo $websites | tr ' ' ','`) " website_name
  108. website_name=`echo ${website_name} | tr -d ' '`
  109. [ -z "${website_name}" ] && website_name="`echo $websites | tr ' ' ','`"
  110. W_tmp=0
  111. for W in `echo ${website_name} | tr ',' ' '`
  112. do
  113. [ ! -e "${wwwroot_dir}/$W" ] && { echo "${CWARNING}${wwwroot_dir}/$W not exist! ${CEND}" ; W_tmp=1; }
  114. done
  115. [ "$W_tmp" != '1' ] && break
  116. done
  117. sed -i "s@^website_name=.*@website_name=${website_name}@" ./options.conf
  118. fi
  119. echo
  120. echo "You have to backup the content:"
  121. [ "$content_bk" != '2' ] && echo "Database: ${CMSG}${db_name}${CEND}"
  122. [ "$content_bk" != '1' ] && echo "Website: ${CMSG}${website_name}${CEND}"
  123. if [ `echo $desc_bk | grep -e 2` ]; then
  124. > tools/iplist.txt
  125. while :; do echo
  126. read -p "Please enter the remote host ip: " remote_ip
  127. [ -z "$remote_ip" -o "$remote_ip" == '127.0.0.1' ] && continue
  128. echo
  129. read -p "Please enter the remote host port(Default: 22) : " remote_port
  130. [ -z "$remote_port" ] && remote_port=22
  131. echo
  132. read -p "Please enter the remote host user(Default: root) : " remote_user
  133. [ -z "$remote_user" ] && remote_user=root
  134. echo
  135. read -p "Please enter the remote host password: " remote_password
  136. IPcode=$(echo "ibase=16;$(echo "$remote_ip" | xxd -ps -u)"|bc|tr -d '\\'|tr -d '\n')
  137. Portcode=$(echo "ibase=16;$(echo "$remote_port" | xxd -ps -u)"|bc|tr -d '\\'|tr -d '\n')
  138. PWcode=$(echo "ibase=16;$(echo "$remote_password" | xxd -ps -u)"|bc|tr -d '\\'|tr -d '\n')
  139. [ -e "~/.ssh/known_hosts" ] && grep $remote_ip ~/.ssh/known_hosts | sed -i "/$remote_ip/d" ~/.ssh/known_hosts
  140. ./tools/mssh.exp ${IPcode}P $remote_user ${PWcode}P ${Portcode}P true 10
  141. if [ $? -eq 0 ]; then
  142. [ -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}"
  143. while :; do
  144. read -p "Do you want to add more host ? [y/n]: " more_host_yn
  145. if [[ ! $more_host_yn =~ ^[y,n]$ ]]; then
  146. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  147. else
  148. break
  149. fi
  150. done
  151. [ "$more_host_yn" == 'n' ] && break
  152. fi
  153. done
  154. fi
  155. if [ `echo $desc_bk | grep -e 3` ]; then
  156. [ ! -e "${python_install_dir}/bin/python" ] && Install_Python
  157. [ ! -e "${python_install_dir}/lib/coscmd" ] && ${python_install_dir}/bin/pip install coscmd >/dev/null 2>&1
  158. while :; do echo
  159. echo 'Please select your backup datacenter:'
  160. echo -e "\t ${CMSG}1${CEND}. 北京一区(华北) ${CMSG}2${CEND}. 北京"
  161. echo -e "\t ${CMSG}3${CEND}. 上海(华东) ${CMSG}4${CEND}. 广州(华南)"
  162. echo -e "\t ${CMSG}5${CEND}. 成都(西南) ${CMSG}6${CEND}. 新加坡"
  163. echo -e "\t ${CMSG}7${CEND}. 香港 ${CMSG}8${CEND}. 多伦多"
  164. echo -e "\t ${CMSG}9${CEND}. 法兰克福"
  165. read -p "Please input a number:(Default 1 press Enter) " Location
  166. [ -z "$Location" ] && Location=1
  167. if [ ${Location} -ge 1 >/dev/null 2>&1 -a ${Location} -le 9 >/dev/null 2>&1 ]; then
  168. break
  169. else
  170. echo "${CWARNING}input error! Please only input number 1~9${CEND}"
  171. fi
  172. done
  173. [ "$Location" == '1' ] && region='ap-beijing-1'
  174. [ "$Location" == '2' ] && region='ap-beijing'
  175. [ "$Location" == '3' ] && region='ap-shanghai'
  176. [ "$Location" == '4' ] && region='ap-guangzhou'
  177. [ "$Location" == '5' ] && region='ap-chengdu'
  178. [ "$Location" == '6' ] && region='ap-singapore'
  179. [ "$Location" == '7' ] && region='ap-hongkong'
  180. [ "$Location" == '8' ] && region='na-toronto'
  181. [ "$Location" == '9' ] && region='eu-frankfurt'
  182. while :; do echo
  183. read -p "Please enter the Qcloud COS APPID: " appid
  184. [ -z "$appid" ] && continue
  185. echo
  186. read -p "Please enter the Qcloud COS SecretId: " SecretId
  187. [ -z "$SecretId" ] && continue
  188. echo
  189. read -p "Please enter the Qcloud COS SecretKey: " SecretKey
  190. [ -z "$SecretKey" ] && continue
  191. echo
  192. read -p "Please enter the Qcloud COS bucket: " bucket
  193. [ -z "$bucket" ] && continue
  194. echo
  195. ${python_install_dir}/bin/coscmd config -u $appid -a $SecretId -s $SecretKey -r $region -b $bucket >/dev/null 2>&1
  196. ${python_install_dir}/bin/coscmd list >/dev/null 2>&1
  197. if [ $? = 0 ]; then
  198. echo "${CMSG}appid/SecretId/SecretKey/region/bucket OK${CEND}"
  199. echo
  200. break
  201. else
  202. echo "${CWARNING}input error! appid/SecretId/SecretKey/region/bucket invalid${CEND}"
  203. fi
  204. done
  205. fi
  206. if [ `echo $desc_bk | grep -e 4` ]; then
  207. if [ ! -e "/usr/local/bin/upx" ] ;then
  208. if [ "$OS_BIT" == '64' ]; then
  209. wget -qc http://collection.b0.upaiyun.com/softwares/upx/upx-linux-amd64-v0.2.3 -O /usr/local/bin/upx
  210. elif [ "$OS_BIT" == '32' ]; then
  211. wget -qc http://collection.b0.upaiyun.com/softwares/upx/upx-linux-386-v0.2.3 -O /usr/local/bin/upx
  212. fi
  213. chmod +x /usr/local/bin/upx
  214. fi
  215. while :; do echo
  216. read -p "Please enter the ServiceName: " ServiceName
  217. [ -z "$ServiceName" ] && continue
  218. echo
  219. read -p "Please enter the Operator: " Operator
  220. [ -z "$Operator" ] && continue
  221. echo
  222. read -p "Please enter the Password: " Password
  223. [ -z "$Password" ] && continue
  224. echo
  225. /usr/local/bin/upx login $ServiceName $Operator $Password >/dev/null 2>&1
  226. /usr/local/bin/upx ls >/dev/null 2>&1
  227. if [ $? = 0 ]; then
  228. echo "${CMSG}ServiceName/Operator/Password OK${CEND}"
  229. echo
  230. break
  231. else
  232. echo "${CWARNING}input error! ServiceName/Operator/Password invalid${CEND}"
  233. fi
  234. done
  235. fi