Skip to main content
1 of 2

Combine the output of while loop into tar

I'm trying to create a script that mysqldumps all my databases, creating an individual .sql file for each database and combines the output into backup-DATE.tar.gz.

I've gotten as far as being able to create the .sql backups, but the last hump I can't overcome due to my limited knowledge is how to combine all the .sql files into backup-DATE.tar.gz.

This is what I have so far:

mysql -N -u user -p'password' -e 'show databases' | while read dbname; do mysqldump --add-drop-table -u user -p'password' "$dbname" > /backuplocation/"$dbname".sql; done

This outputs all my databases into my backup location individually ie:

/backuplocation/db1.sql
/backuplocation/db2.sql
/backuplocation/db3.sql

Is it possible to combine this output into backup-DATE.tar.gz with this strategy?