Marty Wallace
Marty Wallace

Reputation: 35734

Use php to backup a specific database table and compress it

Using php is it possible to create a backup of a specific mysql database table using php.

Additionally I would like it gzipped also.

I would like it so the file was ready to be loaded directly in using the standard mysql import command i.e.

mysql -uuser -ppass mydb < mybackupfile.sql

Current solutions i am looking at involve iterating over each row and adding to a file - however this doesnt seem correct.

Upvotes: 0

Views: 334

Answers (2)

mmeyer2k
mmeyer2k

Reputation: 422

You could use php to execute a mysqldump command. Like so...

exec("mysqldump -uuser -ppass --compact mydb mytable > filename.sql");

Then run another command to compress it however you like.

Upvotes: 1

tadman
tadman

Reputation: 211590

The best tool for this job is mysqldump and gzip combined:

mysqldump ...options... | gzip -9 > backup.sql.gz

As a note, you'll need to reverse this process before restoring:

gunzip -dc backup.sql.gz | mysql ...options...

PHP itself might be able to initiate this process but it's not an exceptionally good tool on its own. You could try phpMyAdmin and see if that works for you.

Upvotes: 0

Related Questions