Jump to content

Individual Table Backup


davidz

Recommended Posts

I am currently running a cron job that dumps my MYSQL databases every night with a command like this:

mysqldump --opt -uUsername -pPassword DBName > /home/sqldump/DBName.sql

This is working great, and I've even restored from it successfully a couple times.  But now the files are getting too large.  My main database is over 3 gigs when its dumped.

 

So what I want to do is dump each individual table to separate .sql files.  But I don't want to have some list that I have to maintain.  I found this bit of code that is close:

echo "SHOW TABLES" | mysql -uUsername -pPassword -D DBName | grep ^tablename | xargs mysqldump -uUsername -pPassword DBName > /home/sqldump/tablename.sql

 

Does anyone have a better solution that does not require a tablename, or dynamically gets the list of table names?

 

 

Thanks!!

David

Link to comment
Share on other sites

  • 2 weeks later...

First, you should be wary of putting the password in plaintext, since it will be visible in log files, top, etc.... but SHOW TABLES is "dynamic", so to speak... besides, only MyISAM tables are individual (well, new innodb has table-based datafiles, too, but you can't mysqldump them easily).

Link to comment
Share on other sites

Thanks for the tip on the password issue, I'll come up with something there.

 

As for the tables, we use all MyISAM tables, so that's not a problem.

 

How about this,  use the command:

echo "SHOW TABLES" |mysql -uUSERNAME -pPASSWORD -D DBNAME

This will produce output like the following:

Tables_in_DBNAME
tblUserData
tblUserPrefs
tblStats

...This will list all the tables in the DB. 

So what I can then do is drop this output to a file instead:

echo "SHOW TABLES" |mysql -uUSERNAME -pPASSWORD -D DBNAME > tablelist

Then use perl to go through the file 'tablelist' line by line (skipping the first line) and perform:

mysqldump -uUSERNAME -pPASSWORD DBNAME > $tablename.sql

Substituting $tablename for the line that I'm on.

 

 

Thanks for the input!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.