/* We used to wait for all threads to exit. Fuck that. The only thing important...
[citadel.git] / citadel / database_cleanup.sh.in
1 #!/bin/bash
2
3 die () {
4         echo Exiting.
5         exit 1
6 }
7
8 DATA_DIR="/usr/local/citadel"
9
10
11
12 usage() {
13         echo "Usage: database_cleanup.sh [ -h citadel_dir ]"
14         exit 2
15 }
16
17 PARSED_ARGUMENTS=$(getopt -a -n database_cleanup.sh -o h: -- "$@")
18 VALID_ARGUMENTS=$?
19 if [ "$VALID_ARGUMENTS" != "0" ]; then
20         usage
21 fi
22
23 eval set -- "$PARSED_ARGUMENTS"
24 while :
25 do
26         case "$1" in
27                 -h | --alpha)
28                         DATA_DIR=${2}
29                         shift 2
30                         ;;
31                 # -- means the end of the arguments; drop this, and break out of the while loop
32                 --) shift; break ;;
33                 # If invalid options were passed, then getopt should have reported an error,
34                 # which we checked as VALID_ARGUMENTS when getopt was called...
35                 *) echo "Unexpected option: $1 - this should not happen."
36                         usage
37                         ;;
38         esac
39 done
40
41 DATA_DIR=$DATA_DIR/data
42
43 # If we're on an Easy Install system, use our own db_ tools.
44 #
45 if [ -x /usr/local/ctdlsupport/bin/db_dump ] ; then
46         export PATH=/usr/local/ctdlsupport/bin:$PATH
47         RECOVER=/usr/local/ctdlsupport/bin/db_recover
48         DUMP=/usr/local/ctdlsupport/bin/db_dump
49         LOAD=/usr/local/ctdlsupport/bin/db_load
50
51 else
52         # ok usual install?
53         if test -f /usr/bin/db_dump; then 
54                 RECOVER=/usr/bin/db_recover
55                 DUMP=/usr/bin/db_dump
56                 LOAD=/usr/bin/db_load
57         else
58                 if test -n "`ls /usr/bin/db?*recover`"; then
59                         # seems we have something debian alike thats adding version in the filename
60                         if test "`ls /usr/bin/db*recover |wc -l`" -gt "1"; then 
61                                 echo "Warning: you have more than one version of the Berkeley DB utilities installed." 1>&2
62                                 echo "Using the latest one." 1>&2
63                                 RECOVER=`ls /usr/bin/db*recover |sort |tail -n 1`
64                                 DUMP=`ls /usr/bin/db*dump |sort |tail -n 1`
65                                 LOAD=`ls /usr/bin/db*load |sort |tail -n 1`
66                         else
67                                 RECOVER=`ls /usr/bin/db*recover`
68                                 DUMP=`ls /usr/bin/db*dump`
69                                 LOAD=`ls /usr/bin/db*load`
70                         fi
71                 else
72                         echo "database_cleanup.sh cannot find the Berkeley DB utilities.  Exiting." 1>&2
73                         die
74                 fi
75
76         fi
77 fi
78
79 # Ok, let's begin.
80 #
81
82 clear
83 cat <<!
84
85 Citadel Database Cleanup
86 ---------------------------
87
88 This script exports, deletes, and re-imports your database.  If you have
89 any data corruption issues, this program may be able to clean them up for you.
90  
91 Please note that this program does a Berkeley DB dump/load, not a Citadel
92 export.  The export files are not generated by the Citadel export module.
93
94 WARNING #1:
95   MAKE A BACKUP OF YOUR DATA BEFORE ATTEMPTING THIS.  There is no guarantee
96   that this will work (in case of disk full, power failure, program crash)!
97
98 WARNING #2:
99   citserver must NOT be running while you do this.
100
101 WARNING #3:
102   Please try "cd $DATA_DIR; $RECOVER -c" first. Run citserver afterwards to 
103   revalidate whether its fixed or not, No news might be good news. Use this
104   tool only if that one fails to fix your problem.
105
106 WARNING #4:
107   You must have an amount of free space on your disk that is at least twice
108   the size of your database, see the following output:
109   (for substantially better performance you should specify a location that is 
110    on another disk than $DATA_DIR)
111
112 `df -h`
113
114 you will need `du -sh $DATA_DIR|sed "s;/.*;;"` of free space.
115
116 !
117
118 echo We will attempt to look for a Citadel database in $DATA_DIR
119 echo -n "Do you want to continue? "
120
121 read yesno
122 case "$yesno" in
123         "y" | "Y" | "yes" | "YES" | "Yes" )
124                 echo 
125                 echo DO NOT INTERRUPT THIS PROCESS.
126                 echo
127         ;;
128         * )
129                 exit
130 esac
131
132 for x in 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d
133 do
134         filename=cdb.$x
135         echo Dumping $filename
136         $DUMP -h $DATA_DIR $filename >/tmp/CitaDump.$x || {
137                 echo error $?
138                 die
139         }
140         rm -f $DATA_DIR/$filename
141 done
142
143 echo Removing old databases
144 rm -f ./data/*
145
146 for x in 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d
147 do
148         filename=cdb.$x
149         echo Loading $filename
150         $LOAD -h $DATA_DIR $filename </tmp/CitaDump.$x && {
151                 rm -f /tmp/CitaDump.$x
152         }
153 done
154
155 echo 
156 echo Dump/load operation complete.  Start your Citadel server now.
157 echo