File fb25-migratedb of Package compat-firebird25
#!/bin/bash
function usage ()
{
cat >&2 <<EOT
usage:
${sn} [options] <source_db> [target_db]
source_db: source (2.5) database (path to db file)
target_db: target (3.0) database (any valid connection string)
-b <bkp_file> intermediate backup file
-f fast mode (uses a pipe, no intermediate backup file)
-i interactive, lets user confirm each step
-h show this help
-k keep the intermediate backup file
-p <password> Firebird 3.0 password for database restore
-r create target database in read_only mode
-v verbose output (if used twice, pass -V also to gbak)
-u <user> Firebird 3.0 username for database restore
-w do not switch source db to read-only mode
EOT
exit $1
}
function arg_err ()
{
echo "${sn}: $1" >&2
usage 1
}
function step ()
{
: $[step++]
step_name="$1"
if $pause || [ $verb -ge 1 ]; then
echo -e "\n*** step ${step}: ${step_name} ***"
fi
if $pause; then
echo "press Enter to continue"
read
fi
}
function step_failed ()
{
echo "${sn}: database migration failed" >&2
echo "failed step ${step}: ${step_name}"
exit 3
}
sn="${0##*/}"
fast=false
keep=false
pause=false
srcro=true
tgtro=false
verb=0
step=0
bkp_file=''
step_name="initialization"
while getopts ":b:fihkp:ru:vw" opt; do
case "$opt" in
b)
bkp_file="$OPTARG"
;;
f)
fast=true
;;
i)
pause=true
;;
h)
usage 0
;;
k) keep=true
;;
p)
ISC_PASSWORD="$OPTARG"
;;
r)
tgtro=true
;;
u)
ISC_USER="$OPTARG"
;;
v)
: $[verb++]
;;
w)
srcro=false
;;
\?)
arg_err "unknown option -${OPTARG}"
;;
:)
arg_err "missing argument to -${OPTARG} option"
;;
esac
done
shift $[OPTIND - 1]
if [ $# -lt 1 ]; then
arg_err "source database argument missing"
fi
srcdb="$1"
if [ "${srcdb:0:1}" != "/" ]; then
# prevent using default directory
srcdb="./${srcdb}"
fi
if [ $# -gt 2 ]; then
arg_err "too many arguments"
fi
if [ $# -ge 2 ]; then
tgtdb="$2"
else
tgtdb="${srcdb}.fb30"
if [ -e "$tgtdb" ]; then
echo "${sn}: target database file '$tgtdb' already exists" >&2
exit 2
fi
fi
if $fast; then
if $keep; then
echo "${sn}: option -k ignored in fast mode" >&2
fi
if [ -n "$bkp_file" ]; then
echo "${sn}: option -b ignored in fast mode" >&2
bkp_file=''
fi
else
if [ -z "$bkp_file" ]; then
bkp_file="${srcdb%.fdb}.fbk"
fi
if [ -f "$bkp_file" ]; then
echo "${sn}: intermediate backup file '$bkp_file' already exists" >&2
exit 2
fi
fi
ISC_USER="${ISC_USER-sysdba}"
export ISC_USER
ISC_PASSWORD="${ISC_PASSWORD-masterkey}"
export ISC_PASSWORD
auth25='-user sysdba -password masterkey'
gbak25_opts="$auth25 -B"
gbak30_opts='-C'
if [ $verb -ge 2 ]; then
gbak25_opts="$gbak25_opts -V"
gbak30_opts="$gbak30_opts -V"
fi
if $tgtro; then
gbak30_opts="$gbak30_opts -mode read_only"
else
gbak30_opts="$gbak30_opts -mode read_write"
fi
if [ $verb -ge 1 ]; then
cat <<EOT
parameters:
source db: $srcdb
target db: $tgtdb
backup file: $bkp_file
username: $ISC_USER
password: $ISC_PASSWORD
fast mode: $($fast && echo yes || echo no)
interactive: $($pause && echo yes || echo no)
keep backup: $($keep && echo yes || echo no)
source r/w: $($srcro && echo no || echo yes)
target r/o: $($tgtro && echo yes || echo no)
verbosity: $verb
EOT
fi
if $srcro; then
step "switch source database to read only mode"
gfix25 $auth25 -mode read_only "$srcdb" \
|| step_failed
fi
if $fast; then
step "backup and restore the database (using a pipe)"
gbak25 $gbak25_opts "$srcdb" stdout \
| gbak $gbak30_opts stdin "$tgtdb" \
|| step_failed
else
step "backup the source (2.5) database"
gbak25 $gbak25_opts "$srcdb" "$bkp_file" || step_failed
step "restore into the target (3.0) database"
gbak $gbak30_opts "$bkp_file" "$tgtdb" || step_failed
if $keep; then
if [ $verb -ge 1 ]; then
echo "intermediate backup file '$bkp_file' left behind"
fi
else
step "remove intermediate backup file"
rm "$bkp_file"
fi
fi