Tester blog

Dumping DB tables with and without Data

Posted by: Mohamed on: June 15, 2008

This is shell script for backing up database.
using this script u can take backup of a database, Say you need to take backup of the mysql DB, but you need some table only structure , some with structure and data, using this script you can do that.

#!/bin/ksh

username=’root’;
password=’xxxxxxxx’;
hostname=’0.0.0.0′;
command=’mysqldump’;
database=’mysql’;
dump_file=’XXXX.sql’;
CURRENT=$(date -d”$CURRENT +$MINUTES minutes”);
GetTableData=( users columns_priv host );

echo show tables | mysql -u $username -p$password -h $hostname $database > /tmp/$CURRENT.txt

#  -e   Allows utilization of the new, much faster INSERT syntax.

if [ -e $dump_file ];
then

# rm – remove files or directories, -r, -R, –recursive remove the contents of directories recursively, -f, –force ignore nonexistent files, never prompt
rm -rf $dump_file
fi

while read TABLENAME
do
echo -n  “Backing up Tables From “  “  $database ” “   $TABLENAME”
for i in  $TABLENAME
do
for tableCheck   in “${GetTableData[@]}”
do
# x is  Deprecated, renamed to –lock-all-tables.
if [ "x$TABLENAME" == "x${tableCheck}" ]
then
backup=1;
break;
fi
done
done

if [ "$backup" = "0" ];
then
echo “                  [ Only Sturcture... ]“;
$command $database  -u $username -p$password -h $hostname $i -d >>$dump_file
else
echo “                  [ DATA and Sturcture .. ]“;
$command $database  -u $username -p$password  -h $hostname $i  >>$dump_file
fi

backup=0
done< /tmp/$CURRENT.txt
echo
echo “++++++++++++++++++++++++++++++++++++++++”
echo  $database “DB Backup Taken Successfully”
echo “++++++++++++++++++++++++++++++++++++++++”
echo
exit;

Leave a Reply

Linkedin

View Mohamed Faisal's profile on LinkedIn

Mohamed Faisal Resume

TutorVista – Free Tutoring

Get one-on-one tutoring with world class tutors in the field of Math, English and Science. Ask a Question? Get an Answer!