diff --git a/update_nextcloud.sh b/update_nextcloud.sh index 12465d2..b47a5a5 100755 --- a/update_nextcloud.sh +++ b/update_nextcloud.sh @@ -9,11 +9,20 @@ declare -a website_arr log_file="$(mktemp)" +backup_date=$(date +%Y-%m-%d-%H%M) + # ============= # --- Some functions # ============= +clean_up() { + + # Perform program exit housekeeping + rm -f $log_file + exit $1 +} + is_number() { return $(test ! -z "${1##*[!0-9]*}" > /dev/null 2>&1); @@ -79,9 +88,7 @@ fatal (){ echo "fatal: $*" echo "Script will be interrupted.." fi - rm -f $log_file - echo "" - exit 1 + clean_up 1 } error(){ @@ -122,6 +129,8 @@ containsElement () { return 1 } +trap clean_up SIGHUP SIGINT SIGTERM + ## - while IFS='' read -r -d '' _conf_file ; do source $_conf_file @@ -426,7 +435,7 @@ fi # - Deaktiviere Cronjobs # - -_backup_crontab_file=/tmp/crontab_root.$(date +%Y-%m-%d-%H%M) +_backup_crontab_file=/tmp/crontab_root.${backup_date} echononl " Backup Crontab to '$_backup_crontab_file'" crontab -l > $_backup_crontab_file 2> $log_file if [[ $? -eq 0 ]]; then @@ -475,7 +484,7 @@ echo "" echononl " Backup MySQL database '$DATABASE_NANE'.." if [[ "$DATABASE_TYPE" = 'mysql' ]]; then mysqldump $MYSQL_CREDENTIALS --opt $DATABASE_NAME > \ - ${WEB_BASE_DIR}/${DATABASE_NAME}-v${PRIOR_VERSION}.`date +"%Y-%m-%d-%H%M"`.sql 2> $log_file + ${WEB_BASE_DIR}/${DATABASE_NAME}-v${PRIOR_VERSION}.${backup_date}.sql 2> $log_file if [[ $? -eq 0 ]]; then echo_ok else @@ -483,7 +492,7 @@ if [[ "$DATABASE_TYPE" = 'mysql' ]]; then fatal "$(cat $log_file)" fi elif [[ "$DATABASE_TYPE" = 'postgres' ]]; then - PGPASSWORD=$PSQL_PASS pg_dump $DATABASE_NAME -h $PSQL_SERVER -U $PSQL_USER -f postfix-$(date +%Y-%m-%d-%H%M).sql + PGPASSWORD=$PSQL_PASS pg_dump $DATABASE_NAME -h $PSQL_SERVER -U $PSQL_USER -f postfix-${backup_date}.sql if [[ $? -eq 0 ]]; then echo_ok else @@ -562,7 +571,7 @@ fi # - Set Permissions on new data directory # - echononl " Change permissions on '${DATA_DIR}'.." -chown -R ${HTTP_USER}:${HTTP_GROUP} ${DATA_DIR} +chown -R ${HTTP_USER}:${HTTP_GROUP} ${DATA_DIR} > $log_file 2>&1 if [[ $? -eq 0 ]]; then echo_ok else @@ -571,11 +580,24 @@ else fi +# - Backup old data directory +# - +echononl " Backup old data directory" +_old_data_dir="$(realpath ${WEB_BASE_DIR}/data)" +mv "$_old_data_dir" "${_old_data_dir}.$backup_date" > $log_file 2>&1 +if [[ $? -eq 0 ]]; then + echo_ok +else + echo_failed + error "$(cat $log_file)" +fi + + # - Delete symlink for old data directory # - echononl " Remove symlink from old data dir 'data-${PRIOR_VERSION}'.." -rm ${WEB_BASE_DIR}/data +rm ${WEB_BASE_DIR}/data > $log_file 2>&1 if [[ $? -eq 0 ]]; then echo_ok else @@ -583,10 +605,11 @@ else fatal "$(cat $log_file)" fi + # - Set symlink for new data directory # - echononl " Set symlink for new data dir 'data-${VERSION}'.." -ln -s data-${VERSION} ${WEB_BASE_DIR}/data +ln -s data-${VERSION} ${WEB_BASE_DIR}/data > $log_file 2>&1 if [[ $? -eq 0 ]]; then echo_ok else @@ -595,9 +618,22 @@ else fi +# - Backup old installation directory +# - +echononl " Backup old installation directory" +_old_install_dir="$(realpath ${WEB_BASE_DIR}/nextcloud)" +mv "$_old_install_dir" "${_old_install_dir}.$backup_date" > $log_file 2>&1 +if [[ $? -eq 0 ]]; then + echo_ok +else + echo_failed + error "$(cat $log_file)" +fi + + # - Remove symlink from old installation directory # - -rm ${WEB_BASE_DIR}/nextcloud +rm ${WEB_BASE_DIR}/nextcloud > $log_file 2>&1 if [[ $? -eq 0 ]]; then echo_ok else @@ -607,7 +643,7 @@ fi # - Set symlink (documentroot) for new installation directory # - -ln -s nextcloud-${VERSION} ${WEB_BASE_DIR}/nextcloud +ln -s nextcloud-${VERSION} ${WEB_BASE_DIR}/nextcloud > $log_file 2>&1 if [[ $? -eq 0 ]]; then echo_ok else @@ -619,7 +655,7 @@ fi # - echo "" echo " Update Nextcloud" -su -c"/usr/local/php/bin/php ${WEB_BASE_DIR}/htdocs/occ upgrade" -s /bin/bash $HTTP_USER +su -c"/usr/local/php/bin/php ${WEB_BASE_DIR}/htdocs/occ upgrade" -s /bin/bash $HTTP_USER > $log_file 2>&1 if [[ $? -eq 0 ]]; then echo_ok else @@ -631,7 +667,7 @@ echo "" echononl " Restart PHP engine.." if [[ "$PHP_ENGINE" = "FPM" ]]; then if $SYSTEMD_EXISTS ; then - systemctl restart php-${PHP_VERSION}-fpm + systemctl restart php-${PHP_VERSION}-fpm > $log_file 2>&1 if [[ $? -eq 0 ]]; then echo_ok else @@ -639,7 +675,7 @@ if [[ "$PHP_ENGINE" = "FPM" ]]; then fatal "$(cat $log_file)" fi else - /etc/init.d/php-${PHP_VERSION}-fpm restart + /etc/init.d/php-${PHP_VERSION}-fpm restart > $log_file 2>&1 if [[ $? -eq 0 ]]; then echo_ok else @@ -657,7 +693,7 @@ echo "" echononl " Start Apache Webserver.." if $IS_HTTPD_RUNNING ; then if $SYSTEMD_EXISTS ; then - systemctl start apache2 + systemctl start apache2 > $log_file 2>&1 if [[ $? -eq 0 ]]; then echo_ok else @@ -665,7 +701,7 @@ if $IS_HTTPD_RUNNING ; then fatal "$(cat $log_file)" fi else - /etc/init.d/apache2 start + /etc/init.d/apache2 start > $log_file 2>&1 if [[ $? -eq 0 ]]; then echo_ok else @@ -688,13 +724,7 @@ else error "$(cat $log_file)" fi -rm -f $log_file -echo "" -exit -## - nextcloud-12.0.0 tar.bz2 -## - - - +clean_up 0 cd $WEB_BASE_DIR