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