Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
512 views
in Technique[技术] by (71.8m points)

bash - mysqldump with db in a separate file

I'm writing a single line command that backups all databases into their respective names instead using of dumping all in one sql.

Eg: db1 get saved to db1.sql and db2 gets saved to db2.sql

So far, I'd gathered the following commands to retrieve all databases.

mysql -uuname -ppwd -e 'show databases' | grep -v 'Database'

I'm planning to pipe it with awk to do something like

awk '{mysqldump -uuname -ppwd $1 > $1.sql}'

But that doesn't work.

I'm new to bash, so I could be wrong in my thinking.
What should I do to make it export the db in their respective names?

update:
Ok, have to finally managed to get it working from the hints below.
This is the final script

# replace [] with your own config
# replace own dir to save
# echo doesn't work. hmm...

mysql -u[uname] -p'[pwd]' -e "show databases" 
| grep -Ev 'Database|information_schema' 
| while read dbname; 
do 
echo 'Dumping $dbname' 
mysqldump -u[uanme] -p'[pwd]' $dbname > ~/db_backup/$dbname.sql;
done

The echoing part of doesn't work though.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
mysql -uroot -N -e 'show databases' | while read dbname; do mysqldump -uroot --complete-insert --some-other-options "$dbname" > "$dbname".sql; done

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...