Search code examples
mysqlbackup

mysqldump for particular ID and date range


mysqldump -uroot -p --no-create-info --no-create-db hawa_new scada_data wind_turbine  "SELECT  s.* FROM scada_data as s,wind_turbine as w WHERE s.local_tm BETWEEN '2018-02-01' AND '2018-02-28' AND s.turbine = w.id AND w.pooling_station = 29" |gzip > /var/lib/mysql-files/sd_feb.sql.gz

In which I got the ERROR

Couldn't find table: "SELECT  s.* FROM scada_data as s,wind_turbine as w WHERE s.local_tm BETWEEN '2018-02-01' AND '2018-02-28' AND s.turbine = w.id AND w.pooling_station = 29"

Solution

  • There are 2 approaches you could take here depending on whether you want a dump of INSERT statements or are happy dumping data to a CSV format to use elsewhere with LOAD DATA INFILE. Both versions assume you want to use gzip and have access to the command line.

    mysqldump method

    You can get use the mysqldump --where clause to filter for records that match values in a second table but you'd need to make a few changes to what you have now

    Change your --where clause to remove the SELECT and FROM parts and match on wind_turbine records in a sub-select

    Add a switch to ensure that you get a consistent dump from the database (either --lock-all-tables or --single-transaction).

    Remove the table wind_turbine from the list of tables because it will not match the --where clause and you only want to dump the data in scada_data.

    Something like this should work:

    mysqldump -uroot -p --single-transaction --no-create-info --no-create-db  --where "local_tm BETWEEN '2018-02-01' AND '2018-02-28' AND turbine IN (SELECT id FROM wind_turbine WHERE pooling_station = 29" hawa_new scada_data | gzip > /var/lib/mysql-files/sd_feb.sql.gz
    

    SELECT INTO OUTFILE method

    (which is identical to your original query and probably the way I'd do this myself)

    mysql -e "SELECT s.* INTO OUTFILE '/var/lib/mysql-files/sd_feb.sql' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n'
    FROM scada_data as s,wind_turbine as w WHERE s.local_tm BETWEEN '2018-02-01' AND '2018-02-28' AND s.turbine = w.id AND w.pooling_station = 29"; gzip /var/lib/mysql-files/sd_feb.sql