File cyrus-imapd.cvt_cyrusdb_all of Package cyrus-imapd (Revision 9721cfa582cd4292f1c94d52c66292bb)

Currently displaying revision 9721cfa582cd4292f1c94d52c66292bb , Show latest

253
 
1
#!/bin/bash
2
# Simplified version of Simon Matter's cvt_cyrusdb_all.
3
4
function usage() {
5
    echo "Usage: $0 [-C <altconfig>] [--verbose] [--devel] <export|import>"
6
    exit 1
7
}
8
9
[ -f "/etc/sysconfig/cyrus-imapd" ] && \
10
    . /etc/sysconfig/cyrus-imapd
11
12
[ -f "/etc/default/cyrus-imapd" ] && \
13
    . /etc/default/cyrus-imapd
14
15
# Ensure this script is run as the cyrus user.
16
if [ -x "$(which runuser 2>/dev/null)" ]; then
17
    SWITCHUSER=runuser
18
else
19
    SWITCHUSER=su
20
fi
21
22
#if [ ! $(whoami) = "cyrus" ]; then
23
#    exec $SWITCHUSER -s /bin/bash - cyrus -c "cd \$HOME < /dev/null ; INSTANCE=$0 $*"
24
#    exit 0
25
#fi
26
27
umask 166
28
29
alt_config="/etc/imapd.conf"
30
devel_mode=0
31
verbose_mode=0
32
mode=""
33
34
while [ $# -gt 0 ]; do
35
    case $1 in
36
        -C)
37
            alt_config=$2
38
            shift; shift
39
        ;;
40
41
        --devel)
42
            devel_mode=1
43
            shift
44
        ;;
45
46
        --verbose)
47
            verbose_mode=1
48
            shift
49
        ;;
50
51
        export|import)
52
            mode=$1
53
            shift
54
        ;;
55
56
        *)
57
            usage
58
        ;;
59
60
    esac
61
done
62
63
if [ -z "${mode}" ]; then
64
    usage
65
fi
66
67
if [ ${devel_mode} -eq 1 ]; then
68
    verbose_mode=1
