]> Frank Brehm's Git Trees - pixelpark/admin-tools.git/commitdiff
Updating bin/update-nextcloud to use /var/www/nextcloud and performing backup of...
authorFrank Brehm <frank@brehm-online.com>
Wed, 4 Oct 2023 08:09:34 +0000 (10:09 +0200)
committerFrank Brehm <frank@brehm-online.com>
Wed, 4 Oct 2023 08:09:34 +0000 (10:09 +0200)
bin/update-nextcloud
lib/functions.rc

index d7e965f761fac4c50ed7940f607c5cfa03d20868..27aa4bcdd761db2a8c98f44c9182c69b6d203b06 100755 (executable)
@@ -14,12 +14,14 @@ CONF_FILE="${CONF_DIR}/update-nextcloud.rc"
 NEW_VERSION=""
 OLD_VERSION=""
 WWW_BASE=/var/www
-NEXTCLOUD_DIR_LINK="${WWW_BASE}/nextcloud"
-NEW_NEXTCLOUD_DIR="${WWW_BASE}/nextcloud-bla"
+NEXTCLOUD_DIR="${WWW_BASE}/nextcloud"
+NEXTCLOUD_DIR_NEW="${NEXTCLOUD_DIR}.new"
+NEXTCLOUD_DIR_OLD="${NEXTCLOUD_DIR}.old"
 DOWNLOAD_DIR="/root/Downloads"
 ARTIFACT="nextcloud-bla.tar.bz2"
 MD5_FILE="${ARTIFACT}.md5"
 SHA256_FILE="${ARTIFACT}.sha256"
+SHA512_FILE="${ARTIFACT}.sha512"
 DOWNLOAD_URL_BASE="https://download.nextcloud.com/server/releases"
 TEMP_DIR_PARENT="/var/tmp"
 TEMP_DIR=
@@ -28,6 +30,10 @@ WWW_GROUP="www-data"
 FILE_PERMS="0640"
 DIR_PERMS="0750"
 SERVICE_NAME="apache2.service"
+MY_HOSTNAME=$( hostname )
+BACKUP_DIR_ROOT="/var/backup"
+MY_BACKUP_DIR="${BACKUP_DIR_ROOT}/${MY_HOSTNAME}"
+BACKUP_DIR="${MY_BACKUP_DIR}/nextcloud-update"
 
 if [[ -f "${LIB_DIR}/functions.rc" ]] ; then
     . "${LIB_DIR}/functions.rc"
