From 63f8d1d2a5a96abeadefbd63e789bf1b410dae1a 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 load database dump from the sql scripts Loosely copied from the domjudge-scripts repo to add an option for reloading an existing dump. We've used this sort of script for multiple years, this makes sure we have the right setups around the tables by making sure the database is in a current state (users etc.). Co-authored-by: Jaap Eldering --- doc/manual/install-domserver.rst | 4 +-- sql/dj_setup_database.in | 56 +++++++++++++++++++++++++++++--- 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/doc/manual/install-domserver.rst b/doc/manual/install-domserver.rst index 4d8f06e0c0..4695340519 100644 --- a/doc/manual/install-domserver.rst +++ b/doc/manual/install-domserver.rst @@ -35,14 +35,14 @@ For your convenience, the following command will install the necessary software on the DOMjudge server as mentioned above when using Debian GNU/Linux, or one of its derivative distributions like Ubuntu:: - sudo apt install libcgroup-dev make acl zip unzip mariadb-server apache2 \ + sudo apt install libcgroup-dev make acl zip unzip pv mariadb-server apache2 \ php php-fpm php-gd php-cli php-intl php-mbstring php-mysql \ php-curl php-json php-xml php-zip composer ntp python3-yaml The following command can be used on Fedora, and related distributions like Red Hat Enterprise Linux and Rocky Linux (before V9):: - sudo dnf install libcgroup-devel make acl zip unzip mariadb-server httpd \ + sudo dnf install libcgroup-devel make acl zip unzip pv mariadb-server httpd \ php-gd php-cli php-intl php-mbstring php-mysqlnd php-fpm \ php-xml php-zip composer chronyd python3-pyyaml diff --git a/sql/dj_setup_database.in b/sql/dj_setup_database.in index eff258a028..7e473507ef 100755 --- a/sql/dj_setup_database.in +++ b/sql/dj_setup_database.in @@ -36,6 +36,7 @@ Commands: 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 .sql.gz suffix) + load [filename] load a backup from file (without .sql.gz suffix), REMOVES ALL PREVIOUS DATA! Options: -u connect to MySQL with DB admin @@ -241,6 +242,19 @@ install_examples() ( cd "$EXAMPLEPROBDIR" && yes y | "$BINDIR"/import-contest ) } +uninstall_helper() +{ + read_dbpasswords + remove_db_users +} + +create_db_users_helper() +{ + read_dbpasswords + create_db_users + verbose "Created empty database and users." +} + create_database_dump () { sudo mysqldump $(mysql_options) --opt --skip-lock-tables domjudge | pv | gzip > "$DATABASEDUMPDIR/${1}.sql.gz" } @@ -301,8 +315,7 @@ genpass) ;; uninstall) - read_dbpasswords - remove_db_users + uninstall_helper ;; install-examples) @@ -321,9 +334,7 @@ install-loadtest) ;; create-db-users) - read_dbpasswords - create_db_users - verbose "Created empty database and users." + create_db_users_helper ;; bare-install|install) @@ -386,6 +397,41 @@ dump) exit 0 ;; +load) + choice="" + DUMPNAME="$2" + if [ -z "$DUMPNAME" ]; then + set -- $(ls $DATABASEDUMPDIR) + ind=0 + while true; do + read -p "Which database should be loaded? " db + ind=0 + for i in $@; do + if [ "$ind" = "$db" ]; then + choice="$i" + break + fi + : $((ind+=1)) + done + if [ -n "$choice" ]; then + break + fi + done + else + choice="${DUMPNAME}.sql.gz" + fi + FILE="$DATABASEDUMPDIR/${choice}" + + if [ ! -f "${FILE}" ]; then + echo "Error: file ${FILE} not found." + exit 1 + fi + + uninstall_helper + create_db_users_helper + pv "${FILE}" | gunzip | mysql domjudge + ;; + *) echo "Error: Unknown subcommand '$1'" usage