From ea42be83722e71fa845a543b13d9049b589ee8ab Mon Sep 17 00:00:00 2001 From: Michael Vasseur <14887731+vmcj@users.noreply.github.com> Date: Thu, 19 Oct 2023 20:43:28 +0200 Subject: [PATCH] Add option to create database dump from sql script Loosely copied from the domjudge-scripts repo. Remove side effect Rewording remove prefix of "snapshot-" Move to output location Add warning on overwriting old database content Remove cli flag in favor of adding name as parameter Handle the comments --- Makefile | 1 + configure.ac | 3 +++ paths.mk.in | 4 +++- sql/dj_setup_database.in | 46 ++++++++++++++++++++++++++++++++++++---- 4 files changed, 49 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 84a61e6fd28..8dec83ba88c 100644 --- a/Makefile +++ b/Makefile @@ -196,6 +196,7 @@ inplace-conf-common: dist --with-domserver_tmpdir=$(CURDIR)/output/tmp \ --with-judgehost_tmpdir=$(CURDIR)/output/tmp \ --with-judgehost_judgedir=$(CURDIR)/output/judgings \ + --with-domserver_databasedumpdir=$(CURDIR)/output/db-dumps \ --with-baseurl='http://localhost/domjudge/' \ CFLAGS='$(MAINT_CXFLAGS) -std=c11' \ CXXFLAGS='$(MAINT_CXFLAGS) -std=c++11' \ diff --git a/configure.ac b/configure.ac index 3cebcf683f1..2739b096b6f 100644 --- a/configure.ac +++ b/configure.ac @@ -156,6 +156,7 @@ if test "x$FHS_ENABLED" = xyes ; then AC_SUBST(domserver_rundir, $localstatedir/run/${PACKAGE_TARNAME}) AC_SUBST(domserver_tmpdir, /tmp) AC_SUBST(domserver_exampleprobdir, $datadir/${PACKAGE_TARNAME}/example_problems) + AC_SUBST(domserver_databasedumpdir, $localstatedir/lib/${PACKAGE_TARNAME}/db-dumps) AC_SUBST(judgehost_root, '') AC_SUBST(judgehost_bindir, $bindir) @@ -200,6 +201,7 @@ AX_PATH(domserver_logdir, [$domserver_root/log]) AX_PATH(domserver_rundir, [$domserver_root/run]) AX_PATH(domserver_tmpdir, [$domserver_root/tmp]) AX_PATH(domserver_exampleprobdir, [$domserver_root/example_problems]) +AX_PATH(domserver_databasedumpdir, [$domserver_root/db-dumps]) AX_WITH_COMMENT(6,[ ]) AX_PATH(judgehost_bindir, [$judgehost_root/bin]) AX_PATH(judgehost_etcdir, [$judgehost_root/etc]) @@ -348,6 +350,7 @@ echo " - sql..............: AX_VAR_EXPAND($domserver_sqldir)" echo " - tmp..............: AX_VAR_EXPAND($domserver_tmpdir)" echo " - webapp...........: AX_VAR_EXPAND($domserver_webappdir)" echo " - example_problems.: AX_VAR_EXPAND($domserver_exampleprobdir)" +echo " - database_dumps...: AX_VAR_EXPAND($domserver_databasedumpdir)" echo "" echo " * judgehost...........: AX_VAR_EXPAND($judgehost_root)" echo " - bin..............: AX_VAR_EXPAND($judgehost_bindir)" diff --git a/paths.mk.in b/paths.mk.in index f5993e39e10..c687ed99335 100644 --- a/paths.mk.in +++ b/paths.mk.in @@ -85,6 +85,7 @@ domserver_logdir = @domserver_logdir@ domserver_rundir = @domserver_rundir@ domserver_tmpdir = @domserver_tmpdir@ domserver_exampleprobdir = @domserver_exampleprobdir@ +domserver_databasedumpdir = @domserver_databasedumpdir@ PHPVERSION = @PHPVERSION@ judgehost_bindir = @judgehost_bindir@ @@ -108,7 +109,7 @@ domserver_dirs = $(domserver_bindir) $(domserver_etcdir) \ $(domserver_libdir) $(domserver_libvendordir) \ $(domserver_logdir) $(domserver_rundir) \ $(addprefix $(domserver_webappdir)/public/images/,affiliations countries teams) \ - $(domserver_exampleprobdir) + $(domserver_exampleprobdir) $(domserver_databasedumpdir) judgehost_dirs = $(judgehost_bindir) $(judgehost_etcdir) $(judgehost_libdir) \ $(judgehost_libjudgedir) $(judgehost_logdir) \ @@ -136,6 +137,7 @@ define substconfigvars -e 's,@domserver_rundir[@],@domserver_rundir@,g' \ -e 's,@domserver_tmpdir[@],@domserver_tmpdir@,g' \ -e 's,@domserver_exampleprobdir[@],@domserver_exampleprobdir@,g' \ + -e 's,@domserver_databasedumpdir[@],@domserver_databasedumpdir@,g' \ -e 's,@judgehost_bindir[@],@judgehost_bindir@,g' \ -e 's,@judgehost_etcdir[@],@judgehost_etcdir@,g' \ -e 's,@judgehost_libdir[@],@judgehost_libdir@,g' \ diff --git a/sql/dj_setup_database.in b/sql/dj_setup_database.in index b04f62e7d01..9db01ca418f 100755 --- a/sql/dj_setup_database.in +++ b/sql/dj_setup_database.in @@ -10,6 +10,7 @@ BINDIR="@domserver_bindir@" ETCDIR="@domserver_etcdir@" WEBAPPDIR="@domserver_webappdir@" EXAMPLEPROBDIR="@domserver_exampleprobdir@" +DATABASEDUMPDIR="@domserver_databasedumpdir@" PASSWDFILE="$ETCDIR/dbpasswords.secret" @@ -22,7 +23,7 @@ verbose() usage() { cat < +Usage: $0 [option]... [argument] Commands: status check database installation status @@ -34,6 +35,7 @@ Commands: install-examples install examples only install-loadtest configure for load testing. WARNING: CREATES A LOT OF EXTRA ITEMS! upgrade upgrade MySQL database schema to current version + dump [filename] backup the current database to file (without .tar.gz suffix) Options: -u connect to MySQL with DB admin @@ -48,8 +50,7 @@ not have to pass any of the options above. EOF } -# Wrapper around mysql command to allow setting options, user, etc. -mysql() +mysql_options() { local user pass @@ -68,7 +69,13 @@ mysql() fi [ -z "$USE_SOCKET" ] && port="-P$DBPORT" - command mysql $user ${pass:+"$pass"} -h "$DBHOST" ${port:+"$port"} --silent --skip-column-names "$@" + echo $user ${pass:+"$pass"} -h "$DBHOST" ${port:+"$port"} +} + +# Wrapper around mysql command to allow setting options, user, etc. +mysql() +{ + command mysql $(mysql_options) --silent --skip-column-names "$@" } # Quick shell hack to get a key from an INI file. @@ -234,6 +241,10 @@ install_examples() ( cd "$EXAMPLEPROBDIR" && yes y | "$BINDIR"/import-contest ) } +create_database_dump () { + sudo mysqldump $(mysql_options) --opt --skip-lock-tables domjudge | pv | gzip > "$DATABASEDUMPDIR/${1}.sql.gz" +} + ### Script starts here ### # Parse command-line options: @@ -354,6 +365,33 @@ upgrade) verbose "DOMjudge database upgrade completed." ;; +dump) + read_dbpasswords + DUMPNAME="$2" + if [ -z "$DUMPNAME" ]; then + usage + exit 1 + fi + + if [ -f "${DATABASEDUMPDIR}/${DUMPNAME}.sql.gz" ]; then + while true; do + read -p "Overwrite existing database dump (y/N)? " yn + case $yn in + [Yy]* ) create_database_dump $DUMPNAME; exit 0 ;; + ''|[Nn]* ) break;; + esac + done + else + create_database_dump $DUMPNAME; exit 0 + fi + + read -p "Provide prefix or say 'no' to abort. Format: [prefix]-$1 " prefix + case $prefix in + ''|[Nn]o|[Nn] ) exit 2; break;; + * ) create_database_dump ${prefix}-${DUMPNAME}; break;; + esac + ;; + *) echo "Error: Unknown subcommand '$1'" usage