0

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
    /db2.sql
    /db3.sql

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

Edited to better explain my goal:

My goal is to run this script and get something like the following:

/backuplocation/
    /backup-DATE.tar.gz/
        /db1.sql
        /db2.sql
        /db3.sql

Instead of this:

/backuplocation/
    /db1.sql
    /db2.sql
    /db3.sql
  • 1
    You want to tar the files like tar -f "backup-$(date).tar.gz" -cz /backuplocation/db[123].sql or you want to tar the output instream like this? – mikeserv Feb 27 '15 at 21:45
  • @mikeserv I edited the post to try to better explain my goal – Patrick Ryan Feb 27 '15 at 21:54
  • I see the edit, but I assume that means the first thing then? You just want to put three files in a compressed tar archive, right? You -create an archive with -c, you specify the -file output w/ -f and you add files by name. If you use pax the process is very similar as well. – mikeserv Feb 27 '15 at 21:56
  • Yes, it would be the first thing. I'm just not clear on how I would implement your first comment into my existing script – Patrick Ryan Feb 27 '15 at 22:06
  • Well, not to be rude, but I'm probably more lost than you are on that score - I've never even seen your script. – mikeserv Feb 28 '15 at 0:07
0

You could modify your script so that it adds backup$(date) instead of the generic "backuplocation" (and first create a folder called backuplocation only once) then:

tar -czf backuplocation/backup-$(date).tar.gz backup$(date)
mv backup-$(date).tar.gz backuplocation/
rm -rf backup$(date)
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.