69
fi
70
71
declare -a dbs
72
declare -a db_paths
73
74
dbs[${#dbs[@]}]="annotation_db";    db_paths[${#db_paths[@]}]="annotations.db"
75
dbs[${#dbs[@]}]="duplicate_db";     db_paths[${#db_paths[@]}]="deliver.db"
76
# dbs[${#dbs[@]}]="mboxkey_db";       db_paths[${#db_paths[@]}]=""
77
dbs[${#dbs[@]}]="mboxlist_db";      db_paths[${#db_paths[@]}]="mailboxes.db"
78
dbs[${#dbs[@]}]="ptscache_db";      db_paths[${#db_paths[@]}]="ptclient/ptscache.db"
79
dbs[${#dbs[@]}]="quota_db";         db_paths[${#db_paths[@]}]="quotas.db"
80
# dbs[${#dbs[@]}]="seenstate_db";     db_paths[${#db_paths[@]}]=""
81
dbs[${#dbs[@]}]="statuscache_db";   db_paths[${#db_paths[@]}]="statuscache.db"
82
# dbs[${#dbs[@]}]="subscription_db";  db_paths[${#db_paths[@]}]=""
83
dbs[${#dbs[@]}]="tls_sessions_db";  db_paths[${#db_paths[@]}]="tls_sessions.db"
84
dbs[${#dbs[@]}]="userdeny_db";      db_paths[${#db_paths[@]}]="user_deny.db"
85
86
# /usr/lib/cyrus-imapd/cyr_dbtool /var/lib/imap/annotations.db skiplist consistent; echo $?
87
# Yes, consistent
88
# 0
89
90
cvt_cyrusdb="/usr/lib/cyrus-imapd/cvt_cyrusdb"
91
cyr_dbtool="/usr/lib/cyrus-imapd/cyr_dbtool"
92
cyr_info="/usr/lib/cyrus-imapd/cyr_info"
93
94
system_magic=$(file --version 2>&1 | awk '/magic file/ {print $4}')
95
96
if [ ${devel_mode} -gt 0 ]; then
97
    if [ -f "$(pwd)/imap/cvt_cyrusdb" ]; then
98
        cvt_cyrusdb="$(pwd)/imap/cvt_cyrusdb"
99
    fi
100
101
    if [ -f "$(pwd)/imap/.libs/cyr_dbtool" ]; then
102
        cyr_dbtool="$(pwd)/imap/cyr_dbtool"
103
    fi
104
105
    if [ -f "$(pwd)/imap/.libs/cyr_info" ]; then
106
        cyr_info="$(pwd)/imap/cyr_info"
107
    fi
108
fi
109
110
function check_consistency() {
111
    db_path=$1
112
    db_tech=$2
113
114
    if [ ! -f "${db_path}" ]; then
115
        return 1
116
    fi
117
118
    retval=$(${cyr_dbtool} -C ${alt_config} ${db_path} ${db_tech} consistent >/dev/null 2>&1; echo $?)
119
120
    return ${retval}
121
}
122
123
function convert_database() {
124
    retval=$(${cvt_cyrusdb} -C ${alt_config} "$1" "$2" "$3" "$4" >/dev/null 2>&1; echo $?)
125
    chown cyrus:mail "$3"
126
}
127
128
function expand_db_path() {
129
    if [ "${1:0:1}" != "/" ]; then
130
        config_directory=$(get_config "configdirectory")
131
        value="${config_directory}/${1}"
132
    fi
133
134
    echo "${value}"
135
}
136
137
function get_config() {
138
    echo $(${cyr_info} -C ${alt_config} conf | grep -E "^$1:\s+" | sed -r -e 's/[a-z0-9_-]+\:\s*(.*)/\1/g')
139
}
140
141
function get_config_default() {
142
    echo $(${cyr_info} -C ${alt_config} conf-default | grep -E "^$1:\s+" | sed -r -e 's/[a-z0-9_-]+\:\s*(.*)/\1/g')
143
}
144
145
function get_current() {
146
    default_path="$2"
147
    value=$(${cyr_info} -C ${alt_config} conf-all | grep -E "^$1_path:\s+" | sed -r -e 's/[a-z0-9_-]+\:\s*(.*)/\1/g')
148
149
    if [ -z "${value}" ]; then
150
        if [ ! -z "${default_path}" ]; then
151
            value=$(expand_db_path "${default_path}")
152
        fi
153
    fi
154
155
    if [ -z "${value}" ]; then
156
        return 1
157
    fi
158
159
    if [ ! -f "${value}" ]; then
160
        return 1
161
    fi
162
163
    current=$(file -b -m /var/lib/imap/rpm/magic:${system_magic} "${value}")
164
165
    if echo "${current}" | grep -qi skiplist > /dev/null 2>&1; then
166
        current="skiplist"
167
    elif echo "${current}" | grep -qi twoskip > /dev/null 2>&1; then
168
        current="twoskip"
169
    elif echo "${current}" | grep -qi text > /dev/null 2>&1; then
170
        current="flat"
171
    else
172
        current="berkeley"
173
    fi
174
175
    echo "${current}"
176
}
177
178
config_directory=$(get_config "configdirectory")
179
180
if [ ${devel_mode} -eq 1 ]; then
181
    echo "Using configuration directory: '${config_directory}'"
182
fi
183
184
x=0
185
while [ ${x} -lt ${#dbs[@]} ]; do
186
    config=$(get_config "${dbs[${x}]}")
187
    default=$(get_config_default "${dbs[${x}]}")
188
    current=$(get_current "${dbs[${x}]}" "${db_paths[${x}]}")
189
190
    if [ ${verbose_mode} -eq 1 ]; then
191
        echo -n "${dbs[${x}]}: "
192
    fi
193
194
    if [ $? -ne 0 ]; then
195
        if [ ${verbose_mode} -eq 1 ]; then
196
            echo "SKIPPED"
197
        fi
198
        let x++
199
        continue
200
    fi
201
202
    # If configuration doesn't hold anything, we have defaults
203
    if [ -z "${config}" ]; then
204
        config="${default}"
205
    fi
206
207
    if [ "${config}" == "${default}" -a "${config}" == "${current}" ]; then
208
        if [ ${verbose_mode} -eq 1 ]; then
209
            echo "SKIPPED (unchanged)"
210
        fi
211
        let x++
212
        continue
213
    fi
214
215
    db_path=$(expand_db_path "${db_paths[${x}]}")
216
217
    if [ ! -f "${db_path}" ]; then
218
        if [ ${verbose_mode} -eq 1 ]; then
219
            echo "SKIPPED (No such file or directory '${db_path}')"
220
        fi
221
        let x++
222
        continue
223
    fi
224
225
    if [ "${current}" != "${default}" ]; then
226
        if [ ${devel_mode} -eq 1 ]; then
227
            echo -n "to default(1), "
228
        fi
229
        convert_database "${db_path}" "${current}" "${db_path}.${default}" "${default}"
230
    fi
231
232
    if [ "${current}" != "${config}" ]; then
233
        mv "${db_path}" "${db_path}.${current}"
234
        if [ "${current}" != "${default}" ]; then
235
            if [ ${devel_mode} -eq 1 ]; then
236
                echo -n "to default(2), "
237
            fi
238
            convert_database "${db_path}.${current}" "${current}" "${db_path}.${default}" "${default}"
239
        fi
240
241
        if [ ${devel_mode} -eq 1 ]; then
242
            echo -n "to config(1), "
243
        fi
244
        convert_database "${db_path}.${current}" "${current}" "${db_path}" "${config}"
245
    fi
246
247
    if [ ${verbose_mode} -eq 1 ]; then
248
        echo "OK"
249
    fi
250
251
    let x++
252
done
253