2

I am going to backup mySQL database in a period of time. For example: Only backup the data from January to June.

Is it possible to do that? The reason I want to do this is I don't want to back up the database that is too old. If it is possible please give me some suggestion how to do that. Thanks.

user2717047
  • 23
  • 1
  • 3

3 Answers3

0

I am assuming you want to backup table ROWS based on date, not tables themselves.

If your tables have a date or timestamp column, then you can backup certain rows based on date using an INTO OUTFILE query, for example.

Or, from the command line, you could use mysqldump with the --where option.

See this answer: MYSQL Dump only certain rows

Community
  • 1
  • 1
Buttle Butkus
  • 9,206
  • 13
  • 79
  • 120
  • 2
    If I need to backup the whole database that included all table in this way, this --where option will still work for this case? The answer provided seems like just backing up certain row of data in a specific table only. – user2717047 Aug 26 '13 at 06:44
0

Try this :

exec("mysqldump --opt -u$db_user -p$db_pass --no-create-info --where date >= '2013-08-01' my_database my_table > backup.sql");
Arun Kumar M
  • 848
  • 9
  • 14
0

You have several ways to do it,you can use shell script and run mysqldump as cronjob,you should only write something like following script ,then add this to your crobjob.

#!/bin/bash
mysqldump --add-drop-table -h <hostname> -u <username> --password=<password> --all-databases > backup.sql
filename="backup.sql."`eval date +%Y%m%d`".tgz"
tar -czf $filename backup.sql 
rm backup.sql 

then edit crobjob by crontab -e and add something like following line:

* * * * * /usr/local/bin/php /home/moein/projects/WebService/index.php
#this cronjob executed every min.

other way you can use event in mysql to get backup.but your mysql version should be > 5.1.8

better description at mysql dev page.

Moein Hosseini
  • 4,309
  • 15
  • 68
  • 106