[adsense id=”0514458240″ width=”468″ height=”60″]
The mysqldump utility allows you to backup a database and/or an individual table. The mysqldump generates the CREATE TABLE statements along with the INSERT statements to populate the table. This is extremely useful as it allows information to be applied to whatever MySQL residing on whatever host platform.
One of the features of the MySQL mysqldump utility is to adapt the individual queries in order to limit the rows selected in the backup. This is achieved with the “-w” or “–where” which appends the same WHERE clause on each of the tables.
So how does this work. Well if we have a table t1date with the following rows:
mysql> select * from t1date; +----+------------+------------+ | id | sdate | edate | +----+------------+------------+ | 1 | 2013-05-01 | 2013-05-01 | | 2 | 2013-05-01 | 2013-05-01 | | 3 | 2001-10-01 | 2001-11-01 | | 4 | 2005-10-01 | 2005-11-01 | +----+------------+------------+ 4 rows in set (0.00 sec) mysql>
We can use the mysqldump utility to dump a selection of the records. For example, we want to backup only rows where “sdate” is between the Jan-02 and Jan-09. This should only give us one record (id = 4).
shell> mysqldump -w "sdate between str_to_date('20020101','%Y%m%d') and str_to_date('20090101','%Y%m%d')" test t1date -- MySQL dump 10.11 -- -- Host: localhost Database: test -- ------------------------------------------------------ -- Server version 5.0.45 /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; /*!40103 SET TIME_ZONE='+00:00' */; /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; -- -- Table structure for table `t1date` -- DROP TABLE IF EXISTS `t1date`; CREATE TABLE `t1date` ( `id` int(11) NOT NULL auto_increment, `sdate` date default NULL, `edate` date default NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1; -- -- Dumping data for table `t1date` -- -- WHERE: sdate between str_to_date('20020101','%Y%m%d') and str_to_date('20090101','%Y%m%d') LOCK TABLES `t1date` WRITE; /*!40000 ALTER TABLE `t1date` DISABLE KEYS */; INSERT INTO `t1date` VALUES (4,'2005-10-01','2005-11-01'); /*!40000 ALTER TABLE `t1date` ENABLE KEYS */; UNLOCK TABLES; /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; -- Dump completed on 2011-03-28 16:08:39