From 4312ceb9635e764efd81c95f0bfdb846774bcec2 Mon Sep 17 00:00:00 2001 From: Cedric Girard Date: Thu, 23 Feb 2012 15:00:18 +0100 Subject: [PATCH] Added script to dump the content of a database into separate csv files --- MySQL/dump_csv.sh | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100755 MySQL/dump_csv.sh 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