- clean up migrate_db, optimize main loop
[myplc.git] / plc.d / db
1 #!/bin/bash
2 #
3 # priority: 800
4 #
5 # Bootstrap the database
6 #
7 # Mark Huang <mlhuang@cs.princeton.edu>
8 # Copyright (C) 2006 The Trustees of Princeton University
9 #
10 # $Id$
11 #
12
13 # Source function library and configuration
14 . /etc/plc.d/functions
15 . /etc/planetlab/plc_config
16
17 # Be verbose
18 set -x
19
20 # Export so that we do not have to specify -p to psql invocations
21 export PGPORT=$PLC_DB_PORT
22
23 # Updates the database by applying all migration scripts in
24 # /usr/share/plc_api/migrations/N-up-*, where N is greater than the
25 # current subversion. At least one of the migration scripts with the
26 # same N must update plc_db_version.subversion.
27 function migrate_db()
28 {
29     subversion=$(psql -U $PLC_DB_USER --quiet --tuples-only --no-align -c \
30                  "SELECT subversion FROM plc_db_version LIMIT 1" \
31                  $PLC_DB_NAME 2>/dev/null || echo 0)
32     shopt -s nullglob
33     for file in /usr/share/plc_api/migrations/[0-9]*-up-* ; do
34         script=$(basename $file)
35         index=${script%-up*}
36         extension=${script##*.}
37         if [ $index -gt $subversion ] ; then
38             if [ "$extension" = "sql" ] ; then
39                 psql -U $PLC_DB_USER -f $file $PLC_DB_NAME
40             elif [ -x $file ] ; then
41                 $file
42             fi
43             check
44         fi
45     done
46 }
47
48 # Dumps the database
49 function dump_db()
50 {
51     dump=/var/lib/pgsql/backups/$(date +"$PLC_DB_NAME.%Y-%m-%d-%H-%M.sql")
52     pg_dump -U $PLC_DB_USER $PLC_DB_NAME > $dump
53     check
54 }
55
56 # Clean up old backups
57 function clean_dumps()
58 {
59     find /var/lib/pgsql/backups -name "$PLC_DB_NAME.*.sql" -atime +15 | xargs rm -f
60     check
61 }
62
63 if [ "$PLC_DB_ENABLED" != "1" ] ; then
64     exit 0
65 fi
66
67 case "$1" in
68     start)
69         MESSAGE=$"Bootstrapping the database"
70         dialog "$MESSAGE"
71
72         # Apply schema updates
73         migrate_db
74
75         # Update the maintenance account username. This can't be
76         # done through the api-config script since it uses the
77         # maintenance account to access the API. The maintenance
78         # account should be person_id 1 since it is created by the
79         # DB schema itself.
80         psql -U $PLC_DB_USER -c "UPDATE persons SET email='$PLC_API_MAINTENANCE_USER' WHERE person_id=1" $PLC_DB_NAME
81
82         # Update the Drupal site_name variable
83         psql -U $PLC_DB_USER drupal <<EOF
84 DELETE FROM variable WHERE name = 'site_name';
85 INSERT INTO variable (name, value) VALUES ('site_name', 's:${#PLC_NAME}:"$PLC_NAME";');
86 EOF
87
88         # Bootstrap the DB
89         db-config
90         check
91
92         result "$MESSAGE"
93         ;;
94
95     migrate)
96         MESSAGE=$"Migrating the database"
97         dialog "$MESSAGE"
98
99         migrate_db
100         result "$MESSAGE"
101         ;;
102
103     dump)
104         MESSAGE=$"Dumping the database"
105         dialog "$MESSAGE"
106
107         dump_db
108         result "$MESSAGE"
109         ;;
110
111     clean-dump)
112         MESSAGE=$"Cleaning old database dumps"
113         dialog "$MESSAGE"
114
115         clean_dumps
116         result "$MESSAGE"
117         ;;
118
119     *)
120         echo "Usage: $0 [start|migrate|dump|clean-dump]"
121         exit 1
122         ;;
123 esac
124
125 exit $ERRORS