commit 4312ceb9635e764efd81c95f0bfdb846774bcec2 Author: Cedric Girard Date: Thu Feb 23 15:00:18 2012 +0100 Added script to dump the content of a database into separate csv files diff --git a/MySQL/dump_csv.sh b/MySQL/dump_csv.sh new file mode 100755 index 0000000..9c0e8af --- /dev/null +++ b/MySQL/dump_csv.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +host='127.0.0.1' +database='cacti' +user='root' +outpath='/tmp/cacti' + +read -s -p"$user DB password: " password +echo +tables=`mysql -u$user -p$password $database -h$host -N -B -e "show tables"` +if [ -z $tables ] ; then + echo "Cannot fetch DB tables" + exit 1 +fi + +#create output dir and change permission if needed +[ -d $outpath ] || mkdir $outpath +[ $(stat --printf='%a' $outpath) -ge 777 ] || chmod a+w $outpath + +IFS=' +' +success=true +for table in $tables ; do + mysql -u$user -p$password -h$host $database -N -B -e "select * from $table into outfile '$outpath/$table.csv' fields terminated by ';' enclosed by '\"'" || success=false +done + +if $success ; then + echo "All tables of $database database have been successfuly dumped to csv files in $outpath" +else + echo "Errors encoutered while dumping $database database tables to $outpath" +fi