Ask Ubuntu is a question and answer site for Ubuntu users and developers. 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 want to take backup of my mysql data using mysqldump. I have three files containing names of mysql tables line by line. My script has this line -

mysqldump -h localhost -uroot --lock-tables=false db < $2 > ~/backup/$name'.sql'

I am using getopts to handle some options. So basically $2 is the file I am going to provide. This file has - table1 and table2. So this command should only take backup of these two tables.

But when I cat the ~/backup/name.sql file it shows all the backup of db. I am not sure if it is the combined backup of all the tables from all the 3 files or the whole db. But it is taking backup of whole db instead of two tables.

What am I doing wrong?

It works fine when I put it like this -

mysqldump -h localhost -uroot --lock-tables=false db table1 table2 >  ~/backup/$name'.sql'
share|improve this question
Should I have asked this question in ? – Hussain Tamboli Oct 9 '12 at 6:51
up vote 1 down vote accepted

The reason why it fails is that mysqldump does not read the standard input; table1 and table2 are (optional) arguments, but you are providing them not on the command line, but on stdin.

If $2 is the filename of a file that contains one line with the text table1 table2, then the following should work:

mysqldump -h localhost -uroot --lock-tables=false db `cat $2` > ~/backup/$name'.sql'

Bash will expand this command line to

mysqldump -h localhost -uroot --lock-tables=false db table1 table2 > /home/user/backup/somename.sql

which is what you want to run.

However, since you are using getopt anyways, maybe you could put "table1" in $2 and "table2" in $3, and run just

mysqldump -h localhost -uroot --lock-tables=false db $2 $3 > ~/backup/$name'.sql'
share|improve this answer
Thanks. great help. Don't mind me asking but should I have put this in instead of askubuntu. I mean these two are very alike. – Hussain Tamboli Oct 9 '12 at 7:37
I think yes, this is not Ubuntu specific. – January Oct 9 '12 at 7:58
yes. thanks. I have just asked it in Meta -… – Hussain Tamboli Oct 9 '12 at 8:25

It depends a bit on how your running the script but you should be able to use back-ticks and cat the file.

mysqldump -h localhost -uroot --lock-tables=false db `cat $2` > ~/backup/$name'.sql
share|improve this answer

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.