How to automatically generate CSVs at given intervals from phpMyAdmin
How can I d开发者_开发技巧o this? Can it be done with cron jobs? If so, where can I read up on how to do this?
I need to dump the data from my database into a CSV file every so many hours. I want the CSV file to be saved into a folder on the server. Is this even possible with MySQL? I'd appreciate any help on this.
If you have proper server access I suggest mysqldump
from a cron job.
Read more about using mysqldump to output a CSV.
PHP isn't necessary. You can schedule a query to run periodically via a cron job and output to CSV using code like the following:
SELECT col1, col2, col3 FROM table WHERE whatever='whatever' INTO OUTFILE 'reports/result.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
ESCAPED BY ‘\\’
LINES TERMINATED BY '\n'
With the above query stored in a file, the cron command would look like
mysql -u username -p password < filewiththequery.sql
If you need to dump the whole database in a way that can be re-imported to the database, use mysqldump
instead.
make a script with the following in it:
select * into outfile '/tmp/data.csv'
fields terminated by ',' ecnlosed by '"' escaped by'\' lines terminated by'\n' from table;
to schedule the job crontab -u username -e
0 24/4 * * * user scriptname
That should give you a dump every 6 hours.
You will need to read up on some scripting language to provide a unique name to your mysql statement each time it is run.
精彩评论