backup_setup.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/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}. Aliyun OSS"
  35. echo -e "\t${CMSG}4${CEND}. Qcloud COS"
  36. echo -e "\t${CMSG}5${CEND}. UPYUN(又拍云)"
  37. read -p "Please input a number:(Default 1 press Enter) " desc_bk
  38. [ -z "${desc_bk}" ] && desc_bk=1
  39. ary=(1 2 3 4 5 12 13 14 15 23 24 25 34 35 45 123 124 125 134 135 145 234 235 245 345 1234 1235 2345 12345)
  40. if [[ "${ary[@]}" =~ "${desc_bk}" ]]; then
  41. break
  42. else
  43. echo "${CWARNING}input error! Please only input number 1,2,12,23,234 and so on${CEND}"
  44. fi
  45. done
  46. sed -i 's@^backup_destination=.*@backup_destination=@' ./options.conf
  47. [ `echo ${desc_bk} | grep -e 1` ] && sed -i 's@^backup_destination=.*@backup_destination=local@' ./options.conf
  48. [ `echo ${desc_bk} | grep -e 2` ] && sed -i 's@^backup_destination=.*@&,remote@' ./options.conf
  49. [ `echo ${desc_bk} | grep -e 3` ] && sed -i 's@^backup_destination=.*@&,oss@' ./options.conf
  50. [ `echo ${desc_bk} | grep -e 4` ] && sed -i 's@^backup_destination=.*@&,cos@' ./options.conf
  51. [ `echo ${desc_bk} | grep -e 5` ] && sed -i 's@^backup_destination=.*@&,upyun@' ./options.conf
  52. sed -i 's@^backup_destination=,@backup_destination=@' ./options.conf
  53. while :; do echo
  54. echo 'Please select your backup content:'
  55. echo -e "\t${CMSG}1${CEND}. Only Database"
  56. echo -e "\t${CMSG}2${CEND}. Only Website"
  57. echo -e "\t${CMSG}3${CEND}. Database and Website"
  58. read -p "Please input a number:(Default 1 press Enter) " content_bk
  59. [ -z "${content_bk}" ] && content_bk=1
  60. if [[ ! ${content_bk} =~ ^[1-3]$ ]]; then
  61. echo "${CWARNING}input error! Please only input number 1~3${CEND}"
  62. else
  63. break
  64. fi
  65. done
  66. [ "${content_bk}" == '1' ] && sed -i 's@^backup_content=.*@backup_content=db@' ./options.conf
  67. [ "${content_bk}" == '2' ] && sed -i 's@^backup_content=.*@backup_content=web@' ./options.conf
  68. [ "${content_bk}" == '3' ] && sed -i 's@^backup_content=.*@backup_content=db,web@' ./options.conf
  69. if [[ ${desc_bk} =~ ^[1,2]$ ]]; then
  70. while :; do echo
  71. echo "Please enter the directory for save the backup file: "
  72. read -p "(Default directory: ${backup_dir}): " new_backup_dir
  73. [ -z "${new_backup_dir}" ] && new_backup_dir="${backup_dir}"
  74. if [ -z "`echo ${new_backup_dir}| grep '^/'`" ]; then
  75. echo "${CWARNING}input error! ${CEND}"
  76. else
  77. break
  78. fi
  79. done
  80. sed -i "s@^backup_dir=.*@backup_dir=${new_backup_dir}@" ./options.conf
  81. fi
  82. while :; do echo
  83. echo "Pleas enter a valid backup number of days: "
  84. read -p "(Default days: 5): " expired_days
  85. [ -z "${expired_days}" ] && expired_days=5
  86. [ -n "`echo ${expired_days} | sed -n "/^[0-9]\+$/p"`" ] && break || echo "${CWARNING}input error! Please only enter numbers! ${CEND}"
  87. done
  88. sed -i "s@^expired_days=.*@expired_days=${expired_days}@" ./options.conf
  89. if [ "${content_bk}" != '2' ]; then
  90. 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)"`
  91. while :; do echo
  92. echo "Please enter one or more name for database, separate multiple database names with commas: "
  93. read -p "(Default database: `echo $databases | tr ' ' ','`) " db_name
  94. db_name=`echo ${db_name} | tr -d ' '`
  95. [ -z "${db_name}" ] && db_name="`echo $databases | tr ' ' ','`"
  96. D_tmp=0
  97. for D in `echo ${db_name} | tr ',' ' '`
  98. do
  99. [ -z "`echo $databases | grep -w $D`" ] && { echo "${CWARNING}$D was not exist! ${CEND}" ; D_tmp=1; }
  100. done
  101. [ "$D_tmp" != '1' ] && break
  102. done
  103. sed -i "s@^db_name=.*@db_name=${db_name}@" ./options.conf
  104. fi
  105. if [ "${content_bk}" != '1' ]; then
  106. websites=`ls ${wwwroot_dir}`
  107. while :; do echo
  108. echo "Please enter one or more name for website, separate multiple website names with commas: "
  109. read -p "(Default website: `echo $websites | tr ' ' ','`) " website_name
  110. website_name=`echo ${website_name} | tr -d ' '`
  111. [ -z "${website_name}" ] && website_name="`echo $websites | tr ' ' ','`"
  112. W_tmp=0
  113. for W in `echo ${website_name} | tr ',' ' '`
  114. do
  115. [ ! -e "${wwwroot_dir}/$W" ] && { echo "${CWARNING}${wwwroot_dir}/$W not exist! ${CEND}" ; W_tmp=1; }
  116. done
  117. [ "$W_tmp" != '1' ] && break
  118. done
  119. sed -i "s@^website_name=.*@website_name=${website_name}@" ./options.conf
  120. fi
  121. echo
  122. echo "You have to backup the content:"
  123. [ "${content_bk}" != '2' ] && echo "Database: ${CMSG}${db_name}${CEND}"
  124. [ "${content_bk}" != '1' ] && echo "Website: ${CMSG}${website_name}${CEND}"
  125. if [ `echo ${desc_bk} | grep -e 2` ]; then
  126. > tools/iplist.txt
  127. while :; do echo
  128. read -p "Please enter the remote host ip: " remote_ip
  129. [ -z "${remote_ip}" -o "${remote_ip}" == '127.0.0.1' ] && continue
  130. echo
  131. read -p "Please enter the remote host port(Default: 22) : " remote_port
  132. [ -z "${remote_port}" ] && remote_port=22
  133. echo
  134. read -p "Please enter the remote host user(Default: root) : " remote_user
  135. [ -z "${remote_user}" ] && remote_user=root
  136. echo
  137. read -p "Please enter the remote host password: " remote_password
  138. IPcode=$(echo "ibase=16;$(echo "${remote_ip}" | xxd -ps -u)"|bc|tr -d '\\'|tr -d '\n')
  139. Portcode=$(echo "ibase=16;$(echo "${remote_port}" | xxd -ps -u)"|bc|tr -d '\\'|tr -d '\n')
  140. PWcode=$(echo "ibase=16;$(echo "$remote_password" | xxd -ps -u)"|bc|tr -d '\\'|tr -d '\n')
  141. [ -e "~/.ssh/known_hosts" ] && grep ${remote_ip} ~/.ssh/known_hosts | sed -i "/${remote_ip}/d" ~/.ssh/known_hosts
  142. ./tools/mssh.exp ${IPcode}P ${remote_user} ${PWcode}P ${Portcode}P true 10
  143. if [ $? -eq 0 ]; then
  144. [ -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}"
  145. while :; do
  146. read -p "Do you want to add more host ? [y/n]: " morehost_yn
  147. if [[ ! ${morehost_yn} =~ ^[y,n]$ ]]; then
  148. echo "${CWARNING}input error! Please only input 'y' or 'n'${CEND}"
  149. else
  150. break
  151. fi
  152. done
  153. [ "${morehost_yn}" == 'n' ] && break
  154. fi
  155. done
  156. fi
  157. if [ `echo ${desc_bk} | grep -e 3` ]; then
  158. if [ ! -e "/usr/local/bin/ossutil" ]; then
  159. if [ "${OS_BIT}" == '64' ]; then
  160. wget -qc http://docs-aliyun.cn-hangzhou.oss.aliyun-inc.com/assets/attach/50452/cn_zh/1516454058701/ossutil64 -O /usr/local/bin/ossutil
  161. elif [ "${OS_BIT}" == '32' ]; then
  162. wget -qc http://docs-aliyun.cn-hangzhou.oss.aliyun-inc.com/assets/attach/50452/cn_zh/1516453988016/ossutil32 -O /usr/local/bin/ossutil
  163. fi
  164. chmod +x /usr/local/bin/ossutil
  165. fi
  166. while :; do echo
  167. echo 'Please select your backup datacenter:'
  168. echo -e "\t ${CMSG}1${CEND}. cn-hangzhou-华东 1 (杭州) ${CMSG}2${CEND}. cn-shanghai-华东 2 (上海)"
  169. echo -e "\t ${CMSG}3${CEND}. cn-qingdao-华北 1 (青岛) ${CMSG}4${CEND}. cn-beijing-华北 2 (北京)"
  170. echo -e "\t ${CMSG}5${CEND}. cn-zhangjiakou-华北 3 (张家口) ${CMSG}6${CEND}. cn-huhehaote-华北 5(呼和浩特)"
  171. echo -e "\t ${CMSG}7${CEND}. cn-shenzhen-华南 1 (深圳) ${CMSG}8${CEND}. cn-hongkong-香港"
  172. echo -e "\t ${CMSG}9${CEND}. us-west-美西 1 (硅谷) ${CMSG}10${CEND}. us-east-美东 1 (弗吉尼亚)"
  173. echo -e "\t${CMSG}11${CEND}. ap-southeast-亚太东南 1 (新加坡) ${CMSG}12${CEND}. ap-southeast-亚太东南 2 (悉尼)"
  174. echo -e "\t${CMSG}13${CEND}. ap-southeast-亚太东南 3 (吉隆坡) ${CMSG}14${CEND}. ap-northeast-亚太东北 1 (日本)"
  175. echo -e "\t${CMSG}15${CEND}. eu-central-欧洲中部 1 (法兰克福) ${CMSG}16${CEND}. me-east-中东东部 1 (迪拜)"
  176. read -p "Please input a number:(Default 1 press Enter) " Location
  177. [ -z "${Location}" ] && Location=1
  178. if [[ "${Location}" =~ ^[1-9]$|^1[0-6]$ ]]; then
  179. break
  180. else
  181. echo "${CWARNING}input error! Please only input number 1~16${CEND}"
  182. fi
  183. done
  184. [ "${Location}" == '1' ] && Host=oss-cn-hangzhou-internal.aliyuncs.com
  185. [ "${Location}" == '2' ] && Host=oss-cn-shanghai-internal.aliyuncs.com
  186. [ "${Location}" == '3' ] && Host=oss-cn-qingdao-internal.aliyuncs.com
  187. [ "${Location}" == '4' ] && Host=oss-cn-beijing-internal.aliyuncs.com
  188. [ "${Location}" == '5' ] && Host=oss-cn-zhangjiakou-internal.aliyuncs.com
  189. [ "${Location}" == '6' ] && Host=oss-cn-huhehaote-internal.aliyuncs.com
  190. [ "${Location}" == '7' ] && Host=oss-cn-shenzhen-internal.aliyuncs.com
  191. [ "${Location}" == '8' ] && Host=oss-cn-hongkong-internal.aliyuncs.com
  192. [ "${Location}" == '9' ] && Host=oss-us-west-1-internal.aliyuncs.com
  193. [ "${Location}" == '10' ] && Host=oss-us-east-1-internal.aliyuncs.com
  194. [ "${Location}" == '11' ] && Host=oss-ap-southeast-1-internal.aliyuncs.com
  195. [ "${Location}" == '12' ] && Host=oss-ap-southeast-2-internal.aliyuncs.com
  196. [ "${Location}" == '13' ] && Host=oss-ap-southeast-3-internal.aliyuncs.com
  197. [ "${Location}" == '14' ] && Host=oss-ap-northeast-1-internal.aliyuncs.com
  198. [ "${Location}" == '15' ] && Host=oss-eu-central-1-internal.aliyuncs.com
  199. [ "${Location}" == '16' ] && Host=oss-me-east-1-internal.aliyuncs.com
  200. [ "$(./include/check_port.py ${Host} 80)" == "False" ] && Host=`echo ${Host} | sed 's@-internal@@g'`
  201. [ -e "/root/.ossutilconfig" ] && rm -f /root/.ossutilconfig
  202. while :; do echo
  203. read -p "Please enter the aliyun oss Access Key ID: " KeyID
  204. [ -z "${KeyID}" ] && continue
  205. echo
  206. read -p "Please enter the aliyun oss Access Key Secret: " KeySecret
  207. [ -z "${KeySecret}" ] && continue
  208. /usr/local/bin/ossutil ls -e ${Host} -i ${KeyID} -k ${KeySecret} >/dev/null 2>&1
  209. if [ $? -eq 0 ];then
  210. /usr/local/bin/ossutil config -e ${Host} -i ${KeyID} -k ${KeySecret} >/dev/null 2>&1
  211. while :; do echo
  212. read -p "Please enter the aliyun oss bucket: " Bucket
  213. /usr/local/bin/ossutil mb oss://${Bucket} >/dev/null 2>&1
  214. [ $? -eq 0 ] && { echo "${CMSG}[${Bucket}] createbucket OK${CEND}"; sed -i "s@^oss_bucket=.*@oss_bucket=${Bucket}@" ./options.conf; break; } || echo "${CWARNING}[${Bucket}] already exists, You need to use the OSS Console to create a bucket for storing.${CEND}"
  215. done
  216. break
  217. fi
  218. done
  219. fi
  220. if [ `echo ${desc_bk} | grep -e 4` ]; then
  221. [ ! -e "${python_install_dir}/bin/python" ] && Install_Python
  222. [ ! -e "${python_install_dir}/lib/coscmd" ] && ${python_install_dir}/bin/pip install coscmd >/dev/null 2>&1
  223. while :; do echo
  224. echo 'Please select your backup datacenter:'
  225. echo -e "\t ${CMSG}1${CEND}. 北京一区(华北) ${CMSG}2${CEND}. 北京"
  226. echo -e "\t ${CMSG}3${CEND}. 上海(华东) ${CMSG}4${CEND}. 广州(华南)"
  227. echo -e "\t ${CMSG}5${CEND}. 成都(西南) ${CMSG}6${CEND}. 新加坡"
  228. echo -e "\t ${CMSG}7${CEND}. 香港 ${CMSG}8${CEND}. 多伦多"
  229. echo -e "\t ${CMSG}9${CEND}. 法兰克福"
  230. read -p "Please input a number:(Default 1 press Enter) " Location
  231. [ -z "${Location}" ] && Location=1
  232. if [[ "${Location}" =~ ^[1-9]$ ]]; then
  233. break
  234. else
  235. echo "${CWARNING}input error! Please only input number 1~9${CEND}"
  236. fi
  237. done
  238. [ "${Location}" == '1' ] && region='ap-beijing-1'
  239. [ "${Location}" == '2' ] && region='ap-beijing'
  240. [ "${Location}" == '3' ] && region='ap-shanghai'
  241. [ "${Location}" == '4' ] && region='ap-guangzhou'
  242. [ "${Location}" == '5' ] && region='ap-chengdu'
  243. [ "${Location}" == '6' ] && region='ap-singapore'
  244. [ "${Location}" == '7' ] && region='ap-hongkong'
  245. [ "${Location}" == '8' ] && region='na-toronto'
  246. [ "${Location}" == '9' ] && region='eu-frankfurt'
  247. while :; do echo
  248. read -p "Please enter the Qcloud COS APPID: " APPID
  249. [ -z "${APPID}" ] && continue
  250. echo
  251. read -p "Please enter the Qcloud COS SecretId: " SecretId
  252. [ -z "${SecretId}" ] && continue
  253. echo
  254. read -p "Please enter the Qcloud COS SecretKey: " SecretKey
  255. [ -z "$SecretKey" ] && continue
  256. echo
  257. read -p "Please enter the Qcloud COS bucket: " bucket
  258. [ -z "${bucket}" ] && continue
  259. echo
  260. ${python_install_dir}/bin/coscmd config -u ${APPID} -a ${SecretId} -s $SecretKey -r $region -b ${bucket} >/dev/null 2>&1
  261. ${python_install_dir}/bin/coscmd list >/dev/null 2>&1
  262. if [ $? = 0 ]; then
  263. echo "${CMSG}APPID/SecretId/SecretKey/region/bucket OK${CEND}"
  264. echo
  265. break
  266. else
  267. echo "${CWARNING}input error! APPID/SecretId/SecretKey/region/bucket invalid${CEND}"
  268. fi
  269. done
  270. fi
  271. if [ `echo ${desc_bk} | grep -e 5` ]; then
  272. if [ ! -e "/usr/local/bin/upx" ]; then
  273. if [ "${OS_BIT}" == '64' ]; then
  274. wget -qc http://collection.b0.upaiyun.com/softwares/upx/upx-linux-amd64-v0.2.3 -O /usr/local/bin/upx
  275. elif [ "${OS_BIT}" == '32' ]; then
  276. wget -qc http://collection.b0.upaiyun.com/softwares/upx/upx-linux-386-v0.2.3 -O /usr/local/bin/upx
  277. fi
  278. chmod +x /usr/local/bin/upx
  279. fi
  280. while :; do echo
  281. read -p "Please enter the ServiceName: " ServiceName
  282. [ -z "${ServiceName}" ] && continue
  283. echo
  284. read -p "Please enter the Operator: " Operator
  285. [ -z "${Operator}" ] && continue
  286. echo
  287. read -p "Please enter the Password: " Password
  288. [ -z "${Password}" ] && continue
  289. echo
  290. /usr/local/bin/upx login ${ServiceName} ${Operator} ${Password} >/dev/null 2>&1
  291. if [ $? = 0 ]; then
  292. echo "${CMSG}ServiceName/Operator/Password OK${CEND}"
  293. echo
  294. break
  295. else
  296. echo "${CWARNING}input error! ServiceName/Operator/Password invalid${CEND}"
  297. fi
  298. done
  299. fi