Skip to content
Snippets Groups Projects
Commit 4f995f06 authored by Daniel's avatar Daniel
Browse files

ENH: Starting to make backup.sh print a nice YAML message.

parent 03785b31
No related branches found
No related tags found
No related merge requests found
...@@ -22,23 +22,54 @@ ...@@ -22,23 +22,54 @@
# ** end header # ** end header
# #
#dump a database with all procedures, permissions, structure and data # 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! # The directory which the dump is to be stored to. Do not change it here. Use
PRINT_HELP="--backupdir=BACKUPDIR\n\tThe directory which the dump is to be stored to. (Defaults to ./backup)\n" # 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 #######################################################
YAMLFAIL=$(cat << "EOF"
#### YAML ####
error:
- code: $error_code
- message: $error_message
#### YAML END ####
EOF
)
YAMLSUCCESS=$(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 . .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
# load useful stuff - parses the commandline parameters and so on...
. patches/utils/patch_header.sh
echo "BACKUPDIR: $BACKUPDIR"
echo "YAML: $YAML"
function backup { function backup() {
NARG_NAME=$1 NARG_NAME="$1"
NARG_FILE=$2 NARG_FILE="$2"
shift 2 shift 2
echo $MYSQLDUMP_CMD echo $MYSQLDUMP_CMD
# parameters: connection, database, outfile # parameters: connection, database, outfile
...@@ -50,15 +81,31 @@ function backup { ...@@ -50,15 +81,31 @@ function backup {
success success
} }
# Prints 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"
}
# Prints a YAML success message, if YAML is true.
function yaml_success() {
}
##test dump file exists ##test dump file exists
#touch dumpfile.tmp #touch dumpfile.tmp
#backup 0 1 2 "dumpfile.tmp" #backup 0 1 2 "dumpfile.tmp"
#rm dumpfile.tmp #rm dumpfile.tmp
# create backup dir if not exists
[ -d "$BACKUPDIR" ] || mkdir "$BACKUPDIR" ||
DATE=$(date -u --rfc-3339=ns | sed 's/ /T/g') DATE=$(date -u --rfc-3339=ns | sed 's/ /T/g')
BACKUPFILE=${BACKUPDIR}/${DATABASE_NAME}.${DATE}.dump.sql BACKUPFILE=${BACKUPDIR}/${DATABASE_NAME}.${DATE}.dump.sql
......
...@@ -25,7 +25,7 @@ ...@@ -25,7 +25,7 @@
CMD_MYSQL=mysql CMD_MYSQL=mysql
CMD_MYSQL_DUMP=mysqldump CMD_MYSQL_DUMP=mysqldump
USAGE="$1 [ --env=ENV_FILE ] [ --patch=PATCH ] [ --backupdir=BACKUPDIR ]\n\n" USAGE="$1 [ --env=ENV_FILE ] [ --patch=PATCH ] [ --backupdir=BACKUPDIR ]\n\n"
CMD_OPTIONS=cat <<EOF CMD_OPTIONS=$(cat <<EOF
options: options:
-h, --help -h, --help
...@@ -37,7 +37,10 @@ options: ...@@ -37,7 +37,10 @@ options:
--patch=PATCH --patch=PATCH
*TODO* Please document this option. *TODO* Please document this option.
--backupdir=BACKUPDIR --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 This script basically uses the same environment variables as the server
configuration make file. Notable examples are: configuration make file. Notable examples are:
...@@ -50,6 +53,7 @@ configuration make file. Notable examples are: ...@@ -50,6 +53,7 @@ configuration make file. Notable examples are:
- DATABASE_USER_PW - DATABASE_USER_PW
EOF EOF
)
set -e set -e
function _print_help() { function _print_help() {
...@@ -79,7 +83,14 @@ while test $# -gt 0; do ...@@ -79,7 +83,14 @@ while test $# -gt 0; do
;; ;;
--backupdir*) --backupdir*)
BACKUPDIR="${1#--*=}" BACKUPDIR="${1#--*=}"
echo "Warning: BACKUPDIR is not used currently." shift
;;
--yaml*)
if [[ "$1" == "--yaml" ]] ; then
YAML="True"
else
YAML="${1#--*=}"
fi
shift shift
;; ;;
*) *)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment