Skip to content

Commit

Permalink
Add option to create database dump from sql script
Browse files Browse the repository at this point in the history
Loosely copied from the domjudge-scripts repo.
  • Loading branch information
vmcj committed Oct 19, 2023
1 parent 74535eb commit 26ca155
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
3 changes: 3 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -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,$datadir/${PACKAGE_TARNAME}/db-dumps)

AC_SUBST(judgehost_root, '')
AC_SUBST(judgehost_bindir, $bindir)
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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)"
Expand Down
1 change: 1 addition & 0 deletions paths.mk.in
Original file line number Diff line number Diff line change
Expand Up @@ -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@
Expand Down
39 changes: 38 additions & 1 deletion sql/dj_setup_database.in
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ BINDIR="@domserver_bindir@"
ETCDIR="@domserver_etcdir@"
WEBAPPDIR="@domserver_webappdir@"
EXAMPLEPROBDIR="@domserver_exampleprobdir@"
DATABASEDUMPDIR="@domserver_databasedumpdir@"

PASSWDFILE="$ETCDIR/dbpasswords.secret"

Expand All @@ -34,6 +35,8 @@ 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
snapshot-dump backup the current database to file (without .tar.gz)
snapshot-load load a backup from file (without.tar.gz)
Options:
-u <user> connect to MySQL with DB admin <user>
Expand Down Expand Up @@ -234,10 +237,14 @@ install_examples()
( cd "$EXAMPLEPROBDIR" && yes y | "$BINDIR"/import-contest )
}

create_database_dump () {
sudo mysqldump --opt --skip-lock-tables domjudge | pv | gzip > "$DATABASEDUMPDIR/${1}.sql.gz"
}

### Script starts here ###

# Parse command-line options:
while getopts ':u:p:qrs' OPT ; do
while getopts ':u:p:d:qrs' OPT ; do
case "$OPT" in
u)
DBA_USER=$OPTARG
Expand All @@ -254,6 +261,9 @@ while getopts ':u:p:qrs' OPT ; do
s)
USE_SOCKET=1
;;
d)
DUMPNAME=$OPTARG
;;
:)
echo "Error: option '$OPTARG' requires an argument."
usage
Expand Down Expand Up @@ -354,6 +364,33 @@ upgrade)
verbose "DOMjudge database upgrade completed."
;;

snapshot-dump)
echo $@
echo $DUMPNAME
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
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
Expand Down

0 comments on commit 26ca155

Please sign in to comment.