dumps and cleans the drupal database as well
[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: db,v 1.7 2007/01/31 19:53:20 mlhuang Exp $
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     dump=/var/lib/pgsql/backups/$(date +"drupal.%Y-%m-%d-%H-%M.sql")
55     pg_dump -U $PLC_DB_USER drupal > $dump
56     check
57 }
58
59 # Clean up old backups
60 function clean_dumps()
61 {
62     find /var/lib/pgsql/backups '(' -name "$PLC_DB_NAME.*.sql" -o -name "drupal.*.sql" ')' -a -atime +15 | xargs rm -f
63     check
64 }
65
66 if [ "$PLC_DB_ENABLED" != "1" ] ; then
67     exit 0
68 fi
69
70 case "$1" in
71     start)
72         MESSAGE=$"Bootstrapping the database"
73         dialog "$MESSAGE"
74
75         # Apply schema updates
76         migrate_db
77
78         # Update the maintenance account username. This can't be
79         # done through the api-config script since it uses the
80         # maintenance account to access the API. The maintenance
81         # account should be person_id 1 since it is created by the
82         # DB schema itself.
83         psql -U $PLC_DB_USER -c "UPDATE persons SET email='$PLC_API_MAINTENANCE_USER' WHERE person_id=1" $PLC_DB_NAME
84
85         # Update the Drupal site_name variable
86         psql -U $PLC_DB_USER drupal <<EOF
87 DELETE FROM variable WHERE name = 'site_name';
88 INSERT INTO variable (name, value) VALUES ('site_name', 's:${#PLC_NAME}:"$PLC_NAME";');
89 EOF
90
91         # Bootstrap the DB
92         db-config
93         check
94
95         result "$MESSAGE"
96         ;;
97
98     migrate)
99         MESSAGE=$"Migrating the database"
100         dialog "$MESSAGE"
101
102         migrate_db
103         result "$MESSAGE"
104         ;;
105
106     dump)
107         MESSAGE=$"Dumping the databases in /var/lib/pgsql/backups"
108         dialog "$MESSAGE"
109
110         dump_db
111         result "$MESSAGE"
112         ;;
113
114     clean-dump)
115         MESSAGE=$"Cleaning old database dumps"
116         dialog "$MESSAGE"
117
118         clean_dumps
119         result "$MESSAGE"
120         ;;
121
122     *)
123         echo "Usage: $0 [start|migrate|dump|clean-dump]"
124         exit 1
125         ;;
126 esac
127
128 exit $ERRORS