Created
May 4, 2012 07:29
-
-
Save turicas/2592877 to your computer and use it in GitHub Desktop.
Convert a MDB (Access) file to SQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Convert a MDB file (Access) to SQL | |
# Needs mdbtools[http://mdbtools.sourceforge.net/] | |
# run 'aptitude install mdbtools' on Debian/Ubuntu | |
# Created by Álvaro Justen <https://github.com/turicas> | |
# License: GPLv2 | |
mdb=$1 | |
sql=$2 | |
if [ -z "$2" ]; then | |
echo 'This script convert a MDB file to SQL file. You need to specify the name of both' | |
echo "Usage: $0 <mdb_file> <sql_file>" | |
exit 1 | |
fi | |
if [ -z "$(which mdb-tables)" ]; then | |
echo 'You need mdbtools installed.' | |
echo 'Learn more at http://mdbtools.sourceforge.net/' | |
echo 'If you use Debian/Ubuntu, just execute:' | |
echo ' sudo aptitude install mdbtools' | |
exit 2 | |
fi | |
mdb-schema $mdb > $sql | |
sed -i 's/Long Integer/INT(11)/g; s/Text /VARCHAR/g' $sql | |
for table in $(mdb-tables $mdb); do | |
mdb-export -I -R ';' $mdb $table >> $sql | |
done | |
sed -i '/^-\{2,\}/d; s/DROP TABLE /DROP TABLE IF EXISTS /' $sql |
The same thing with all error checking removed:
#!/bin/bash
mdb=$1
output=${2:--}
dialect=${3:-mysql}
[[ $output != - ]] && exec >"$output"
mdb-schema "$mdb" "$dialect"
mdb-tables -1 "$mdb" |
while read -r table
do
mdb-export -I "$dialect" -q\' -boctal -D%F "$mdb" "$table"
done
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@anentropic Yes it's fairly common for shell scripts to be missing quotes, and to make other dubious assumptions (
for var in $( ... )
is usually a bad idea, as is checking the output ofwhich
).It is also missing the database type after the
-I
option tomdb-export
, which will cause (some versions of?)mdb-export
to fail.The default string quote used by (some versions of?)
mdb-export
is"
, which is not standard SQL; we need to add-q\'
to get the correct behaviour.My suggestion below incorporates these:
mysql
)/dev/stdout
to the end of the script invocationsed
commandsmdb-schema
to output the correct format to begin withmdb-export
which database typecmd | while read x ; do ...
instead offor x in $( cmd ) ; do ...
mdb-schema
fails, skip the remaining stepscommand
instead ofwhich
, and check all required commands/usr/include/sysexits.h
(but if you don't like them you can just useexit 1
)Normally I would point out the necessity to adjust the
#!
line to match wherever you have bash installed on your system, but that would be pointless in this case since it can only run on Linux (where themdb-tools
package is available, and where/bin/bash
is more reliable than/usr/bin/env bash
).