3

tmp-file contains:

database_1
database_2
database_3

I want to run a command like "mysqldump DATABASE > database.sql && gzip database.sql" for each line in the above file.

I've got as far as cat /tmp/database-list | xargs -L 1 mysqldump -u root -p

I guess I want to know how to put the data passed to xargs in more than once (and not just on the end)

EDIT: the following command will dump each database into its own .sql file, then gzip them.

mysql -u root -pPASSWORD -B -e 'show databases' | sed -e '$!N; s/Database\n//' | xargs -L1 -I db mysqldump -u root -pPASSWORD -r db.backup.sql db; gzip *.sql
Thomas R
  • 3,026
  • 5
  • 32
  • 31

2 Answers2

3

In your own example you use && to use two commands on one line - so why not do

cat file | xargs -L1 -I db mysqldump db > db.sql && cat file | xargs -L1 -I db gzip database.sql

if you really want to do it all in one line using xargs only. Though I believe that

cat file | xargs -L1 -I db mysqldump db > db.sql && cat file; gzip *.sql

would make more sense.

  • Excellent, the -I switch was what I wanted to know. Thanks! – Thomas R Sep 03 '09 at 22:27
  • interesting, I've been trying to figure out why I couldn't get my replacement to be done twice in one command line. I've been using `-I {}` but when I switched it to an alphabetic `-I log` (since I'm dealing with log files) it started working. I hope this comment will help someone who is having the same trouble I was. – dldnh Mar 19 '12 at 15:57
1

If you have a multicore CPU (most of us have these days) then GNU Parallel http://www.gnu.org/software/parallel/ may improve the time to run:

mysql -u root -pPASSWORD -B -e 'show databases' \
| sed -e '$!N; s/Database\n//' \
| parallel -j+0 "mysqldump -u root -pPASSWORD {} | gzip > {}.backup.sql"

-j+0 will run as many jobs in parallel as you have CPU cores.

Ole Tange
  • 31,768
  • 5
  • 86
  • 104