backup.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. #!/bin/bash
  2. # Author: yeho <lj2007331 AT gmail.com>
  3. # BLOG: https://linuxeye.com
  4. #
  5. # Notes: OneinStack for CentOS/RedHat 7+ Debian 9+ and Ubuntu 16+
  6. #
  7. # Project home page:
  8. # https://oneinstack.com
  9. # https://github.com/oneinstack/oneinstack
  10. # Check if user is root
  11. [ $(id -u) != "0" ] && { echo "${CFAILURE}Error: You must be root to run this script${CEND}"; exit 1; }
  12. oneinstack_dir=$(dirname "`readlink -f $0`")
  13. pushd ${oneinstack_dir}/tools > /dev/null
  14. . ../options.conf
  15. [ ! -e "${backup_dir}" ] && mkdir -p ${backup_dir}
  16. DB_Local_BK() {
  17. for D in `echo ${db_name} | tr ',' ' '`
  18. do
  19. ./db_bk.sh ${D}
  20. done
  21. }
  22. DB_Remote_BK() {
  23. for D in `echo ${db_name} | tr ',' ' '`
  24. do
  25. ./db_bk.sh ${D}
  26. DB_GREP="DB_${D}_`date +%Y%m%d`"
  27. DB_FILE=`ls -lrt ${backup_dir} | grep ${DB_GREP} | tail -1 | awk '{print $NF}'`
  28. echo "file:::${backup_dir}/${DB_FILE} ${backup_dir} push" >> config_backup.txt
  29. echo "com:::[ -e "${backup_dir}/${DB_FILE}" ] && rm -rf ${backup_dir}/DB_${D}_$(date +%Y%m%d --date="${expired_days} days ago")_*.tgz" >> config_backup.txt
  30. done
  31. }
  32. DB_OSS_BK() {
  33. for D in `echo ${db_name} | tr ',' ' '`
  34. do
  35. ./db_bk.sh ${D}
  36. DB_GREP="DB_${D}_`date +%Y%m%d`"
  37. DB_FILE=`ls -lrt ${backup_dir} | grep ${DB_GREP} | tail -1 | awk '{print $NF}'`
  38. /usr/local/bin/ossutil cp -f ${backup_dir}/${DB_FILE} oss://${oss_bucket}/`date +%F`/${DB_FILE}
  39. if [ $? -eq 0 ]; then
  40. /usr/local/bin/ossutil rm -rf oss://${oss_bucket}/`date +%F --date="${expired_days} days ago"`/
  41. [ -z "`echo ${backup_destination} | grep -ow 'local'`" ] && rm -f ${backup_dir}/${DB_FILE}
  42. fi
  43. done
  44. }
  45. DB_COS_BK() {
  46. for D in `echo ${db_name} | tr ',' ' '`
  47. do
  48. ./db_bk.sh ${D}
  49. DB_GREP="DB_${D}_`date +%Y%m%d`"
  50. DB_FILE=`ls -lrt ${backup_dir} | grep ${DB_GREP} | tail -1 | awk '{print $NF}'`
  51. ${python_install_dir}/bin/coscmd upload ${backup_dir}/${DB_FILE} /`date +%F`/${DB_FILE}
  52. if [ $? -eq 0 ]; then
  53. ${python_install_dir}/bin/coscmd delete -r -f `date +%F --date="${expired_days} days ago"` > /dev/null 2>&1
  54. [ -z "`echo ${backup_destination} | grep -ow 'local'`" ] && rm -f ${backup_dir}/${DB_FILE}
  55. fi
  56. done
  57. }
  58. DB_UPYUN_BK() {
  59. for D in `echo ${db_name} | tr ',' ' '`
  60. do
  61. ./db_bk.sh ${D}
  62. DB_GREP="DB_${D}_`date +%Y%m%d`"
  63. DB_FILE=`ls -lrt ${backup_dir} | grep ${DB_GREP} | tail -1 | awk '{print $NF}'`
  64. /usr/local/bin/upx put ${backup_dir}/${DB_FILE} /`date +%F`/${DB_FILE}
  65. if [ $? -eq 0 ]; then
  66. /usr/local/bin/upx rm -a `date +%F --date="${expired_days} days ago"` > /dev/null 2>&1
  67. [ -z "`echo ${backup_destination} | grep -ow 'local'`" ] && rm -f ${backup_dir}/${DB_FILE}
  68. fi
  69. done
  70. }
  71. DB_QINIU_BK() {
  72. for D in `echo ${db_name} | tr ',' ' '`
  73. do
  74. ./db_bk.sh ${D}
  75. DB_GREP="DB_${D}_`date +%Y%m%d`"
  76. DB_FILE=`ls -lrt ${backup_dir} | grep ${DB_GREP} | tail -1 | awk '{print $NF}'`
  77. /usr/local/bin/qshell rput ${qiniu_bucket} /`date +%F`/${DB_FILE} ${backup_dir}/${DB_FILE}
  78. if [ $? -eq 0 ]; then
  79. /usr/local/bin/qshell listbucket ${qiniu_bucket} /`date +%F --date="${expired_days} days ago"` /tmp/qiniu.txt > /dev/null 2>&1
  80. /usr/local/bin/qshell batchdelete -force ${qiniu_bucket} /tmp/qiniu.txt > /dev/null 2>&1
  81. [ -z "`echo ${backup_destination} | grep -ow 'local'`" ] && rm -f ${backup_dir}/${DB_FILE}
  82. rm -f /tmp/qiniu.txt
  83. fi
  84. done
  85. }
  86. DB_S3_BK() {
  87. for D in `echo ${db_name} | tr ',' ' '`
  88. do
  89. ./db_bk.sh ${D}
  90. DB_GREP="DB_${D}_`date +%Y%m%d`"
  91. DB_FILE=`ls -lrt ${backup_dir} | grep ${DB_GREP} | tail -1 | awk '{print $NF}'`
  92. ${python_install_dir}/bin/s3cmd put ${backup_dir}/${DB_FILE} s3://${s3_bucket}/`date +%F`/${DB_FILE}
  93. if [ $? -eq 0 ]; then
  94. ${python_install_dir}/bin/s3cmd rm -r s3://${s3_bucket}/`date +%F --date="${expired_days} days ago"` > /dev/null 2>&1
  95. [ -z "`echo ${backup_destination} | grep -ow 'local'`" ] && rm -f ${backup_dir}/${DB_FILE}
  96. fi
  97. done
  98. }
  99. DB_DROPBOX_BK() {
  100. for D in `echo ${db_name} | tr ',' ' '`
  101. do
  102. ./db_bk.sh ${D}
  103. DB_GREP="DB_${D}_`date +%Y%m%d`"
  104. DB_FILE=`ls -lrt ${backup_dir} | grep ${DB_GREP} | tail -1 | awk '{print $NF}'`
  105. /usr/local/bin/dbxcli put ${backup_dir}/${DB_FILE} `date +%F`/${DB_FILE}
  106. if [ $? -eq 0 ]; then
  107. /usr/local/bin/dbxcli rm -f `date +%F --date="${expired_days} days ago"` > /dev/null 2>&1
  108. [ -z "`echo ${backup_destination} | grep -ow 'local'`" ] && rm -f ${backup_dir}/${DB_FILE}
  109. fi
  110. done
  111. }
  112. WEB_LOCAL_BK() {
  113. for W in `echo ${website_name} | tr ',' ' '`
  114. do
  115. ./website_bk.sh $W
  116. done
  117. }
  118. WEB_Remote_BK() {
  119. for W in `echo ${website_name} | tr ',' ' '`
  120. do
  121. if [ `du -sm "${wwwroot_dir}/${WebSite}" | awk '{print $1}'` -lt 2048 ]; then
  122. ./website_bk.sh $W
  123. Web_GREP="Web_${W}_`date +%Y%m%d`"
  124. Web_FILE=`ls -lrt ${backup_dir} | grep ${Web_GREP} | tail -1 | awk '{print $NF}'`
  125. echo "file:::${backup_dir}/${Web_FILE} ${backup_dir} push" >> config_backup.txt
  126. echo "com:::[ -e "${backup_dir}/${Web_FILE}" ] && rm -rf ${backup_dir}/Web_${W}_$(date +%Y%m%d --date="${expired_days} days ago")_*.tgz" >> config_backup.txt
  127. else
  128. echo "file:::${wwwroot_dir}/$W ${backup_dir} push" >> config_backup.txt
  129. fi
  130. done
  131. }
  132. WEB_OSS_BK() {
  133. for W in `echo $website_name | tr ',' ' '`
  134. do
  135. [ ! -e "${wwwroot_dir}/${WebSite}" ] && { echo "[${wwwroot_dir}/${WebSite}] not exist"; break; }
  136. PUSH_FILE="${backup_dir}/Web_${W}_$(date +%Y%m%d_%H).tgz"
  137. if [ ! -e "${PUSH_FILE}" ]; then
  138. pushd ${wwwroot_dir} > /dev/null
  139. tar czf ${PUSH_FILE} ./$W
  140. popd > /dev/null
  141. fi
  142. /usr/local/bin/ossutil cp -f ${PUSH_FILE} oss://${oss_bucket}/`date +%F`/${PUSH_FILE##*/}
  143. if [ $? -eq 0 ]; then
  144. /usr/local/bin/ossutil rm -rf oss://${oss_bucket}/`date +%F --date="${expired_days} days ago"`/
  145. [ -z "`echo ${backup_destination} | grep -ow 'local'`" ] && rm -f ${PUSH_FILE}
  146. fi
  147. done
  148. }
  149. WEB_COS_BK() {
  150. for W in `echo ${website_name} | tr ',' ' '`
  151. do
  152. [ ! -e "${wwwroot_dir}/${WebSite}" ] && { echo "[${wwwroot_dir}/${WebSite}] not exist"; break; }
  153. PUSH_FILE="${backup_dir}/Web_${W}_$(date +%Y%m%d_%H).tgz"
  154. if [ ! -e "${PUSH_FILE}" ]; then
  155. pushd ${wwwroot_dir} > /dev/null
  156. tar czf ${PUSH_FILE} ./$W
  157. popd > /dev/null
  158. fi
  159. ${python_install_dir}/bin/coscmd upload ${PUSH_FILE} /`date +%F`/${PUSH_FILE##*/}
  160. if [ $? -eq 0 ]; then
  161. ${python_install_dir}/bin/coscmd delete -r -f `date +%F --date="${expired_days} days ago"` > /dev/null 2>&1
  162. [ -z "`echo ${backup_destination} | grep -ow 'local'`" ] && rm -f ${PUSH_FILE}
  163. fi
  164. done
  165. }
  166. WEB_UPYUN_BK() {
  167. for W in `echo ${website_name} | tr ',' ' '`
  168. do
  169. [ ! -e "${wwwroot_dir}/${WebSite}" ] && { echo "[${wwwroot_dir}/${WebSite}] not exist"; break; }
  170. [ ! -e "${backup_dir}" ] && mkdir -p ${backup_dir}
  171. PUSH_FILE="${backup_dir}/Web_${W}_$(date +%Y%m%d_%H).tgz"
  172. if [ ! -e "${PUSH_FILE}" ]; then
  173. pushd ${wwwroot_dir} > /dev/null
  174. tar czf ${PUSH_FILE} ./$W
  175. popd > /dev/null
  176. fi
  177. /usr/local/bin/upx put ${PUSH_FILE} /`date +%F`/${PUSH_FILE##*/}
  178. if [ $? -eq 0 ]; then
  179. /usr/local/bin/upx rm -a `date +%F --date="${expired_days} days ago"` > /dev/null 2>&1
  180. [ -z "`echo ${backup_destination} | grep -ow 'local'`" ] && rm -f ${PUSH_FILE}
  181. fi
  182. done
  183. }
  184. WEB_QINIU_BK() {
  185. for W in `echo ${website_name} | tr ',' ' '`
  186. do
  187. [ ! -e "${wwwroot_dir}/${WebSite}" ] && { echo "[${wwwroot_dir}/${WebSite}] not exist"; break; }
  188. [ ! -e "${backup_dir}" ] && mkdir -p ${backup_dir}
  189. PUSH_FILE="${backup_dir}/Web_${W}_$(date +%Y%m%d_%H).tgz"
  190. if [ ! -e "${PUSH_FILE}" ]; then
  191. pushd ${wwwroot_dir} > /dev/null
  192. tar czf ${PUSH_FILE} ./$W
  193. popd > /dev/null
  194. fi
  195. /usr/local/bin/qshell rput ${qiniu_bucket} /`date +%F`/${PUSH_FILE##*/} ${PUSH_FILE}
  196. if [ $? -eq 0 ]; then
  197. /usr/local/bin/qshell listbucket ${qiniu_bucket} /`date +%F --date="${expired_days} days ago"` /tmp/qiniu.txt > /dev/null 2>&1
  198. /usr/local/bin/qshell batchdelete -force ${qiniu_bucket} /tmp/qiniu.txt > /dev/null 2>&1
  199. [ -z "`echo ${backup_destination} | grep -ow 'local'`" ] && rm -f ${PUSH_FILE}
  200. rm -f /tmp/qiniu.txt
  201. fi
  202. done
  203. }
  204. WEB_S3_BK() {
  205. for W in `echo ${website_name} | tr ',' ' '`
  206. do
  207. [ ! -e "${wwwroot_dir}/${WebSite}" ] && { echo "[${wwwroot_dir}/${WebSite}] not exist"; break; }
  208. [ ! -e "${backup_dir}" ] && mkdir -p ${backup_dir}
  209. PUSH_FILE="${backup_dir}/Web_${W}_$(date +%Y%m%d_%H).tgz"
  210. if [ ! -e "${PUSH_FILE}" ]; then
  211. pushd ${wwwroot_dir} > /dev/null
  212. tar czf ${PUSH_FILE} ./$W
  213. popd > /dev/null
  214. fi
  215. ${python_install_dir}/bin/s3cmd put ${PUSH_FILE} s3://${s3_bucket}/`date +%F`/${PUSH_FILE##*/}
  216. if [ $? -eq 0 ]; then
  217. ${python_install_dir}/bin/s3cmd rm -r s3://${s3_bucket}/`date +%F --date="${expired_days} days ago"` > /dev/null 2>&1
  218. [ -z "`echo ${backup_destination} | grep -ow 'local'`" ] && rm -f ${PUSH_FILE}
  219. fi
  220. done
  221. }
  222. WEB_DROPBOX_BK() {
  223. for W in `echo ${website_name} | tr ',' ' '`
  224. do
  225. [ ! -e "${wwwroot_dir}/${WebSite}" ] && { echo "[${wwwroot_dir}/${WebSite}] not exist"; break; }
  226. [ ! -e "${backup_dir}" ] && mkdir -p ${backup_dir}
  227. PUSH_FILE="${backup_dir}/Web_${W}_$(date +%Y%m%d_%H).tgz"
  228. if [ ! -e "${PUSH_FILE}" ]; then
  229. pushd ${wwwroot_dir} > /dev/null
  230. tar czf ${PUSH_FILE} ./$W
  231. popd > /dev/null
  232. fi
  233. /usr/local/bin/dbxcli put ${PUSH_FILE} `date +%F`/${PUSH_FILE##*/}
  234. if [ $? -eq 0 ]; then
  235. /usr/local/bin/dbxcli rm -f `date +%F --date="${expired_days} days ago"` > /dev/null 2>&1
  236. [ -z "`echo ${backup_destination} | grep -ow 'local'`" ] && rm -f ${PUSH_FILE}
  237. fi
  238. done
  239. }
  240. for DEST in `echo ${backup_destination} | tr ',' ' '`
  241. do
  242. if [ "${DEST}" == 'local' ]; then
  243. [ -n "`echo ${backup_content} | grep -ow db`" ] && DB_Local_BK
  244. [ -n "`echo ${backup_content} | grep -ow web`" ] && WEB_LOCAL_BK
  245. fi
  246. if [ "${DEST}" == 'remote' ]; then
  247. echo "com:::[ ! -e "${backup_dir}" ] && mkdir -p ${backup_dir}" > config_backup.txt
  248. [ -n "`echo ${backup_content} | grep -ow db`" ] && DB_Remote_BK
  249. [ -n "`echo ${backup_content} | grep -ow web`" ] && WEB_Remote_BK
  250. ./mabs.sh -c config_backup.txt -T -1 | tee -a mabs.log
  251. fi
  252. if [ "${DEST}" == 'oss' ]; then
  253. [ -n "`echo ${backup_content} | grep -ow db`" ] && DB_OSS_BK
  254. [ -n "`echo ${backup_content} | grep -ow web`" ] && WEB_OSS_BK
  255. fi
  256. if [ "${DEST}" == 'cos' ]; then
  257. [ -n "`echo ${backup_content} | grep -ow db`" ] && DB_COS_BK
  258. [ -n "`echo ${backup_content} | grep -ow web`" ] && WEB_COS_BK
  259. fi
  260. if [ "${DEST}" == 'upyun' ]; then
  261. [ -n "`echo ${backup_content} | grep -ow db`" ] && DB_UPYUN_BK
  262. [ -n "`echo ${backup_content} | grep -ow web`" ] && WEB_UPYUN_BK
  263. fi
  264. if [ "${DEST}" == 'qiniu' ]; then
  265. [ -n "`echo ${backup_content} | grep -ow db`" ] && DB_QINIU_BK
  266. [ -n "`echo ${backup_content} | grep -ow web`" ] && WEB_QINIU_BK
  267. fi
  268. if [ "${DEST}" == 's3' ]; then
  269. [ -n "`echo ${backup_content} | grep -ow db`" ] && DB_S3_BK
  270. [ -n "`echo ${backup_content} | grep -ow web`" ] && WEB_S3_BK
  271. fi
  272. if [ "${DEST}" == 'dropbox' ]; then
  273. [ -n "`echo ${backup_content} | grep -ow db`" ] && DB_DROPBOX_BK
  274. [ -n "`echo ${backup_content} | grep -ow web`" ] && WEB_DROPBOX_BK
  275. fi
  276. done