简体   繁体   English

如何使用mysql中的事件定期进行数据库脚本备份

[英]how to take periodically database script backup using event in mysql

I want to take database script backup every day using event in mySql ..I am new to mySql , so unable to find out exact solution..can anybody help me to do so?? 我想每天使用mySql中的事件进行数据库脚本备份。我是mySql的新手,所以无法找到确切的解决方案。有人可以帮助我吗? Tried it using mysqldump utility but it is command promt oriented , i want it to be done through event scheduler only. 使用mysqldump实用工具对其进行了尝试,但它是面向命令promt的,我希望仅通过事件调度程序来完成。

DELIMITER $$
create  EVENT `Backup` 
ON SCHEDULE EVERY 1 minute
STARTS '2016-02-25 17:08:06' ON COMPLETION  PRESERVE ENABLE 
DO 
BEGIN
SET @sql_text = CONCAT("SELECT * FROM purpleaid INTO OUTFILE '/C:/Users/Admin123/Desktop/db/" , DATE_FORMAT( NOW(), '%Y%m%d') , "db.csv'" ); 
PREPARE s1 FROM @sql_text; 
EXECUTE s1; 
DEALLOCATE PREPARE s1;
END $$
DELIMITER ;

tried this , but its for single table only.I want complete database script 尝试过这个,但仅适用于单个表。我想要完整的数据库脚本

You can use information_schema.tables table to get list of tables within a database, and information_schema.columns table to get list of columns (just in case you want to have column names included in the backup files). 您可以使用information_schema.tables表获取数据库中的表列表,并使用information_schema.columns表获取列的列表(以防万一您希望备份文件中包含列名)。

  1. Create a cursor by getting all table names from your database 通过从数据库获取所有表名来创建游标
  2. Loop through the cursor and get the table name into a variable 循环游标并将表名放入变量中
  3. Construct your select ... into outfile ... statements the same way as you do in your current code, just add the table name from the variable. 就像在当前代码中一样,将select ... into outfile ...构造select ... into outfile ...语句,只需从变量中添加表名即可。
  4. Execute the prepared statement. 执行准备好的语句。

If you want to add the column names dynamically to the output, then combine Joe's and matt's answers from this SO topic . 如果要动态地将列名添加到输出中,请结合来自SO主题的 Joe和matt的答案。

UPDATE 更新

For views, stored procedures, functions, and triggers (and tables, for that matter) the issue is that you can't really interact with show create ... statements' results within sql. 对于视图,存储过程,函数和触发器(就此而言,表)的问题是,您不能真正在sql中与show create ...语句的结果进行交互。 You can try to recreate their definitions from their respective information_schema tables , but as far as I know, it is not possible to fully reconstruct each object just based on these tables. 您可以尝试从各自的information_schema表中重新创建它们的定义,但是据我所知,仅根据这些表就不可能完全重建每个对象。 You need to use an external tool for that, such us mysqldump. 您需要为此使用外部工具,例如mysqldump。 If you want a full backup option, then you would be a lot better off, if you used an external tool, that is scheduled by the OS' task scheduler. 如果您想要一个完整的备份选项,那么,如果使用的是由OS的任务计划程序计划的外部工具,则情况会好得多。

Since table structures and other database objects do not change that often (at least, not in production), you can use external tool to back up the structure and use the internal scheduled script to regularly back up the contents. 由于表结构和其他数据库对象不会经常更改(至少在生产环境中不会更改),因此您可以使用外部工具备份结构,并使用内部计划脚本定期备份内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM