1

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.

DB_host=127.0.0.1
DB_user=root
DB=mydb
DB_pass=mydbpassword
DIR=/

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

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

tbl_count=0

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

echo "$tbl_count tables dumped from database '$DB' into dir=$DIR"
1
  • You seem to know how to check if a directory exists already. Do you need anything else than to check if a file exists? Commented Aug 17, 2013 at 7:01

1 Answer 1

0

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..."
else
  mysqldump -h $DB_host -u $DB_user -p$DB_pass $DB $t  > $DIR/$t.sql
fi
(( tbl_count++ ))
0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.