diff --git a/backup.sh b/backup.sh index bbfa53d80bedd93141cfe2c3d779ccadaf768cd1..5a802f0c5dd5db21a30fe13a53c2860e9fa6ae56 100755 --- a/backup.sh +++ b/backup.sh @@ -27,15 +27,12 @@ BACKUPDIR=./backup #The directory which the dump is to be stored to. Do not change it here. Use the --backupdir=./my/dir/ option! PRINT_HELP="--backupdir=BACKUPDIR\n\tThe directory which the dump is to be stored to. (Defaults to ./backup)\n" -. .config -# load useful stuff - scans the parameters and so on... -. patches/utils/patch_header.sh +. load_settings.sh +. utils.sh #create backup dir if not exists [ -d $BACKUPDIR ] || mkdir $BACKUPDIR - - function backup { NARG_NAME=$1 NARG_FILE=$2 @@ -50,18 +47,11 @@ function backup { success } - - - - ##test dump file exists #touch dumpfile.tmp #backup 0 1 2 "dumpfile.tmp" #rm dumpfile.tmp - DATE=$(date -u --rfc-3339=ns | sed 's/ /T/g') BACKUPFILE=${BACKUPDIR}/${DATABASE_NAME}.${DATE}.dump.sql backup $DATABASE_NAME $BACKUPFILE - - diff --git a/load_settings.sh b/load_settings.sh new file mode 100644 index 0000000000000000000000000000000000000000..d26b2968265395f93a2408e59b7e4ee2b1cd33b4 --- /dev/null +++ b/load_settings.sh @@ -0,0 +1,60 @@ +# This file is a part of the CaosDB Project. +# +# Copyright (C) 2018 Research Group Biomedical Physics, +# Max-Planck-Institute for Dynamics and Self-Organization Göttingen +# Copyright 2019 Daniel Hornung +# Copyright 2020 Henrik tom Wörden, IndiScale GmbH +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +[[ -n "config.defaults" ]] && source "config.defaults" +[[ -n ".config" ]] && source ".config" +[[ -n "$ENV_FILE" ]] && source "$ENV_FILE" + +if [[ -z "$DATABASE_NAME" && -z "$MYSQL_CONNECTION" ]] +then + _print_help "Please specify the database." + exit 1 +fi + +if [ "$LOGIN_PATH" ]; then + MYSQL_CONNECTION="--login-path=$LOGIN_PATH" + MYSQL_CONNECTION_NO_DB="$MYSQL_CONNECTION" + MYSQL_CONNECTION="$MYSQL_CONNECTION --database=$DATABASE_NAME" + +elif [[ -z "$MYSQL_CONNECTION" ]]; then + MYSQL_CONNECTION="" + if [ "$DATABASE_USER" ] + then + MYSQL_CONNECTION="--user=$DATABASE_USER" + fi + if [ "$DATABASE_USER_PW" ] + then + MYSQL_CONNECTION="$MYSQL_CONNECTION --password=$DATABASE_USER_PW" + fi + if [[ "$MYSQL_HOST" && ( "$MYSQL_HOST" != "localhost" ) ]]; then + MYSQL_CONNECTION="$MYSQL_CONNECTION --host=$MYSQL_HOST" + if [ "$MYSQL_PORT" ]; then + MYSQL_CONNECTION="$MYSQL_CONNECTION --port=$MYSQL_PORT" + fi + fi + # This option should come last, so we also have one version without the database + MYSQL_CONNECTION_NO_DB="$MYSQL_CONNECTION" + MYSQL_CONNECTION="$MYSQL_CONNECTION --database=$DATABASE_NAME" +fi + +# TODO is this a good idea? +export MYSQL_CONNECTION +export MYSQL_CONNECTION_NO_DB +export DATABASE_NAME diff --git a/patches/utils/patch_header.sh b/patches/utils/patch_header.sh index c742a1f75ff3fbe3fac4d486c16a55c44c3b2464..5456c2a1c38fde3ae917c726fb171a24fb3250f0 100644 --- a/patches/utils/patch_header.sh +++ b/patches/utils/patch_header.sh @@ -22,6 +22,10 @@ # ** end header # #header for patch scripts + +. ../../load_settings.sh +. ../../utils.sh + USAGE="$1 [ --env=ENV_FILE ] [ --patch=PATCH ] [ --backupdir=BACKUPDIR ]\n\n" CMD_OPTIONS=cat <<EOF options: @@ -88,61 +92,11 @@ while test $# -gt 0; do esac done -[[ -n "$ENV_FILE" ]] && source "$ENV_FILE" - -if [[ -z "$DATABASE_NAME" && -z "$MYSQL_CONNECTION" ]] -then - _print_help "Please specify the database." - exit 1 -fi - -if [ "$LOGIN_PATH" ]; then - MYSQL_CONNECTION="--login-path=$LOGIN_PATH" - MYSQL_CONNECTION_NO_DB="$MYSQL_CONNECTION" - MYSQL_CONNECTION="$MYSQL_CONNECTION --database=$DATABASE_NAME" -elif [[ -z "$MYSQL_CONNECTION" ]]; then - MYSQL_CONNECTION="" - if [ "$DATABASE_USER" ] - then - MYSQL_CONNECTION="--user=$DATABASE_USER" - fi - if [ "$DATABASE_USER_PW" ] - then - MYSQL_CONNECTION="$MYSQL_CONNECTION --password=$DATABASE_USER_PW" - fi - if [[ "$MYSQL_HOST" && ( "$MYSQL_HOST" != "localhost" ) ]]; then - MYSQL_CONNECTION="$MYSQL_CONNECTION --host=$MYSQL_HOST" - if [ "$MYSQL_PORT" ]; then - MYSQL_CONNECTION="$MYSQL_CONNECTION --port=$MYSQL_PORT" - fi - fi - # This option should come last, so we also have one version without the database - MYSQL_CONNECTION_NO_DB="$MYSQL_CONNECTION" - MYSQL_CONNECTION="$MYSQL_CONNECTION --database=$DATABASE_NAME" -fi -export MYSQL_CONNECTION -export MYSQL_CONNECTION_NO_DB -export DATABASE_NAME if [ -n "$PATCH" ]; then echo -ne "applying patch $PATCH to $DATABASE_NAME ... " fi - -function success { - echo "[OK]" - exit 0 -} - -function failure { - echo "[FAILED] $*" - exit 1 -} - -function uptodate { - echo "[UPTODATE]" - exit 0 -} # @param $1: db version string, e.g. v2.0.0 # @return: 0 on success, 1 on failure function check_version { @@ -157,24 +111,3 @@ function check_version { function update_version { mysql_execute "DROP FUNCTION IF EXISTS CaosDBVersion; CREATE FUNCTION CaosDBVersion() RETURNS VARCHAR(255) DETERMINISTIC RETURN '$1';" } - -function dump_table { - $MYSQLDUMP_CMD $MYSQL_CONNECTION_NO_DB $DATABASE_NAME $1 \ - > ${DATABASE_NAME}.${1}.${OLD_VERSION}.dump.sql -} - - -function mysql_execute { - set +e - $MYSQL_CMD $MYSQL_CONNECTION -e "$1" - ret=${PIPESTATUS[0]} - if [ "$ret" -ne 0 ]; then - failure "MYSQL ERROR" - fi - set -e -} - -function redo_table { - $MYSQL_CMD $MYSQL_CONNECTION < ${DATABASE_NAME}.${1}.${OLD_VERSION}.dump.sql -} - diff --git a/update_sql_procedures.sh b/update_sql_procedures.sh index 7b0b457f07456663645c383703e8c1c7ec962291..823f7eba99cc188089afb747120f8fa8140c688b 100755 --- a/update_sql_procedures.sh +++ b/update_sql_procedures.sh @@ -24,13 +24,13 @@ # Updates all SQL procedures -source patches/utils/patch_header.sh +. load_settings echo -n "updating rules ... " set -e mysql_execute "$(sed s/db_2_0/$DATABASE_NAME/g rules.sql)" -echo "[OK]" +success echo -n "updating procedures ... " mysql_execute "$(sed s/db_2_0/$DATABASE_NAME/g procedures/*.sql procedures/query/*.sql)" diff --git a/utils.sh b/utils.sh new file mode 100644 index 0000000000000000000000000000000000000000..c78116585f96ecd9bcc395ab768fbd7191a8b092 --- /dev/null +++ b/utils.sh @@ -0,0 +1,53 @@ +# This file is a part of the CaosDB Project. +# +# Copyright (C) 2018 Research Group Biomedical Physics, +# Max-Planck-Institute for Dynamics and Self-Organization Göttingen +# Copyright 2019 Daniel Hornung +# Copyright 2020 Henrik tom Wörden, IndiScale GmbH +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +function dump_table { + $MYSQLDUMP_CMD $MYSQL_CONNECTION_NO_DB $DATABASE_NAME $1 \ + > ${DATABASE_NAME}.${1}.${OLD_VERSION}.dump.sql +} + +function mysql_execute { + set +e + $MYSQL_CMD $MYSQL_CONNECTION -e "$1" + ret=${PIPESTATUS[0]} + if [ "$ret" -ne 0 ]; then + failure "MYSQL ERROR" + fi + set -e +} + +function redo_table { + $MYSQL_CMD $MYSQL_CONNECTION < ${DATABASE_NAME}.${1}.${OLD_VERSION}.dump.sql +} + +function success { + echo "[OK]" + exit 0 +} + +function failure { + echo "[FAILED] $*" + exit 1 +} + +function uptodate { + echo "[UPTODATE]" + exit 0 +}