Browse Source

Add qiniu backup

lj2007331@gmail.com 6 years ago
parent
commit
ee80546faf
6 changed files with 124 additions and 11 deletions
  1. 1 1
      README.md
  2. 39 0
      backup.sh
  3. 77 7
      backup_setup.sh
  4. 3 0
      options.conf
  5. 1 1
      versions.txt
  6. 3 2
      vhost.sh

+ 1 - 1
README.md

@@ -18,7 +18,7 @@ Script properties:
 - Jemalloc optimize MySQL, Nginx
 - Providing add a virtual host script, include Let's Encrypt SSL certificate
 - Provide Nginx/Tengine/OpenResty/Apache, MySQL/MariaDB/Percona, PHP, Redis, Memcached, phpMyAdmin upgrade script
-- Provide local backup,remote backup (rsync between servers),Aliyun OSS,Qcloud COS and UPYUN script
+- Provide local backup,remote backup (rsync between servers),Aliyun OSS,Qcloud COS,UPYUN and QINIU script
 - Provided under HHVM install CentOS 6,7
 
 ## How to use

+ 39 - 0
backup.sh

@@ -66,6 +66,21 @@ DB_UPYUN_BK() {
   done
 }
 
+DB_QINIU_BK() {
+  for D in `echo ${db_name} | tr ',' ' '`
+  do
+    ./db_bk.sh ${D}
+    DB_GREP="DB_${D}_`date +%Y%m%d`"
+    DB_FILE=`ls -lrt ${backup_dir} | grep ${DB_GREP} | tail -1 | awk '{print $NF}'`
+    /usr/local/bin/qshell rput ${qiniu_bucket} /`date +%F`/${DB_FILE} ${backup_dir}/${DB_FILE}
+    if [ $? -eq 0 ]; then
+      /usr/local/bin/qshell listbucket ${qiniu_bucket} /`date +%F --date="${expired_days} days ago"` /tmp/qiniu.txt > /dev/null 2>&1
+      /usr/local/bin/qshell batchdelete -force ${qiniu_bucket} /tmp/qiniu.txt > /dev/null 2>&1
+      rm -f /tmp/qiniu.txt
+    fi
+  done
+}
+
 WEB_Local_BK() {
   for W in `echo ${website_name} | tr ',' ' '`
   do
@@ -140,6 +155,26 @@ WEB_UPYUN_BK() {
   done
 }
 
+WEB_QINIU_BK() {
+  for W in `echo ${website_name} | tr ',' ' '`
+  do
+    [ ! -e "${wwwroot_dir}/$WebSite" ] && { echo "[${wwwroot_dir}/$WebSite] not exist"; break; }
+    [ ! -e "${backup_dir}" ] && mkdir -p ${backup_dir}
+    PUSH_FILE="${backup_dir}/Web_${W}_$(date +%Y%m%d_%H).tgz"
+    if [ ! -e "${PUSH_FILE}" ]; then
+      pushd ${wwwroot_dir}
+      tar czf ${PUSH_FILE} ./$W
+      popd
+    fi
+    /usr/local/bin/qshell rput ${qiniu_bucket} /`date +%F`/${PUSH_FILE##*/} ${PUSH_FILE} 
+    if [ $? -eq 0 ]; then
+      /usr/local/bin/qshell listbucket ${qiniu_bucket} /`date +%F --date="${expired_days} days ago"` /tmp/qiniu.txt > /dev/null 2>&1
+      /usr/local/bin/qshell batchdelete -force ${qiniu_bucket} /tmp/qiniu.txt > /dev/null 2>&1
+      rm -f /tmp/qiniu.txt
+    fi
+  done
+}
+
 for DEST in `echo ${backup_destination} | tr ',' ' '`
 do
   if [ "${DEST}" == 'local' ]; then
@@ -164,4 +199,8 @@ do
     [ -n "`echo ${backup_content} | grep -ow db`" ] && DB_UPYUN_BK
     [ -n "`echo ${backup_content} | grep -ow web`" ] && WEB_UPYUN_BK
   fi
+  if [ "${DEST}" == 'qiniu' ]; then
+    [ -n "`echo ${backup_content} | grep -ow db`" ] && DB_QINIU_BK 
+    [ -n "`echo ${backup_content} | grep -ow web`" ] && WEB_QINIU_BK 
+  fi
 done

+ 77 - 7
backup_setup.sh

@@ -36,10 +36,11 @@ while :; do echo
   echo -e "\t${CMSG}2${CEND}. Remote host"
   echo -e "\t${CMSG}3${CEND}. Aliyun OSS"
   echo -e "\t${CMSG}4${CEND}. Qcloud COS"
-  echo -e "\t${CMSG}5${CEND}. UPYUN(又拍云)"
+  echo -e "\t${CMSG}5${CEND}. UPYUN"
+  echo -e "\t${CMSG}6${CEND}. QINIU"
   read -p "Please input a number:(Default 1 press Enter) " desc_bk
   [ -z "${desc_bk}" ] && desc_bk=1
-  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)
+  ary=(1 2 3 4 5 6 12 13 14 15 16 23 24 25 26 34 35 36 45 46 56 123 124 125 126 134 135 136 145 146 156 234 235 236 245 246 256 345 346 456 1234 1235 1236 2345 2346 3456 12345 12346 13456 23456 123456)
   if [[ "${ary[@]}" =~ "${desc_bk}" ]]; then
     break
   else
@@ -53,6 +54,7 @@ sed -i 's@^backup_destination=.*@backup_destination=@' ./options.conf
 [ `echo ${desc_bk} | grep -e 3` ] && sed -i 's@^backup_destination=.*@&,oss@' ./options.conf
 [ `echo ${desc_bk} | grep -e 4` ] && sed -i 's@^backup_destination=.*@&,cos@' ./options.conf
 [ `echo ${desc_bk} | grep -e 5` ] && sed -i 's@^backup_destination=.*@&,upyun@' ./options.conf
+[ `echo ${desc_bk} | grep -e 6` ] && sed -i 's@^backup_destination=.*@&,qiniu@' ./options.conf
 sed -i 's@^backup_destination=,@backup_destination=@' ./options.conf
 
 while :; do echo
@@ -177,7 +179,7 @@ if [ `echo ${desc_bk} | grep -e 3` ]; then
     chmod +x /usr/local/bin/ossutil
   fi
   while :; do echo
-    echo 'Please select your backup datacenter:'
+    echo 'Please select your backup aliyun datacenter:'
     echo -e "\t ${CMSG}1${CEND}. cn-hangzhou-华东 1 (杭州)         ${CMSG}2${CEND}. cn-shanghai-华东 2 (上海)"
     echo -e "\t ${CMSG}3${CEND}. cn-qingdao-华北 1 (青岛)          ${CMSG}4${CEND}. cn-beijing-华北 2 (北京)"
     echo -e "\t ${CMSG}5${CEND}. cn-zhangjiakou-华北 3 (张家口)    ${CMSG}6${CEND}. cn-huhehaote-华北 5(呼和浩特)"
@@ -238,7 +240,7 @@ if [ `echo ${desc_bk} | grep -e 4` ]; then
   [ ! -e "${python_install_dir}/bin/python" ] && Install_Python
   [ ! -e "${python_install_dir}/lib/coscmd" ] && ${python_install_dir}/bin/pip install coscmd >/dev/null 2>&1
   while :; do echo
-    echo 'Please select your backup datacenter:'
+    echo 'Please select your backup qcloud datacenter:'
     echo -e "\t ${CMSG} 1${CEND}. 北京一区(华北)  ${CMSG}2${CEND}. 北京"
     echo -e "\t ${CMSG} 3${CEND}. 上海(华东)      ${CMSG}4${CEND}. 广州(华南)"
     echo -e "\t ${CMSG} 5${CEND}. 成都(西南)      ${CMSG}6${CEND}. 重庆"
@@ -303,13 +305,13 @@ if [ `echo ${desc_bk} | grep -e 5` ]; then
     chmod +x /usr/local/bin/upx
   fi
   while :; do echo
-    read -p "Please enter the ServiceName: " ServiceName
+    read -p "Please enter the upyun ServiceName: " ServiceName
     [ -z "${ServiceName}" ] && continue
     echo
-    read -p "Please enter the Operator: " Operator
+    read -p "Please enter the upyun Operator: " Operator
     [ -z "${Operator}" ] && continue
     echo
-    read -p "Please enter the Password: " Password
+    read -p "Please enter the upyun Password: " Password
     [ -z "${Password}" ] && continue
     echo
     /usr/local/bin/upx login ${ServiceName} ${Operator} ${Password} >/dev/null 2>&1
@@ -322,3 +324,71 @@ if [ `echo ${desc_bk} | grep -e 5` ]; then
     fi
   done
 fi
+
+if [ `echo ${desc_bk} | grep -e 6` ]; then
+  if [ ! -e "/usr/local/bin/qrsctl" ]; then
+    if [ "${OS_BIT}" == '64' ]; then
+      wget -qc http://devtools.qiniu.com/linux/amd64/qrsctl -O /usr/local/bin/qrsctl
+    elif [ "${OS_BIT}" == '32' ]; then
+      wget -qc http://devtools.qiniu.com/linux/386/qrsctl -O /usr/local/bin/qrsctl
+    fi
+    chmod +x /usr/local/bin/qrsctl
+  fi
+  if [ ! -e "/usr/local/bin/qshell" ]; then
+    wget -qc http://devtools.qiniu.com/qshell-v2.1.8.zip -O /tmp/qshell-v2.1.8.zip
+    unzip -q /tmp/qshell-v2.1.8.zip -d /tmp/
+    if [ "${OS_BIT}" == '64' ]; then
+      /bin/cp /tmp/qshell-linux-x64 /usr/local/bin/qshell
+    elif [ "${OS_BIT}" == '32' ]; then
+      /bin/cp /tmp/qshell-linux-x86 /usr/local/bin/qshell
+    fi
+    chmod +x /usr/local/bin/qshell
+    rm -f /tmp/qshell*
+  fi
+  while :; do echo
+    echo 'Please select your backup qiniu datacenter:'
+    echo -e "\t ${CMSG} 1${CEND}. 华东            ${CMSG}2${CEND}. 华北"
+    echo -e "\t ${CMSG} 3${CEND}. 华南            ${CMSG}4${CEND}. 北美"
+    echo -e "\t ${CMSG} 5${CEND}. 东南亚" 
+    read -p "Please input a number:(Default 1 press Enter) " Location
+    [ -z "${Location}" ] && Location=1
+    if [[ "${Location}" =~ ^[1-5]$ ]]; then
+      break
+    else
+      echo "${CWARNING}input error! Please only input number 1~5${CEND}"
+    fi
+  done
+  [ "${Location}" == '1' ] && zone='z0'
+  [ "${Location}" == '2' ] && zone='z1'
+  [ "${Location}" == '3' ] && zone='z2'
+  [ "${Location}" == '4' ] && zone='na0'
+  [ "${Location}" == '5' ] && zone='as0'
+  while :; do echo
+    read -p "Please enter the qiniu AccessKey: " AccessKey 
+    [ -z "${AccessKey}" ] && continue
+    echo
+    read -p "Please enter the qiniu SecretKey: " SecretKey 
+    [ -z "${SecretKey}" ] && continue
+    echo
+    read -p "Please enter the qiniu bucket: " bucket
+    [ -z "${bucket}" ] && continue
+    echo
+    /usr/local/bin/qshell account ${AccessKey} ${SecretKey}
+    /usr/local/bin/qrsctl login ${AccessKey} ${SecretKey}
+    if /usr/local/bin/qrsctl bucketinfo ${bucket} > /dev/null 2>&1; then
+      sed -i "s@^qiniu_bucket=.*@qiniu_bucket=${bucket}@" ./options.conf
+      echo "${CMSG}AccessKey/SecretKey OK${CEND}"
+      echo
+      break
+    elif /usr/local/bin/qrsctl mkbucket ${bucket} ${zone} > /dev/null 2>&1; then
+      /usr/local/bin/qrsctl private ${bucket} 1
+      echo "${CMSG}[${bucket}] createbucket OK${CEND}"
+      sed -i "s@^qiniu_bucket=.*@qiniu_bucket=${bucket}@" ./options.conf
+      echo "${CMSG}AccessKey/SecretKey OK${CEND}"
+      echo
+      break
+    else 
+      echo "${CWARNING}input error! AccessKey/SecretKey invalid${CEND}"
+    fi
+  done
+fi

+ 3 - 0
options.conf

@@ -75,6 +75,9 @@ backup_content=
 # aliyun OSS Bucket, You can't change
 oss_bucket=
 
+# qiniu Bucket, You can't change
+qiniu_bucket=
+
 # The backup of the database, You can't change
 db_name=
 

+ 1 - 1
versions.txt

@@ -34,7 +34,7 @@ percona55_ver=5.5.59-38.11
 
 alisql_ver=5.6.32-8
 
-pgsql_ver=10.3
+pgsql_ver=10.4
 
 mongodb_ver=3.6.4
 

+ 3 - 2
vhost.sh

@@ -234,10 +234,11 @@ EOF
     fi
     if [ -s ~/.acme.sh/${domain}/fullchain.cer ]; then
       [ -e "${PATH_SSL}/${domain}.crt" ] && rm -rf ${PATH_SSL}/${domain}.{crt,key}
+      [ -e /bin/systemctl -a -e /lib/systemd/system/nginx.service ] && Nginx_cmd='/bin/systemctl restart nginx' || Nginx_cmd='/etc/init.d/nginx force-reload'
       if [ -e "${web_install_dir}/sbin/nginx" -a -e "${apache_install_dir}/conf/httpd.conf" ]; then
-        Command="/etc/init.d/nginx force-reload;/etc/init.d/httpd graceful"
+        Command="${Nginx_cmd};/etc/init.d/httpd graceful"
       elif [ -e "${web_install_dir}/sbin/nginx" -a ! -e "${apache_install_dir}/conf/httpd.conf" ]; then
-        Command="/etc/init.d/nginx force-reload"
+        Command="${Nginx_cmd}"
       elif [ ! -e "${web_install_dir}/sbin/nginx" -a -e "${apache_install_dir}/conf/httpd.conf" ]; then
         Command="/etc/init.d/httpd graceful"
       fi