@@ -44,7 +50,7 @@ if [[ -f "${CONF_FILE}" ]] ; then
 fi
 
 DESCRIPTION=$( cat <<-EOF
-       Updates the local Nextcloud installation under '${CYAN}${NEXTCLOUD_DIR_LINK}${NORMAL}'.
+       Updates the local Nextcloud installation under '${CYAN}${NEXTCLOUD_DIR}${NORMAL}'.
 
        You must be root to execute this script.
 
@@ -113,18 +119,39 @@ get_options() {
     ARTIFACT="nextcloud-${NEW_VERSION}.tar.bz2"
     MD5_FILE="${ARTIFACT}.md5"
     SHA256_FILE="${ARTIFACT}.sha256"
-    NEW_NEXTCLOUD_DIR="${WWW_BASE}/nextcloud-${NEW_VERSION}"
+    SHA512_FILE="${ARTIFACT}.sha512"
 
     check_for_root
 
 }
 
+#------------------------------------------------------------------------------
+WGET() {
+
+    url="$1"
+
+    info "Downloading '${CYAN}${url}${NORMAL}' ..."
+
+    if [[ "${VERBOSE}" == "y" ]] ; then
+        wget "${url}"
+    else
+        wget -q "${url}"
+    fi
+
+}
+
 #------------------------------------------------------------------------------
 get_old_version() {
 
-    local cur_real_nc_dir=$( readlink -f "${NEXTCLOUD_DIR_LINK}" )
-    debug "Current real nextcloud directory: '${CYAN}${cur_real_nc_dir}${NORMAL}'."
-    OLD_VERSION=$( echo "${cur_real_nc_dir}" | xargs basename | sed -e 's/^nextcloud-//' )
+    local version_file="${NEXTCLOUD_DIR}/version.php"
+    debug "Get current Nextcloud version from '${CYAN}${version_file}${NORMAL}' ..."
+
+    if [[ ! -f "${version_file}" ]] ; then
+        error "Did not found version file '${RED}${version_file}${NORMAL}'."
+        exit 6
+    fi
+
+    OLD_VERSION=$( cat "${version_file}" | grep -i '^\$OC_VersionString' | sed -e "s/.*=  *'//" -e "s/'.*//" )
     debug "Current Nextcloud version: '${CYAN}${OLD_VERSION}${NORMAL}'."
 
     if [[ "${OLD_VERSION}" == "${NEW_VERSION}" ]] ; then
@@ -143,11 +170,11 @@ get_old_version() {
         fi
     fi
 
-    if [[ -d "${NEW_NEXTCLOUD_DIR}" ]] ; then
-        warn "The directory '${YELLOW}${NEW_NEXTCLOUD_DIR}${NORMAL}' is already existing."
+    if [[ -d "${NEXTCLOUD_DIR_NEW}" ]] ; then
+        warn "The directory '${YELLOW}${NEXTCLOUD_DIR_NEW}${NORMAL}' is already existing."
         if yes_or_no "Continue [y/n]? " ; then
-            info "Removing existing '${YELLOW}${NEW_NEXTCLOUD_DIR}${NORMAL}' ..."
-            RM -r "${NEW_NEXTCLOUD_DIR}"
+            info "Removing existing '${YELLOW}${NEXTCLOUD_DIR_NEW}${NORMAL}' ..."
+            RM -r "${NEXTCLOUD_DIR_NEW}"
         else
             info "Give it up."
             exit 1
@@ -159,6 +186,36 @@ get_old_version() {
 
 }
 
+#------------------------------------------------------------------------------
+do_backup() {
+
+    local backup_file="${BACKUP_DIR}/backup-${OLD_VERSION}.tar.bz2"
+
+    info "Backing up current Nextcloud directory to '${GREEN}${backup_file}${NORMAL}' ..."
+
+    if [[ ! -d "${BACKUP_DIR}" ]] ; then
+        info "Creating '${CYAN}${BACKUP_DIR}${NORMAL}' ..."
+        MKDIR -p "${BACKUP_DIR}"
+    fi
+
+    cd "${WWW_BASE}"
+
+    local cmd="tar -c"
+    if [[ "${VERBOSE}" == "y" ]] ; then
+        cmd+=" --verbose"
+    fi
+    cmd+=" --file \"${backup_file}\" --bzip2 nextcloud"
+    info "Executing: ${cmd}"
+    eval ${cmd}
+
+    cd
+
+    empty_line
+    info "Backup directory '${CYAN}${BACKUP_DIR}${NORMAL}':"
+    ls -l "${BACKUP_DIR}"
+
+}
+
 #------------------------------------------------------------------------------
 download_release() {
 
@@ -167,32 +224,22 @@ download_release() {
     local release_url="${DOWNLOAD_URL_BASE}/${ARTIFACT}"
     local md5_url="${DOWNLOAD_URL_BASE}/${MD5_FILE}"
     local sha256_url="${DOWNLOAD_URL_BASE}/${SHA256_FILE}"
+    local sha512_url="${DOWNLOAD_URL_BASE}/${SHA512_FILE}"
 
     if [[ ! -f "${ARTIFACT}" ]] ; then
-        info "Downloading '${CYAN}${release_url}${NORMAL}' ..."
-        if [[ "${VERBOSE}" == "y" ]] ; then
-            wget "${release_url}"
-        else
-            wget -q "${release_url}"
-        fi
+        WGET "${release_url}"
     fi
 
     if [[ ! -f "${MD5_FILE}" ]] ; then
-        info "Downloading '${CYAN}${md5_url}${NORMAL}' ..."
-        if [[ "${VERBOSE}" == "y" ]] ; then
-            wget "${md5_url}"
-        else
-            wget -q "${md5_url}"
-        fi
+        WGET "${md5_url}"
     fi
 
     if [[ ! -f "${SHA256_FILE}" ]] ; then
-        info "Downloading '${CYAN}${sha256_url}${NORMAL}' ..."
-        if [[ "${VERBOSE}" == "y" ]] ; then
-            wget "${sha256_url}"
-        else
-            wget -q "${sha256_url}"
-        fi
+        WGET "${sha256_url}"
+    fi
+
+    if [[ ! -f "${SHA512_FILE}" ]] ; then
+        WGET "${sha512_url}"
     fi
 
     info "Checking MD5 consistence of ${ARTIFACT} ..."
@@ -201,6 +248,9 @@ download_release() {
     info "Checking SHA256 consistence of ${ARTIFACT} ..."
     sha256sum -c "${SHA256_FILE}"
 
+    info "Checking SHA512 consistence of ${ARTIFACT} ..."
+    sha512sum -c "${SHA512_FILE}"
+
 }
 
 #------------------------------------------------------------------------------
@@ -238,8 +288,8 @@ unpack_archive() {
         tar xfj "${archive}"
     fi
 
-    info "Moving '${CYAN}${TEMP_DIR}/nextcloud${NORMAL}' to '${CYAN}${NEW_NEXTCLOUD_DIR}${NORMAL}' ..."
-    mv "${TEMP_DIR}/nextcloud" "${NEW_NEXTCLOUD_DIR}"
+    info "Moving '${CYAN}${TEMP_DIR}/nextcloud${NORMAL}' to '${CYAN}${NEXTCLOUD_DIR_NEW}${NORMAL}' ..."
+    mv "${TEMP_DIR}/nextcloud" "${NEXTCLOUD_DIR_NEW}"
     cd
 
     debug "Deactivating trap ..."
@@ -251,9 +301,9 @@ unpack_archive() {
 #------------------------------------------------------------------------------
 adjust_permissions() {
 
-    cd "${NEW_NEXTCLOUD_DIR}"
+    cd "${NEXTCLOUD_DIR_NEW}"
 
-    info "Adjusting permissions if '${CYAN}${NEW_NEXTCLOUD_DIR}${NORMAL}' ..."
+    info "Adjusting permissions if '${CYAN}${NEXTCLOUD_DIR_NEW}${NORMAL}' ..."
     info "Setting ownership to ${WWW_USER}:${WWW_GROUP} ..."
     chown -R "${WWW_USER}:${WWW_GROUP}" .
     info "Setting file mode to '${FILE_PERMS}' ..."
@@ -266,9 +316,8 @@ adjust_permissions() {
 #------------------------------------------------------------------------------
 sync_apps() {
 
-    local cur_nc_dir="${WWW_BASE}/nextcloud-${OLD_VERSION}"
-    local cur_apps_dir="${cur_nc_dir}/apps"
-    local new_apps_dir="${NEW_NEXTCLOUD_DIR}/apps"
+    local cur_apps_dir="${NEXTCLOUD_DIR}/apps"
+    local new_apps_dir="${NEXTCLOUD_DIR_NEW}/apps"
     local app=
 
     TEMP_DIR=$( mktemp -d "${TEMP_DIR_PARENT}/nextcloud-apps-XXXXXXXXXX" )
@@ -298,21 +347,31 @@ sync_apps() {
     cleanup_tmp_dir
 }
 
+#------------------------------------------------------------------------------
+occ() {
+
+    debug "Executing '${CYAN}php occ $*${NORMAL}' ..."
+    (
+        cd "${NEXTCLOUD_DIR}"
+        sudo -u "${WWW_USER}" php occ "$@"
+    )
+
+}
+
 #------------------------------------------------------------------------------
 set_maintenance() {
 
     info "Setting maintenance mode ..."
-    cd "${NEXTCLOUD_DIR_LINK}"
-    sudo -u "${WWW_USER}" php occ maintenance:mode --on
+    cd "${NEXTCLOUD_DIR}"
+    occ maintenance:mode --on
 
 }
 
 #------------------------------------------------------------------------------
 copy_config() {
 
-    local cur_nc_dir="${WWW_BASE}/nextcloud-${OLD_VERSION}"
-    local cur_cfg_file="${cur_nc_dir}/config/config.php"
-    local new_cfg_file="${NEW_NEXTCLOUD_DIR}/config/config.php"
+    local cur_cfg_file="${NEXTCLOUD_DIR}/config/config.php"
+    local new_cfg_file="${NEXTCLOUD_DIR_NEW}/config/config.php"
 
     info "Copying configuration ..."
     CP "${cur_cfg_file}" "${new_cfg_file}"
@@ -332,22 +391,25 @@ stop_service() {
 }
 
 #------------------------------------------------------------------------------
-change_symlinks() {
+switch_nc_dirs() {
 
-    info "Setting symlink '${CYAN}${NEXTCLOUD_DIR_LINK}${NORMAL}' -> '${CYAN}nextcloud-${NEW_VERSION}${NORMAL}' ..."
-    cd "${WWW_BASE}"
-    RM "${NEXTCLOUD_DIR_LINK}"
-    LN -s "nextcloud-${NEW_VERSION}" "${NEXTCLOUD_DIR_LINK}"
+    empty_line
+    info "Switching Nextcloud directories to new version ..."
+
+    debug "Moving '${CYAN}${NEXTCLOUD_DIR}${NORMAL}' -> '${CYAN}${NEXTCLOUD_DIR_OLD}${NORMAL}' ..."
+    MV "${NEXTCLOUD_DIR}" "${NEXTCLOUD_DIR_OLD}"
+
+    debug "Moving '${CYAN}${NEXTCLOUD_DIR_NEW}${NORMAL}' -> '${CYAN}${NEXTCLOUD_DIR}${NORMAL}' ..."
+    MV "${NEXTCLOUD_DIR_NEW}" "${NEXTCLOUD_DIR}"
 
 }
 
 #------------------------------------------------------------------------------
 upgrade_nc() {
 
-    echo
+    empty_line
     info "Upgrading Nextcloud ..."
-    cd "${NEXTCLOUD_DIR_LINK}"
-    sudo -u "${WWW_USER}" php occ upgrade
+    occ upgrade
 
 }
 
@@ -366,8 +428,7 @@ start_service() {
 release_maintenance() {
 
     info "Releasing maintenance mode ..."
-    cd "${NEXTCLOUD_DIR_LINK}"
-    sudo -u "${WWW_USER}" php occ maintenance:mode --off
+    occ maintenance:mode --off
 
 }
 
@@ -378,6 +439,7 @@ main() {
     umask 0022
 
     get_old_version
+    do_backup
     download_release
     unpack_archive
     adjust_permissions
@@ -385,7 +447,7 @@ main() {
     set_maintenance
     copy_config
     stop_service
-    change_symlinks
+    switch_nc_dirs
     upgrade_nc
     start_service
     release_maintenance
index 89d690060d6d1d2a21b60347dfc4f1bbc90a4551..d124156140ddc4320b0916b7f3e88003ec34d7ae 100644 (file)
@@ -10,7 +10,7 @@ BLUE=""
 CYAN=""
 NORMAL=""
 
-VERSION="0.4.0"
+VERSION="0.4.1"
 
 STD_SHORT_OPTIONS="sdvqhV"
 STD_LONG_OPTIONS="simulate,debug,verbose,quiet,nocolor,help,version"