Question

I have a typical ubuntu setup and a mysql database. I'm trying to find a way to set a cron job to export each table in the mysql db into individual .csvs into a folder on the server. I'm not entirely sure how to approach this, everything I've finding shows how to take multiple csvs into a mysql db but not the other way around. Any help would be greatly appreciated

Was it helpful?

Solution

You can use the MySQL command line cliet to execute a command something like

select * from someTable into outfile 'somFile.csv' 
  fields optionally enclosed by '"' terminated by ',' lines terminated by '\n';

Stick that in an SQL script file (with additional SELECTs if appropriate) then you can execute:

mysql -u username --password=******** < sqlscript.sql

Add that command to cron with crontab -e and you should be done.

A couple of gotchas: - make sure the user that cron is executing as has appropriate rights to the filesystem - the mySQL user you're logging in as will need FILE privilege - you might need some extra commands to move the CSV files about after they're created.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top