diff --git a/backup.sh b/backup.sh deleted file mode 100755 index f8fee2ec1eff8664058d113a3763ddcf75634e54..0000000000000000000000000000000000000000 --- a/backup.sh +++ /dev/null @@ -1,67 +0,0 @@ -#!/bin/bash -# -# ** header v3.0 -# 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 -# -# 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/>. -# -# ** end header -# - -#dump a database with all procedures, permissions, structure and data - -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 - -#create backup dir if not exists -[ -d $BACKUPDIR ] || mkdir $BACKUPDIR - - - -function backup { - NARG_NAME=$1 - NARG_FILE=$2 - shift 2 - echo $MYSQLDUMP_CMD - # parameters: connection, database, outfile - if [ -e "$NARG_FILE" ]; then - failure "dumpfile already exists." - fi - echo "Dumping database $NARG_NAME to $NARG_FILE ... " - $CMD_MYSQL_DUMP $MYSQL_CONNECTION_NO_DB $* --opt --default-character-set=utf8 --routines $NARG_NAME > $NARG_FILE - 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/patches/utils/patch_header.sh b/patches/utils/patch_header.sh index 7cb477b93f635679f8fdf529623fa36663e49eaa..bc0045c8a8f43a9761c27d7cc16fb4f719df8e51 100644 --- a/patches/utils/patch_header.sh +++ b/patches/utils/patch_header.sh @@ -25,7 +25,7 @@ CMD_MYSQL=mysql CMD_MYSQL_DUMP=mysqldump USAGE="$1 [ --env=ENV_FILE ] [ --patch=PATCH ] [ --backupdir=BACKUPDIR ]\n\n" -CMD_OPTIONS=cat <<EOF +CMD_OPTIONS=$(cat <<EOF options: -h, --help @@ -37,7 +37,10 @@ options: --patch=PATCH *TODO* Please document this option. --backupdir=BACKUPDIR - *TODO* Please document this option. + The directory where backup files are stored to. +--yaml[=True|False] + If given (and the optional value is not False), additional machine-readable + YAML output is emitted. Only implemented for some commands yet. This script basically uses the same environment variables as the server configuration make file. Notable examples are: @@ -50,6 +53,7 @@ configuration make file. Notable examples are: - DATABASE_USER_PW EOF + ) set -e function _print_help() { @@ -79,7 +83,14 @@ while test $# -gt 0; do ;; --backupdir*) BACKUPDIR="${1#--*=}" - echo "Warning: BACKUPDIR is not used currently." + shift + ;; + --yaml*) + if [[ "$1" == "--yaml" ]] ; then + YAML="True" + else + YAML="${1#--*=}" + fi shift ;; *) diff --git a/utils/backup.sh b/utils/backup.sh new file mode 100755 index 0000000000000000000000000000000000000000..ff671437f42832c8c66ba250f777c4aa8d3f0c88 --- /dev/null +++ b/utils/backup.sh @@ -0,0 +1,139 @@ +#!/bin/bash +# +# ** header v3.0 +# 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 +# +# 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/>. +# +# ** end header +# + +# Dump a database with all procedures, permissions, structure and data + +# The directory which the dump is to be stored to. Do not change it here. Use +# the --backupdir=./my/dir/ option or an environment variable instead. +BACKUPDIR="${BACKUPDIR:-../backup}" + +# Iff this (environment) variable is "true" as lower-case, then this script also +# outputs a machine readable status report. The variable can also be set by +# giving the `--yaml` command line option. +YAML="${YAML:-false}" + +PRINT_HELP="--backupdir=BACKUPDIR\n\tThe directory which the dump is to be stored to. (Defaults to ./backup,\n\tcan also be set as an environment variable.)\n" + +# YAML string templates ####################################################### + +YAML_FAIL=$(cat << "EOF" +#### YAML #### +error: + code: $error_code + message: $error_message +#### YAML END #### +EOF + ) + +YAML_SUCCESS=$(cat << "EOF" +#### YAML #### +error: + code: 0 + message: Backup successfully created +backup: + location: $file_location + date: $date +#### YAML END #### +EOF + ) + +# Config and command line parameter loading ################################### + +. ../.config + +# load useful stuff - parses the commandline parameters and so on... +. ../patches/utils/patch_header.sh + +function backup() { + NARG_NAME="$1" + NARG_FILE="$2" + + shift 2 + # parameters: connection, database, outfile + if [ -e "$NARG_FILE" ]; then + failure "dumpfile already exists." + fi + echo "Dumping database $NARG_NAME to $NARG_FILE ... " + $CMD_MYSQL_DUMP $MYSQL_CONNECTION_NO_DB $* --opt --default-character-set=utf8 --routines \ + "$NARG_NAME" > "$NARG_FILE" + + if [[ "$YAML" == "true" ]] ; then + yaml_success "$NARG_FILE" "$DATE" + fi + success +} + +# Print a YAML failure message, if YAML is true. +# +# Arguments: +# - $1 Error code, integer > 0 +# - $2 Error message +function yaml_fail() { + err_code="$1" + err_msg="$2" + local yaml="${YAML_FAIL/\$err_code/$err_code}" + yaml="${yaml/\$err_msg/$err_msg}" + echo "$yaml" +} + +# Print a YAML success message, if YAML is true. +# +# Arguments: +# - $1 The file location of the backup file. +# - $2 The current date & time +function yaml_success() { + file_location="$1" + date="$2" + local yaml="${YAML_SUCCESS/\$file_location/$file_location}" + yaml="${yaml/\$date/$date}" + echo "$yaml" +} + +## test if dump file exists +#touch dumpfile.tmp +#backup 0 1 2 "dumpfile.tmp" +#rm dumpfile.tmp + +# Assert backup dir +mkdir -p "$BACKUPDIR" + +DATE=$(date -u --rfc-3339=ns | sed 's/ /T/g') +BACKUPFILE=${BACKUPDIR}/${DATABASE_NAME}.${DATE}.dump.sql + +# echo "date: $date" >&2 +# echo "file_location: $file_location" >&2 + +#YAML_SUCCESS=$(cat <<"EOF" +read -r -d '' YAML_SUCCESS <<"EOF" || true +#### YAML #### +error: + code: 0 + message: Backup successfully created +backup: + location: $file_location + date: $date +#### YAML END #### +EOF + +backup $DATABASE_NAME $BACKUPFILE