datatracker/docker/scripts/updatedb
2021-11-10 21:51:55 +00:00

105 lines
2.9 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
version=0.20
program=${0##*/}
progdir=${0%/*}
if [ "$progdir" = "$program" ]; then progdir="."; fi
if [ "$progdir" = "." ]; then progdir="$PWD"; fi
parent=$(dirname "$progdir")
if [ "$parent" = "." ]; then parent="$PWD"; fi
if [[ $(uname) =~ CYGWIN.* ]]; then parent=$(echo "$parent" | sed -e 's/^\/cygdrive\/\(.\)/\1:/'); fi
function usage() {
cat <<EOF
NAME
$program - Update the local copy of the IETF database from a dump
SYNOPSIS
$program [OPTIONS] ARGS
DESCRIPTION
This script downloads a dump of the IETF database and loads into the
local sql server if it is newer than the current dump.
OPTIONS
EOF
grep -E '^\s+-[a-zA-Z])' "$0" | sed -E -e 's/\)[^#]+#/ /'
cat <<EOF
AUTHOR
Written by:
Henrik Levkowetz, <henrik@levkowetz.com>
Lars Eggert, <lars@eggert.org>
COPYRIGHT
Copyright (c) 2016 IETF Trust and the persons identified as authors of
the code. All rights reserved. Redistribution and use in source and
binary forms, with or without modification, is permitted pursuant to,
and subject to the license terms contained in, the Revised BSD
License set forth in Section 4.c of the IETF Trusts Legal Provisions
Relating to IETF Documents(https://trustee.ietf.org/license-info).
EOF
}
function die() {
echo -e "\n$program: error: $*" >&2
exit 1
}
function version() {
echo -e "$program $version"
}
trap 'echo "$program($LINENO): Command failed with error code $? ([$$] $0 $*)"; exit 1' ERR
# Option parsing
shortopts=DLZhV
LOAD=1
DOWNLOAD=1
DROP=1
args=$(getopt -o$shortopts $*)
if [ $? != 0 ] ; then die "Terminating..." >&2 ; exit 1 ; fi
set -- $args
while true ; do
case "$1" in
-D) DOWNLOAD="";; # Don't download, use existing file
-L) LOAD=""; ;; # Don't load the database
-Z) DROP="";; # Don't drop new tables
-h) usage; exit;; # Show this help, then exit
-V) version; exit;; # Show program version, then exit
--) shift; break;;
*) die "Internal error, inconsistent option specification: '$1'";;
esac
shift
done
# The program itself
DATADIR=$parent/data
DUMP=ietf_utf8.sql.gz
if [ "$DOWNLOAD" ]; then
echo "Fetching database dump..."
rsync --info=progress2 rsync.ietf.org::dev.db/$DUMP "$DATADIR"
fi
if [ "$LOAD" ]; then
echo "Loading database..."
SIZE=$(pigz --list "$DATADIR/$DUMP" | tail -n 1 | awk '{ print $2 }')
pigz -d < "$DATADIR/$DUMP" \
| pv --progress --bytes --rate --eta --size "$SIZE" \
| sed -e 's/ENGINE=MyISAM/ENGINE=InnoDB/' \
| "$parent/ietf/manage.py" dbshell
fi
if [ "$DROP" ]; then
echo "Dropping tables not in the dump (so migrations can succeed)..."
diff <(pigz -d -c "$DATADIR/$DUMP" | grep '^DROP TABLE IF EXISTS' | tr -d '`;' | awk '{ print $5 }') \
<("$parent/ietf/manage.py" dbshell <<< 'show tables;' | tail -n +2) \
| grep '^>' | awk '{print "drop table if exists", $2, ";";}' \
| tee >(cat >&2) | "$parent/ietf/manage.py" dbshell
fi