final touches on dkim test harness
[citadel.git] / citadel / utils / 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 # Find our Berkeley DB utilities.
42 unset TOOLSBIN
43 for d in /usr/local/ctdlsupport/bin /usr/local/bin /usr/bin
44 do
45         if [ -x ${d}/db_dump ] ; then
46                 echo -n "Found Berkeley DB tools in ${d}: "
47                 TOOLSBIN=${d}
48                 break;
49         fi
50 done
51
52 RECOVER=${TOOLSBIN}/db_recover
53 DUMP=${TOOLSBIN}/db_dump
54 LOAD=${TOOLSBIN}/db_load
55 ls ${RECOVER} ${DUMP} ${LOAD} || {
56         echo "database_cleanup.sh cannot find the Berkeley DB utilities.  Exiting." 1>&2
57         exit 1
58 }
59
60 # Ok, let's begin.
61
62 cat <<!
63
64 Citadel Database Cleanup
65 ---------------------------
66
67 This script exports, deletes, and re-imports your database.  If you have
68 any data corruption issues, this program may be able to clean them up for you.
69  
70 Please note that this program does a Berkeley DB dump/load, not a Citadel
71 export.  The export files are not generated by the Citadel export module.
72
73 WARNING #1:
74   MAKE A BACKUP OF YOUR DATA BEFORE ATTEMPTING THIS.  There is no guarantee
75   that this will work (in case of disk full, power failure, program crash)!
76
77 WARNING #2:
78   citserver must NOT be running while you do this.
79
80 WARNING #3:
81   Please try "cd $DATA_DIR; $RECOVER -c" first. Run citserver afterwards to 
82   revalidate whether its fixed or not, No news might be good news. Use this
83   tool only if that one fails to fix your problem.
84
85 WARNING #4:
86   You must have an amount of free space in /tmp that is at least twice
87   the size of your database. see the following output:
88
89 `df -h`
90
91 you will need `du -sh $DATA_DIR|sed "s;/.*;;"` of free space.
92
93 !
94
95 echo We will attempt to look for a Citadel database in $DATA_DIR
96 echo -n "Do you want to continue? "
97
98 read yesno
99 case "$yesno" in
100         "y" | "Y" | "yes" | "YES" | "Yes" )
101                 echo 
102                 echo DO NOT INTERRUPT THIS PROCESS.
103                 echo
104         ;;
105         * )
106                 exit
107 esac
108
109 for x in 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d
110 do
111         filename=cdb.$x
112         echo Dumping $filename
113         $DUMP -h $DATA_DIR $filename >/tmp/CitaDump.$x || {
114                 echo error $?
115                 die
116         }
117         rm -vf $DATA_DIR/$filename
118 done
119
120 echo Removing old databases
121 rm -vf $DATA_DIR/*
122
123 for x in 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d
124 do
125         filename=cdb.$x
126         echo Loading $filename
127         $LOAD -h $DATA_DIR $filename </tmp/CitaDump.$x && {
128                 rm -f /tmp/CitaDump.$x
129         }
130 done
131
132 echo 
133 echo Dump/load operation complete.  Start your Citadel server now.
134 echo