build database cleanup from configure to have it behaving a bit closer to the install...
[citadel.git] / citadel / database_cleanup.sh.in
1 #!/bin/bash
2
3 die () {
4         echo Exiting.
5         exit 1
6 }
7
8 # data dir from autoconf?
9 DATA_DIR="@MAKE_DATA_DIR@"
10 if test -z "$DATA_DIR" ; then
11         DATA_DIR="/usr/local/citadel"
12 fi
13 DATA_DIR=$DATA_DIR/data
14
15 # If we're on an Easy Install system, use our own db_ tools.
16 #
17 if [ -x /usr/local/ctdlsupport/bin/db_dump ] ; then
18         export PATH=/usr/local/ctdlsupport/bin:$PATH
19         RECOVER=/usr/local/ctdlsupport/bin/db_recover
20         DUMP=/usr/local/ctdlsupport/bin/db_dump
21         LOAD=/usr/local/ctdlsupport/bin/db_load
22
23 else
24         # ok usual install?
25         if test -f /usr/bin/db_dump; then 
26                 RECOVER=/usr/bin/db_recover
27                 DUMP=/usr/bin/db_dump
28                 LOAD=/usr/bin/db_load
29         else
30                 if test -n "`ls /usr/bin/db?*recover`"; then
31                         # seems we have something debian alike thats adding version in the filename
32                         if test "`ls /usr/bin/db*recover |wc -l`" -gt "1"; then 
33                                 echo "Warning, you have more than one berkley db utility version installed. using the latest one." 1>&2
34                                 RECOVER=`ls /usr/bin/db*recover |sort |tail -n 1`
35                                 DUMP=`ls /usr/bin/db*dump |sort |tail -n 1`
36                                 LOAD=`ls /usr/bin/db*load |sort |tail -n 1`
37                         fi
38                 else
39                         echo "We can't find the berkeley DB tools, exiting." 1>&2
40                         die
41                 fi
42
43         fi
44 fi
45
46 # Ok, let's begin.
47 #
48
49 clear
50 cat <<!
51
52 Citadel Database Cleanup
53 ---------------------------
54
55 This script exports, deletes, and re-imports your database.  If you have
56 any data corruption issues, this program may be able to clean them up for you.
57  
58 Please note that this program does a Berkeley DB dump/load, not a Citadel
59 export.  The export files are not generated by the Citadel export module.
60
61 WARNING #1:
62   MAKE A BACKUP OF YOUR DATA BEFORE ATTEMPTING THIS.  There is no guarantee
63   that this will work (in case of disk full, power failure, program crash)!
64
65 WARNING #2:
66   citserver must NOT be running while you do this.
67
68 WARNING #3:
69   Please try "cd $DATA_DIR; $RECOVER -c" first.  Use this tool
70   only if that one fails to fix your problem.
71
72 WARNING #4:
73   You must have an amount of free space on your disk that is at least twice
74   the size of your database, see the following output:
75   (for substantial better performance you should specify a location that's 
76    on another disk than $DATA_DIR)
77
78 `df -h`
79
80 you will need `du -sh $DATA_DIR|sed "s;/.*;;"` of free space.
81
82 !
83
84 echo -n "Do you want to continue? "
85
86 read yesno
87 case "$yesno" in
88         "y" | "Y" | "yes" | "YES" | "Yes" )
89                 echo 
90                 echo DO NOT INTERRUPT THIS PROCESS.
91                 echo
92         ;;
93         * )
94                 exit
95 esac
96
97 for x in 00 01 02 03 04 05 06 07 08 09 0a
98 do
99         filename=cdb.$x
100         echo Dumping $filename
101         $DUMP -h $DATA_DIR $filename >/tmp/CitaDump.$x || die
102         rm -f $DATA_DIR/$filename
103 done
104
105 echo Removing old databases
106 rm -f ./data/*
107
108 for x in 00 01 02 03 04 05 06 07 08 09 0a
109 do
110         filename=cdb.$x
111         echo Loading $filename
112         $LOAD -h $DATA_DIR $filename </tmp/CitaDump.$x && {
113                 rm -f /tmp/CitaDump.$x
114         }
115 done
116
117 echo 
118 echo Dump/load operation complete.  Start your Citadel server now.
119 echo