Skip to content

Commit 4acbb89

Browse files
vmcjeldering
andcommitted
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 <[email protected]>
1 parent 30f8858 commit 4acbb89

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

doc/manual/install-domserver.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ For your convenience, the following command will install the necessary
3535
software on the DOMjudge server as mentioned above when using Debian
3636
GNU/Linux, or one of its derivative distributions like Ubuntu::
3737

38-
sudo apt install libcgroup-dev make acl zip unzip mariadb-server apache2 \
38+
sudo apt install libcgroup-dev make acl zip unzip pv mariadb-server apache2 \
3939
php php-fpm php-gd php-cli php-intl php-mbstring php-mysql \
4040
php-curl php-json php-xml php-zip composer ntp python3-yaml
4141

4242
The following command can be used on Fedora, and related distributions like
4343
Red Hat Enterprise Linux and Rocky Linux (before V9)::
4444

45-
sudo dnf install libcgroup-devel make acl zip unzip mariadb-server httpd \
45+
sudo dnf install libcgroup-devel make acl zip unzip pv mariadb-server httpd \
4646
php-gd php-cli php-intl php-mbstring php-mysqlnd php-fpm \
4747
php-xml php-zip composer chronyd python3-pyyaml
4848

sql/dj_setup_database.in

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Commands:
3636
install-loadtest configure for load testing. WARNING: CREATES A LOT OF EXTRA ITEMS!
3737
upgrade upgrade MySQL database schema to current version
3838
dump [filename] backup the current database to file (without .sql.gz suffix)
39+
load [filename] load a backup from file (without .sql.gz suffix), REMOVES ALL PREVIOUS DATA!
3940
4041
Options:
4142
-u <user> connect to MySQL with DB admin <user>
@@ -241,6 +242,19 @@ install_examples()
241242
( cd "$EXAMPLEPROBDIR" && yes y | "$BINDIR"/import-contest )
242243
}
243244

245+
uninstall_helper()
246+
{
247+
read_dbpasswords
248+
remove_db_users
249+
}
250+
251+
create_db_users_helper()
252+
{
253+
read_dbpasswords
254+
create_db_users
255+
verbose "Created empty database and users."
256+
}
257+
244258
create_database_dump () {
245259
sudo mysqldump $(mysql_options) --opt --skip-lock-tables domjudge | pv | gzip > "$DATABASEDUMPDIR/${1}.sql.gz"
246260
}
@@ -301,8 +315,7 @@ genpass)
301315
;;
302316

303317
uninstall)
304-
read_dbpasswords
305-
remove_db_users
318+
uninstall_helper
306319
;;
307320

308321
install-examples)
@@ -321,9 +334,7 @@ install-loadtest)
321334
;;
322335

323336
create-db-users)
324-
read_dbpasswords
325-
create_db_users
326-
verbose "Created empty database and users."
337+
create_db_users_helper
327338
;;
328339

329340
bare-install|install)
@@ -386,6 +397,41 @@ dump)
386397
exit 0
387398
;;
388399

400+
load)
401+
choice=""
402+
DUMPNAME="$2"
403+
if [ -z "$DUMPNAME" ]; then
404+
set -- $(ls $DATABASEDUMPDIR)
405+
ind=0
406+
while true; do
407+
read -p "Which database should be loaded? " db
408+
ind=0
409+
for i in $@; do
410+
if [ "$ind" = "$db" ]; then
411+
choice="$i"
412+
break
413+
fi
414+
: $((ind+=1))
415+
done
416+
if [ -n "$choice" ]; then
417+
break
418+
fi
419+
done
420+
else
421+
choice="${DUMPNAME}.sql.gz"
422+
fi
423+
FILE="$DATABASEDUMPDIR/${choice}"
424+
425+
if [ ! -f "${FILE}" ]; then
426+
echo "Error: file ${FILE} not found."
427+
exit 1
428+
fi
429+
430+
uninstall_helper
431+
create_db_users_helper
432+
pv "${FILE}" | gunzip | mysql domjudge
433+
;;
434+
389435
*)
390436
echo "Error: Unknown subcommand '$1'"
391437
usage

0 commit comments

Comments
 (0)