Tuesday, April 30, 2024
 Popular · Latest · Hot · Upcoming
1
rated 0 times [  1] [ 0]  / answers: 1 / hits: 1276  / 2 Years ago, sun, may 1, 2022, 8:02:10

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'

More From » mysql

 Answers
6

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'

[#35063] Monday, May 2, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
asketbridiculou

Total Points: 168
Total Questions: 116
Total Answers: 115

Location: Antigua and Barbuda
Member since Sat, Jan 28, 2023
1 Year ago
asketbridiculou questions
Thu, Dec 22, 22, 08:36, 1 Year ago
Thu, Jan 27, 22, 14:28, 2 Years ago
Tue, Nov 29, 22, 21:50, 1 Year ago
Sun, Jun 20, 21, 04:42, 3 Years ago
Sun, Aug 15, 21, 08:42, 3 Years ago
;