Aujourd'hui j'ai pris le temps de faire un script bash qui permet d'exporter une base de données innodb.
Ce script utilise mysqldump pour exporter la base de donnée et désactive les clés étrangères pour faciliter l'import.
Script d'export innodb
#!/bin/bash
#
# dumpinno
# Bash script allowing to export innodb database
# Disable foreign keys during import
#
# @author Patrick Poulain
# @licence GPL
# @see http://petitchevalroux.net
# Sample Usage :
# dumpinno.sh DATABASE ~/DATABASE.sql
# dumpinno.sh -u root DATABASE ~/DATABASE.sql
# dumpinno.sh -u root -p dummypassword DATABASE ~/DATABASE.sql
#
# function triggered when a wrong call is made
function usage() {
echo "Usage: "`basename $0`" [-u dbuser][-p dbpassword] DATABASE FILE";
exit 2;
}
# check if mysqldump exists
cmd=`which mysqldump`;
if [ -z "$cmd" ] || [ ! -x "$cmd" ]
then
echo "Error : unable to find mysqldump command";
exit 1;
fi
# get user and password options
while getopts u:p: name
do
case $name in
u)cmd="$cmd -u $OPTARG";;
p)cmd="$cmd --password=$OPTARG";;
?) usage;;
esac
done
# get database and output file from arguments array
args=("$@");
file=${args[$OPTIND]};
database=${args[$OPTIND-1]};
# check if one of them is empty
if [ -z $file ] || [ -z $database ]
then
usage;
fi
# disable innodb foreign keys check and autocommit
echo "SET AUTOCOMMIT = 0;SET FOREIGN_KEY_CHECKS=0;" > $file;
# dump database to a tmp file
$cmd -r $file.tmp $database;
# copy dump content to the final file
cat $file.tmp >> $file;
# delete temporary file
rm $file.tmp;
# reset foreign key check and commit
echo "SET FOREIGN_KEY_CHECKS = 1;COMMIT;SET AUTOCOMMIT = 1;" >> $file;
Installation
Créer un fichier et ajouter le script précédent à l'intérieur
pcr@home:~$ vi ~/bin/dumpinno.sh
Rendre le script exécutable
pcr@home:~$ chmod u+x ~/bin/dumpinno.sh
Utilisation
Export de la base de données :
pcr@home:~$ ~/bin/dumpinno.sh -u mysql -p motdepasse MABASE export.sql
Import de la base de données :
pcr@home:~$ mysql -u mysql -p motdepasse MABASE < export.sql
Articles en relations :
Image : adesigna