forked from jsomers/lesswrong
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathload_db_dump.sh
More file actions
executable file
·78 lines (60 loc) · 1.91 KB
/
load_db_dump.sh
File metadata and controls
executable file
·78 lines (60 loc) · 1.91 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
#!/bin/bash
for arg in "$1" "$2"; do
if [ "EMPTY${arg}" = "EMPTY" ]; then
echo "Usage: load_db_dump.sh basedir lesswrong.ini"
echo "E.g. load_db_dump.sh /srv/www/lesswrong.org lesswrong.org.ini"
exit 2
fi
done
BASEDIR="$1"
INIFILE="$2" # E.g. lesswrong.org.ini
first_char=`echo "$BASEDIR" | grep -o '^/'`
if [ "EMPTY${first_char}" = "EMPTY" ]; then
echo "The basepath passed to this script must be an absolute path"
exit 2
fi
CURRENT="$BASEDIR/current"
SHARED="$BASEDIR/shared"
APPDIR="$CURRENT/r2"
if [ ! -e "$CURRENT" ]; then
echo "The base path doesn't appear to refer to a capistrano managed directory tree"
exit 2
fi
DUMPFILE="$CURRENT/db/dumps/serpent.trikeapps.com/production.psql.gz"
EXPORTDB="$SHARED/files/lesswrong.db"
PIDFILE="$SHARED/pids/paster.pid"
cd "$CURRENT"
# Fetch the remote production dump
cap production db:fetch_dump
cd "$APPDIR"
# Stop the current server
if [ -e "$PIDFILE" ]; then
echo "Stopping server"
sudo -u www-data paster serve --stop-daemon --pid-file "$PIDFILE" "$INIFILE"
fi
# Drop existing databases
for TABLE in changes email query_queue reddit; do
sudo -u postgres dropdb "$TABLE"
done
# Extract dump
DUMPEXTRACT=`echo "$DUMPFILE" | sed s/\\.gz$//`
rm -f "$DUMPEXTRACT" # gzip will abort if the target file exists
gunzip "$DUMPFILE"
# Load dump
sudo -u postgres psql -f "$DUMPEXTRACT"
# Restart memcache as the cache is now out of date
if [ -x /etc/init.d/memcached ]; then
sudo /etc/init.d/memcached restart
fi
# Start the server
if [ ! -e "$PIDFILE" ]; then
echo "Starting server"
sudo -u www-data paster serve --daemon --pid-file "$PIDFILE" "$INIFILE"
fi
# Run the export
paster run -c "export_to('${EXPORTDB}')" "$INIFILE" ../scripts/db_export.py
# Compress the export
rm -f "$EXPORTDB.bz2" # bzip2 will abort if the target file exists
bzip2 "$EXPORTDB"
# Let the group remove these files if needed (by Capistrano for example)
chmod -R g+w "$CURRENT/db"