66 lines
2.4 KiB
Bash
66 lines
2.4 KiB
Bash
#!/bin/bash
|
|
set -Eeuo pipefail
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
|
# shellcheck source=./roundcube_common.sh
|
|
source "$SCRIPT_DIR/roundcube_common.sh"
|
|
|
|
FORCE="${FORCE:-0}"
|
|
SKIP_NATIVE_DUMP="${SKIP_NATIVE_DUMP:-0}"
|
|
LOG_FILE="${LOG_FILE:-$ROUNDCUBE_PLUGIN_DIR/data/logs/roundcube-migrate.log}"
|
|
mkdir -p "$(dirname "$LOG_FILE")"
|
|
|
|
log() {
|
|
printf '[%s] %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$*" | tee -a "$LOG_FILE"
|
|
}
|
|
|
|
die() {
|
|
log "ERROR: $*"
|
|
exit 1
|
|
}
|
|
|
|
roundcube_enabled || die "Roundcube integration is disabled by ROUNDCUBE_ENABLED."
|
|
|
|
DB_NAME="$(roundcube_db_name)"
|
|
NATIVE_MY_CNF="$(roundcube_native_my_cnf)"
|
|
TMP_DUMP="$(mktemp)"
|
|
trap 'rm -f "$TMP_DUMP"' EXIT
|
|
|
|
roundcube_create_alt_database_and_user
|
|
|
|
if [ "$SKIP_NATIVE_DUMP" != "1" ]; then
|
|
[ -r "$NATIVE_MY_CNF" ] || die "Native DirectAdmin MySQL config not readable: $NATIVE_MY_CNF"
|
|
NATIVE_MYSQL_BIN="$(mysql_native_binary mysql)" || die "native mysql/mariadb client not found"
|
|
NATIVE_MYSQLDUMP_BIN="$(mysql_native_binary mysqldump)" || die "native mysqldump/mariadb-dump client not found"
|
|
|
|
if ! "$NATIVE_MYSQL_BIN" --defaults-extra-file="$NATIVE_MY_CNF" --batch --skip-column-names information_schema \
|
|
-e "SELECT 1 FROM SCHEMATA WHERE SCHEMA_NAME='$(mysql_escape_literal "$DB_NAME")' LIMIT 1" | grep -q 1; then
|
|
if [ "$FORCE" != "1" ]; then
|
|
die "Native Roundcube database $DB_NAME not found. Use SKIP_NATIVE_DUMP=1 if this is a new Roundcube install."
|
|
fi
|
|
log "Native Roundcube database $DB_NAME not found; continuing because FORCE=1."
|
|
else
|
|
log "Dumping native Roundcube database: $DB_NAME"
|
|
"$NATIVE_MYSQLDUMP_BIN" --defaults-extra-file="$NATIVE_MY_CNF" \
|
|
--single-transaction \
|
|
--quick \
|
|
--skip-lock-tables \
|
|
--routines \
|
|
--triggers \
|
|
--events \
|
|
--default-character-set=utf8mb4 \
|
|
"$DB_NAME" > "$TMP_DUMP" 2>>"$LOG_FILE" || die "native Roundcube dump failed"
|
|
|
|
log "Importing Roundcube database into alt-mariadb: $DB_NAME"
|
|
mysql_exec mysql -e "DROP DATABASE IF EXISTS $(mysql_quote_identifier "$DB_NAME");"
|
|
mysql_exec mysql -e "CREATE DATABASE $(mysql_quote_identifier "$DB_NAME") CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
|
|
mysql_exec "$DB_NAME" < "$TMP_DUMP" >> "$LOG_FILE" 2>&1 || die "Roundcube import into alt-mariadb failed"
|
|
roundcube_create_alt_database_and_user
|
|
fi
|
|
fi
|
|
|
|
"$SCRIPT_DIR/roundcube_repair_config.sh"
|
|
"$SCRIPT_DIR/roundcube_health_check.sh"
|
|
|
|
log "Roundcube migration to alt-mariadb completed."
|