126 lines
3.6 KiB
Bash
Executable file
126 lines
3.6 KiB
Bash
Executable file
#!/bin/bash
|
|
# -*- indent-with-tabs: 1 -*-
|
|
|
|
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
|
|
|
|
# ----------------------------------------------------------------------
|
|
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.
|
|
|
|
EOF
|
|
echo -e "OPTIONS"
|
|
if [ "$(uname)" = "Linux" ]; then
|
|
grep -E "^[ ]+[-][A-Za-z| -]+\*?\)[ ]+[A-Za-z].+#" "$0" | tr -s "\t|" "\t," | sed -r -e 's/\)[ \t]+([A-Z]+)=\$2[^#]*#/=\1\t/' -e 's/\)[^#]*#/\t/'
|
|
else
|
|
grep -E "^[ ]+[-][A-Za-z| -]+\*?\)[ ]+[A-Za-z].+#" "$0" | sed 's/\|.*\$2[^#]*#/ /'| sed -E 's/\|.*\)[^#]*#/ /'
|
|
fi
|
|
cat <<EOF
|
|
|
|
FILES
|
|
|
|
AUTHOR
|
|
Written by Henrik Levkowetz, <henrik@levkowetz.com>
|
|
|
|
COPYRIGHT
|
|
|
|
Copyright (c) 2015 IETF Trust and the persons identified as authors of
|
|
the code. All rights reserved. License 'Simplified BSD', as specified
|
|
in http://opensource.org/licenses/BSD-3-Clause.
|
|
|
|
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
|
|
|
|
# Options
|
|
shortopts=DLZhqvV
|
|
longopts=no-download,no-load,no-zap,help,version
|
|
|
|
LOAD=1
|
|
DOWNLOAD=1
|
|
DROP=1
|
|
|
|
if [ "$(uname)" = "Linux" ]; then
|
|
args=$(getopt -o "$shortopts" --long "$longopts" -n "$program" -- $SV "$@")
|
|
if [ $? != 0 ] ; then die "Terminating..." >&2 ; exit 1 ; fi
|
|
eval set -- "$args"
|
|
else
|
|
# Darwin, BSDs
|
|
args=$(getopt -o$shortopts $SV $*)
|
|
if [ $? != 0 ] ; then die "Terminating..." >&2 ; exit 1 ; fi
|
|
set -- $args
|
|
fi
|
|
|
|
while true ; do
|
|
case "$1" in
|
|
-D| --no-download) DOWNLOAD="";; #Don't download, use existing file
|
|
-L| --no-load) LOAD=""; ;; # Don't load the database
|
|
-Z| --no-zap) DROP="";; # Don't drop new tables
|
|
-h| --help) usage; exit;; # Show this help, then exit
|
|
-V| --version) 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 |