Skip to content

Commit

Permalink
Add option to load database dump from the sql scripts
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
vmcj and eldering committed Mar 13, 2024
1 parent 30f8858 commit 4acbb89
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
4 changes: 2 additions & 2 deletions doc/manual/install-domserver.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
56 changes: 51 additions & 5 deletions sql/dj_setup_database.in
Original file line number Diff line number Diff line change
Expand Up @@ -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 <user> connect to MySQL with DB admin <user>
Expand Down Expand Up @@ -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"
}
Expand Down Expand Up @@ -301,8 +315,7 @@ genpass)
;;

uninstall)
read_dbpasswords
remove_db_users
uninstall_helper
;;

install-examples)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4acbb89

Please sign in to comment.