Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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 unix.stackexchange.com ? –  Hussain Tamboli Oct 9 '12 at 6:51
add comment

2 Answers

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 unix.stackexchange.com 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 - meta.stackoverflow.com/questions/149833/… –  Hussain Tamboli Oct 9 '12 at 8:25
add comment

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
add comment

Your Answer

 
discard

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.