you must create an script that generates a text file.
Start sending headers to the browser for a text file and the go sending the contents.
For the consents will be usefus SQL sentences like SHOW TABLES FROM db and SHOW CREATE TABLE tablename
Then start making SELECTs and generating corresponding INSERTS.
It's not as hard as it seems indeed.