forked from adamlaska/datatracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdatedb
More file actions
executable file
·158 lines (127 loc) · 4.8 KB
/
updatedb
File metadata and controls
executable file
·158 lines (127 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/bin/bash
# -*- indent-with-tabs: 1 -*-
version=0.10
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
egrep "^[ ]+[-][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
egrep "^[ ]+[-][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 note() {
if [ -n "$VERBOSE" ]; then echo -e "$*"; fi
}
# ----------------------------------------------------------------------
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,quiet,verbose,version
LOAD=1
DOWNLOAD=1
DROP=1
QUIET=""
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"
sed="sed -r"
else
# Darwin, BSDs
args=$(getopt -o$shortopts $SV $*)
if [ $? != 0 ] ; then die "Terminating..." >&2 ; exit 1 ; fi
set -- $args
sed="sed -E"
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
-q| --quiet) QUIET="-q";; # Don't show progress
-v| --verbose) VERBOSE=1;; # Be more talkative
-V| --version) version; exit;; # Show program version, then exit
--) shift; break;;
*) die "Internal error, inconsistent option specification: '$1'";;
esac
shift
done
# ----------------------------------------------------------------------
# The program itself
echo "Gathering info ..."
MYSQLDIR="$(/usr/sbin/mysqld --verbose --help 2>/dev/null | awk '$1 == "datadir" { print $2; exit }')"
MYSQLDIR=${MYSQLDIR%/}
DATADIR=$parent/data
# echo "Checking if MySQL base data exists ..."
# if [ ! -d $MYSQLDIR/mysql ]; then
# die "Expected the directory $MYSQLDIR/mysql/ to exist -- have you downloaded and unpacked the IETF binary database tarball?"
# fi
# echo "Checking if the IETF database exists at $MYSQLDIR ..."
# if [ ! -d $MYSQLDIR/ietf_utf8 ]; then
# echo "Creating database ..."
# mysqladmin -u root --default-character-set=utf8 create ietf_utf8
#
# echo "Setting up permissions ..."
# mysql -u root ietf_utf8 <<< "GRANT ALL PRIVILEGES ON ietf_utf8.* TO django@localhost IDENTIFIED BY 'RkTkDPFnKpko'; FLUSH PRIVILEGES;"
# fi
if [ "$DOWNLOAD" ]; then
echo "Fetching database dump ..."
wget $QUIET -N -P $DATADIR https://www.ietf.org/lib/dt/sprint/ietf_utf8.sql.gz || die "No new data, quitting."
fi
if [ "$LOAD" ]; then
echo "Loading database ..."
if [ -z "$QUIET" ]; then
gunzip < $DATADIR/ietf_utf8.sql.gz \
| pv --progress --bytes --rate --eta --size $(gzip --list --quiet $DATADIR/ietf_utf8.sql.gz | awk '{ print $2 }') \
| sed -e 's/ENGINE=MyISAM/ENGINE=InnoDB/' \
| $parent/ietf/manage.py dbshell
else
gunzip < $DATADIR/ietf_utf8.sql.gz \
| sed -e 's/ENGINE=MyISAM/ENGINE=InnoDB/' \
| $parent/ietf/manage.py dbshell
fi
fi
if [ "$DROP" ]; then
echo "Dropping tables not in the dump (so migrations can succeed) ..."
diff <(zcat $DATADIR/ietf_utf8.sql.gz | 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