You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
5.3 KiB

2 years ago
  1. #!/bin/bash
  2. ## author : Dryusdan
  3. ## date : 30/09/2019
  4. ## description : A Nextcloud Update
  5. ## usage : ./nextcloud.sh
  6. ## Bash strict mode ####################################
  7. set -o errexit # abort on nonzero exitstatus
  8. set -o nounset # abort on unbound variable
  9. set -o pipefail # don't hide errors within pipes
  10. ## Bash color ##########################################
  11. # Set colors
  12. RED='\033[0;31m'
  13. GREEN='\033[00;32m'
  14. YELLOW='\033[00;33m'
  15. BLUE='\033[00;34m'
  16. PURPLE='\033[00;35m'
  17. CYAN='\033[00;36m'
  18. LIGHTGRAY='\033[00;37m'
  19. LRED='\033[01;31m'
  20. LGREEN='\033[01;32m'
  21. LYELLOW='\033[01;33m'
  22. LBLUE='\033[01;34m'
  23. LPURPLE='\033[01;35m'
  24. LCYAN='\033[01;36m'
  25. WHITE='\033[01;37m'
  26. NC='\033[0m' # No Color
  27. ## Logs ################################################
  28. readonly SCRIPTNAME="$(basename "$0")"
  29. info() { echo -e "${LBLUE}[INFO] $* ${NC}" | logger --tag "${SCRIPTNAME}" --stderr ; }
  30. warning() { echo -e "${YELLOW}[WARNING] $* ${NC}" | logger --tag "${SCRIPTNAME}" --stderr ; }
  31. error() { echo -e "${LRED}[ERROR] $* ${NC}" | logger --tag "${SCRIPTNAME}" --stderr ; }
  32. fatal() { echo -e "${RED}[FATAL] $* ${NC}" | logger --tag "${SCRIPTNAME}" --stderr ; exit 1 ; }
  33. ########################################################
  34. ## Define nextcloud's folder ##########################
  35. FOLDER="/var/www/nextcloud/"
  36. ## Ask nextcloud ######################################
  37. read -p "Nextcloud version ? " VERSION
  38. info "Check if require folders exist"
  39. if [ -d "${FOLDER}/nextcloud" ]; then
  40. warning "Aborded installation exist. Remove nextcloud/ folder"
  41. rm -rf ${FOLDER}/nextcloud || fatal "Error when removing folder. Exiting"
  42. rm "${FOLDER}"/nextcloud-* || fatal "Error when removing nextcloud-* .zip, .zip.sha256, .zip.asc. Please remove $FOLDER/nextcloud and $FOLDER/nextcloud-* and retry"
  43. fi
  44. if [ -d "${FOLDER}/www" ]; then
  45. info "www/ directory exist, continue"
  46. NEW_INSTALLATION=false
  47. else
  48. warning "www/ directory don't exist, creating"
  49. mkdir "${FOLDER}/www" || fatal "Error when creating folder. Exiting"
  50. NEW_INSTALLATION=true
  51. fi
  52. if [ -d "${FOLDER}/data" ]; then
  53. info "data/ directory exist, continue"
  54. else
  55. warning "data/ directory don't exist, creating"
  56. mkdir ${FOLDER}/data || fatal "Error when creating folder. Exiting"
  57. NEW_INSTALLATION=true
  58. fi
  59. if [ -d "${FOLDER}/config" ]; then
  60. info "config/ directory exist, continue"
  61. else
  62. warning "config/ directory don't exist, creating"
  63. mkdir ${FOLDER}/config || fatal "Error when creating folder. Exiting"
  64. NEW_INSTALLATION=true
  65. fi
  66. if [ -d "${FOLDER}/apps" ]; then
  67. info "apps/ directory exist, continue"
  68. else
  69. warning "apps/ directory don't exist, creating"
  70. mkdir ${FOLDER}/apps || fatal "Error when creating folder. Exiting"
  71. NEW_INSTALLATION=true
  72. fi
  73. # Download nextcloud
  74. info "Download Nextcloud ${VERSION}"
  75. wget -q https://download.nextcloud.com/server/releases/nextcloud-${VERSION}.zip || fatal "Download failed. Please retry"
  76. if [ ! -f "${FOLDER}/nextcloud-${VERSION}.zip" ]; then
  77. fatal "Download of Nextcloud archive failed. Please retry"
  78. fi
  79. info "Download sha256 of Nextcloud"
  80. wget -q https://download.nextcloud.com/server/releases/nextcloud-$VERSION.zip.sha256 || fatal "Download of Nextcloud checksum failed. Please retry"
  81. if [ ! -f "$FOLDER/nextcloud-$VERSION.zip.sha256" ]; then
  82. fatal "Download of Nextcloud checksum failed. Please retry"
  83. fi
  84. info "Download asc of Nextcloud"
  85. wget -q https://download.nextcloud.com/server/releases/nextcloud-${VERSION}.zip.asc || fatal "Download of Nextcloud signature failed. Please retry"
  86. if [ ! -f "${FOLDER}/nextcloud-${VERSION}.zip.asc" ]; then
  87. fatal "Download of Nextcloud signature failed. Please retry"
  88. fi
  89. # Check integrity nextcloud
  90. info "Check nextcloud integrity"
  91. sha256sum -c nextcloud-${VERSION}.zip.sha256 >/dev/null || fatal "Checksum did not match"
  92. gpg -q --verify nextcloud-${VERSION}.zip.asc 2>/dev/null|| fatal "Signature is not good"
  93. info "Start update"
  94. info "Unzip Nextcloud ${VERSION}"
  95. unzip -q nextcloud-${VERSION}.zip
  96. if [[ ${NEW_INSTALLATION} == "false" ]]; then
  97. info "Unlink folders that contain the data"
  98. cp ${FOLDER}/config/config.php ${FOLDER}/config.php
  99. unlink www/data
  100. unlink www/config
  101. unlink www/apps
  102. info "Backuping old installation"
  103. tar -zcf "www.$(date '+%Y-%m-%d').tar.gz" www
  104. tar -zcf "apps.$(date '+%Y-%m-%d').tar.gz" apps
  105. info "Removing old installation"
  106. rm -rf "${FOLDER}"/www/*
  107. info "Copying news apps files"
  108. fi
  109. info "Copying new Nextcloud"
  110. cp -rf "${FOLDER}"/nextcloud/apps/* apps/
  111. rm -rf "${FOLDER}"/nextcloud/{config,data,apps}
  112. cp -rf "${FOLDER}"/nextcloud/* www/
  113. cp -rf ${FOLDER}/config.php ${FOLDER}/config/config.php
  114. info "Link folders that contain the data into new installation"
  115. ln -s ${FOLDER}/data ${FOLDER}/www/
  116. ln -s ${FOLDER}/config ${FOLDER}/www/
  117. ln -s ${FOLDER}/apps ${FOLDER}/www/
  118. info "Removing archive and temp folder"
  119. rm -rf ${FOLDER}/nextcloud-${VERSION}.zip* ${FOLRDER}/nextcloud/
  120. info "Setting good permission"
  121. cd www/
  122. find -type f -exec chmod 644 {} \; 2> /dev/null;
  123. find -type d -exec chmod 755 {} \; 2> /dev/null;
  124. info "Launch upgrade with occ"
  125. php occ upgrade
  126. info "Launch integrity check"
  127. php occ check
  128. read -n1 -p "Would you like scann all file (recommanded) [y,n]" SCAN
  129. case ${SCAN} in
  130. y|Y) info "Launch files scan" && php console.php files:scan --all ;;
  131. n|N) info "Don't launch files scan" ;;
  132. *) echo Please use y or n ;;
  133. esac
  134. ###########################################################
  135. info "exiting ${SCRIPTNAME}"
  136. exit 0