diff --git a/utils/backup.sh b/utils/backup.sh index c1ab8ca72bf0d15a0faa76be783e51650cd1c92b..ff671437f42832c8c66ba250f777c4aa8d3f0c88 100755 --- a/utils/backup.sh +++ b/utils/backup.sh @@ -27,6 +27,7 @@ # 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. @@ -36,7 +37,7 @@ PRINT_HELP="--backupdir=BACKUPDIR\n\tThe directory which the dump is to be store # YAML string templates ####################################################### -YAMLFAIL=$(cat << "EOF" +YAML_FAIL=$(cat << "EOF" #### YAML #### error: code: $error_code @@ -45,7 +46,7 @@ error: EOF ) -YAMLSUCCESS=$(cat << "EOF" +YAML_SUCCESS=$(cat << "EOF" #### YAML #### error: code: 0 @@ -64,25 +65,26 @@ EOF # load useful stuff - parses the commandline parameters and so on... . ../patches/utils/patch_header.sh -echo "BACKUPDIR: $BACKUPDIR" -echo "YAML: $YAML" - 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 + "$NARG_NAME" > "$NARG_FILE" + + if [[ "$YAML" == "true" ]] ; then + yaml_success "$NARG_FILE" "$DATE" + fi success } -# Prints a YAML failure message, if YAML is true. +# Print a YAML failure message, if YAML is true. # # Arguments: # - $1 Error code, integer > 0 @@ -95,24 +97,35 @@ function yaml_fail() { echo "$yaml" } -# Prints a YAML success message, if YAML is true. +# 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 dump file exists +## test if dump file exists #touch dumpfile.tmp #backup 0 1 2 "dumpfile.tmp" #rm dumpfile.tmp -# create backup dir if not exists +# Assert backup dir mkdir -p "$BACKUPDIR" DATE=$(date -u --rfc-3339=ns | sed 's/ /T/g') BACKUPFILE=${BACKUPDIR}/${DATABASE_NAME}.${DATE}.dump.sql -#YAMLSUCCESS=$(cat <<"EOF" -read -r -d '' YAMLSUCCESS <<"EOF" || true +# 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 @@ -123,10 +136,4 @@ backup: #### YAML END #### EOF -echo "YAMLSUCCESS:" -echo "$YAMLSUCCESS" - - backup $DATABASE_NAME $BACKUPFILE - -