tags:

views:

2465

answers:

5

I've written a big sql script that creates a CSV file. I want to call a cronjob every night to create a fresh CSV file and have it available on the website.

Say for example I'm store my file in '/home/sites/example.com/www/files/backup.csv'

and my SQL is

SELECT * INTO OUTFILE '/home/sites/example.com/www/files/backup.csv'
  FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
  LINES TERMINATED BY '\n'
  FROM ( ....

MySQL gives me an error when the file already exists

File '/home/sites/example.com/www/files/backup.csv' already exists

Is there a way to make MySQL overwrite the file?

I could have PHP detect if the file exists and delete it before creating it again but it would be more succinct if I can do it directly in MySQL.

A: 

Why not rm -f /home/sites/example.com/www/files/backup.csv in the script ran by cron?

Flavius Stef
yea I can do that but it would be neater to do it in MySQL because loops through many databases and creates 100s of files. Looks like i may have to do it in PHP though.
Derek Organ
I went through this exact same process of trying to find a way and the conclusion was that you can't, as the docs mention, for security.
Artem Russakovskii
+5  A: 

No, there's no way to overwrite it. From the docs:

file_name cannot be an existing file, which among other things prevents files such as /etc/passwd and database tables from being destroyed.

It might be a better idea to use a different filename each night, as having multiple backups means you can recover from problems that have existed for more than a day. You could then maintain a symlink that always points at the latest complete csv.

cheers, thought i might not be able. agh well. Its not quite a backup its more a user accessable backup. Backups are done seperately. Still, food for thought.
Derek Organ
Have you checked out the linux *logrotate* tool?
Konerak
+2  A: 

For a job like this I would place it into a bash file, delete the file

#!/bin/bash
rm /path/to/backup.csv
./backup_sql_query.sh  <<-- This contains the script to backup to CSV.

The better option is to actually add a timestamp though. Disk space isn't expensive in this day and age.

no not expensive, just awkward to update the HTML link to the new file every day. Possible, yes but probably more work than it needs. thanks.
Derek Organ
just `ln` the `backup.csv` to the latest and greatest version as soon as it's been made - make new version, rm backup.csv, ln newversion backup.csv -- ta-da, you're done.
Alex Martelli
yea i like the ln -s idea. hmm
Derek Organ
A: 

How to pass outfilename with time . Is it possible?

Regards, Mani B Pace Automation Ltd, Chennai

mani
A: 

There is no way. Only one possible you can procedure with dynamic statement.

CREATE PROCEDURE export_dynamic(IN file_name char(64)) BEGIN set @myvar = concat('SELECT * INTO OUTFILE ',"'",file_name,"'",' FROM Table1') ; PREPARE stmt1 FROM @myvar; EXECUTE stmt1; Deallocate prepare stmt1; END;

Regards, Mani B Pace Automation
Chennai

mani