Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using the following script to dump my database. it works great. I want to add functionality that it first checks if a database table is already dumped or not, if a dump exists then it should be skipped and move to the next table.


[ -n "$DIR" ] || DIR=.
test -d $DIR || mkdir -p $DIR

echo "Dumping tables into separate SQL command files for database '$DB' into dir=$DIR"


for t in $(mysql -NBA -h $DB_host -u $DB_user -p$DB_pass -D $DB -e 'show tables')
    echo "DUMPING TABLE: $t"
    mysqldump -h $DB_host -u $DB_user -p$DB_pass $DB $t  > $DIR/$t.sql
    (( tbl_count++ ))

echo "$tbl_count tables dumped from database '$DB' into dir=$DIR"
share|improve this question
You seem to know how to check if a directory exists already. Do you need anything else than to check if a file exists? – Mat Aug 17 '13 at 7:01
up vote 0 down vote accepted

Change this block of the code:

echo "DUMPING TABLE: $t"
mysqldump -h $DB_host -u $DB_user -p$DB_pass $DB $t  > $DIR/$t.sql
(( tbl_count++ ))

To this:

echo "DUMPING TABLE: $t"
if [ -e "$DIR/$t.sql" ]; then
  echo "TABLE $DIR/$t.sql exists, skipping..."
  mysqldump -h $DB_host -u $DB_user -p$DB_pass $DB $t  > $DIR/$t.sql
(( tbl_count++ ))
share|improve this answer
Thanks a lot .... – K.K Patel Aug 18 '13 at 8:04

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.